You are on page 1of 6

Practical no.

32
Write a program to draw a route between two location
Java Code: package com.example.pr32;
import static android.content.Intent.ACTION_VIEW;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText source, destination;
Button track;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
source = findViewById(R.id.et_source);
destination = findViewById(R.id.et_destination);
track = findViewById(R.id.track);
track.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String sSource = source.getText().toString();
String sDestination = destination.getText().toString();
if (sSource.equals("") && sDestination.equals("")) {
Toast.makeText(MainActivity.this,
"Please enter your Source/Destination",
Toast.LENGTH_SHORT).show();}
else{
DisplayTrack(sSource, sDestination);}}});}
private void DisplayTrack(String sSource, String sDestination) {
try {
// Construct the Google Maps URL with source and destination addresses
Uri uri = Uri.parse("https://www.google.com/maps/dir/?api=1&origin="
+ sSource + "&destination=" + sDestination);
Intent intent = new Intent(ACTION_VIEW, uri);
intent.setPackage("com.google.android.apps.maps");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);}
catch (ActivityNotFoundException e){
// If Google Maps app is not installed, open the Google Play Store link
Uri uri=
Uri.parse("https://play.google.com/store/apps/details?id=com.google.android.apps.maps");
Intent intent = new Intent(ACTION_VIEW,uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);}}}
XML File: <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ECECEC"
android:padding="20dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Source:"
android:textSize="36sp"
android:layout_marginTop="100dp"
android:layout_centerHorizontal="true"
android:id="@+id/et_source"
android:textColor="#212121"
android:textColorHint="#575757"
android:backgroundTint="#575757"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Destination:"
android:textSize="36sp"
android:layout_marginTop="200dp"
android:layout_centerHorizontal="true"
android:id="@+id/et_destination"
android:textColor="#212121"
android:textColorHint="#575757"
android:backgroundTint="#575757"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DRAW ROUTE"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="300dp"
android:id="@+id/track" />
</RelativeLayout>
Output-
Practical no. 31
Write a program to locate a user’s current location.
Java Code: package com.example.pr31;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Looper;
import android.provider.Settings;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
public class MainActivity extends AppCompatActivity {
FusedLocationProviderClient mFusedLocationClient;
TextView latitudeTextView, longitTextView;
int PERMISSION_ID = 44;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latitudeTextView = findViewById(R.id.latitude);
longitTextView = findViewById(R.id.longitude);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
getLastLocation();}
@SuppressLint("MissingPermission")
private void getLastLocation() {
if (checkPermissions()) {
if (isLocationEnabled()) {
mFusedLocationClient.getLastLocation().addOnCompleteListener(this, new
OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
Location location = task.getResult();
if (location == null) {
requestNewLocationData();
} else {
latitudeTextView.setText(location.getLatitude() + "");
longitTextView.setText(location.getLongitude() + "");}}
});} else {
Toast.makeText(this, "Please turn on your location...",
Toast.LENGTH_LONG).show();
Intent intent = new
Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}} else {
requestPermissions();}}
@SuppressLint("MissingPermission")
private void requestNewLocationData() {
LocationRequest locationRequest = new LocationRequest();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(5);
locationRequest.setFastestInterval(0);
locationRequest.setNumUpdates(1);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mFusedLocationClient.requestLocationUpdates(
locationRequest, locationCallback, Looper.myLooper());}
private LocationCallback locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
Location location = locationResult.getLastLocation();
latitudeTextView.setText("Latitude: " + location.getLatitude() + "");
longitTextView.setText("Longitude: " + location.getLongitude() + "");}};
private boolean checkPermissions() {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) ==
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
return true;}
return false;}
private void requestPermissions() {
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_ID);}
private boolean isLocationEnabled() {
LocationManager locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
return
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull
String[]
permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
if (requestCode == PERMISSION_ID) {
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
getLastLocation();}}}
@Override
public void onResume() {
super.onResume();
if (checkPermissions()) {
getLastLocation();}}}

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


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ECECEC"
android:padding="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Latitude:"
android:textSize="36sp"
android:textStyle="bold"
android:layout_marginTop="150dp"
android:layout_centerHorizontal="true"
android:id="@+id/latitudeValue"
android:textColor="#212121"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="28sp"
android:textStyle="bold"
android:layout_below="@+id/latitudeValue"
android:layout_centerHorizontal="true"
android:id="@+id/latitude"
android:textColor="#424242"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Longitude:"
android:textSize="36sp"
android:textStyle="bold"
android:layout_marginTop="300dp"
android:layout_centerHorizontal="true"
android:id="@+id/longitudeValue"
android:textColor="#212121"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="28sp"
android:textStyle="bold"
android:layout_below="@+id/longitudeValue"
android:layout_centerHorizontal="true"
android:id="@+id/longitude"
android:textColor="#424242"/>
</RelativeLayout>
Output-
Practical no. 29
Write a program to send & receive SMS.
Java Code: package com.example.pr29;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText mobileno, message;
Button sendsms;
private static final int PERMISSION_REQUEST_CODE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mobileno = (EditText) findViewById(R.id.editText1);
message = (EditText) findViewById(R.id.editText2);
sendsms = (Button) findViewById(R.id.button1);
sendsms.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String no = mobileno.getText().toString();
String msg = message.getText().toString();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(android.Manifest.permission.SEND_SMS)
== PackageManager.PERMISSION_GRANTED) {
sendSms(no, msg);} else {
requestPermissions(new
String[]{android.Manifest.permission.SEND_SMS},
PERMISSION_REQUEST_CODE);}} else {
sendSms(no, msg);}}});}
private void sendSms(String phoneNumber, String message) {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);
Toast.makeText(getApplicationContext(), "Message Sent successfully!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Failed to send SMS. Please try again.", Toast.LENGTH_LONG).show();
e.printStackTrace();}}
@Override
public void onRequestPermissionsResult(int requestCode, String[]
permissions, int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
String no = mobileno.getText().toString();
String msg = message.getText().toString();
sendSms(no, msg);} else {
Toast.makeText(getApplicationContext(), "SMS permission denied. Please try again.",
Toast.LENGTH_LONG).show();
}}}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;}}

XML File(Layout): <?xml version="1.0" encoding="utf-8"?>


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_margin="20dp">
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone Number"
android:ems="10"/>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="SMS Message"
android:inputType="textMultiLine" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:text="Send SMS" />
</LinearLayout>

AndroidManifest.XML File: <uses-permission android:name="android.permission.SEND_SMS"/>


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

XML File(Menu): <?xml version="1.0" encoding="utf-8"?>


<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
</menu>

Output-

You might also like