Mad 5-1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Camera

MainActivity.java

package com.example.pa;

import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity


{
private static final int REQUEST_IMAGE_CAPTURE = 1;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

findViewById(R.id.cameraButton).setOnClickListener(view ->
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
{ } }}

Activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/cameraButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Photo" />

</RelativeLayout>
Bluetooth

MainActivity.java

import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity


{
private BluetoothAdapter bluetoothAdapter;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

Button enableBluetoothButton = findViewById(R.id.enableBluetoothButton);


Button scanDevicesButton = findViewById(R.id.scanDevicesButton);

enableBluetoothButton.setOnClickListener(v ->
{
if (bluetoothAdapter != null && !bluetoothAdapter.isEnabled())
{
bluetoothAdapter.enable();
Toast.makeText(this, "Bluetooth Enabled", Toast.LENGTH_SHORT).show();
}
});

scanDevicesButton.setOnClickListener(v ->
{
if (bluetoothAdapter != null)
{
bluetoothAdapter.startDiscovery();
Toast.makeText(this, "Scanning for Devices", Toast.LENGTH_SHORT).show();
}
});
}

@Override
protected void onDestroy()
{
super.onDestroy();
if (bluetoothAdapter != null) {
bluetoothAdapter.cancelDiscovery();
}
}
}

Activity_main.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
android:id="@+id/enableBluetoothButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enable Bluetooth" />

<Button
android:id="@+id/scanDevicesButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scan Devices" />

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.BLUETOOTH" />


<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />

</manifest>
SMS send

MainActivity.java

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity


{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button sendSmsButton = findViewById(R.id.btn_send_sms);


sendSmsButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String phoneNumber = "1234567890";
String message = "Hello, this is a test SMS from my app!";

Intent intent = new Intent(Intent.ACTION_VIEW);


intent.setData(Uri.parse("smsto:" + phoneNumber));
intent.putExtra("sms_body", message);

startActivity(intent);
}
});
}
}
Activity_main.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
android:id="@+id/btn_send_sms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send SMS" />

</LinearLayout>

Displaying the zoom control

MainActivity.java

import android.os.Bundle;
import android.util.Log;
import android.widget.ZoomControls;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity


{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ZoomControls zoomControls = findViewById(R.id.zoomControls);

zoomControls.setOnZoomInClickListener(v ->
{

Log.d("ZoomControl", "Zooming in");


});

zoomControls.setOnZoomOutClickListener(v ->
{

Log.d("ZoomControl", "Zooming out");


});
}
}
Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ZoomControls
android:id="@+id/zoomControls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />

</RelativeLayout>

Navigating to a specific location

MainActivity.java

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity


{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

double latitude = 37.4221;


double longitude = -122.0841;

Uri locationUri = Uri.parse("geo:" + latitude + "," + longitude);

Intent intent = new Intent(Intent.ACTION_VIEW, locationUri);

if (intent.resolveActivity(getPackageManager()) != null)
{
startActivity(intent);
}
}
}
Geo Coding and Reverse Geo coding

MainActivity.java

import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Toast;
import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class MainActivity extends AppCompatActivity


{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

Geocoder geocoder = new Geocoder(this, Locale.getDefault());

try
{
List<Address> geocodeResult = geocoder.getFromLocationName("1600 Amphitheatre
Parkway, Mountain View, CA", 1);

if (!geocodeResult.isEmpty())
{
Address addr = geocodeResult.get(0);
Toast.makeText(this, "Lat: " + addr.getLatitude() + ", Lon: " + addr.getLongitude(),
Toast.LENGTH_LONG).show();
}
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
List<Address> reverseGeocodeResult = geocoder.getFromLocation(37.4220, -
122.0841, 1);
if (!reverseGeocodeResult.isEmpty())
{
Toast.makeText(this, reverseGeocodeResult.get(0).getAddressLine(0),
Toast.LENGTH_LONG).show();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />


<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

</manifest>

You might also like