0% found this document useful (0 votes)
0 views

Codes-

The document contains multiple Android application code snippets demonstrating various functionalities including displaying the current location on Google Maps, routing to a specific location, sending and receiving SMS, sending emails, and capturing images from the camera. Each section includes XML layout files and corresponding Java code for implementing these features. Permissions required for each functionality are also specified in the code.

Uploaded by

gautamsp22hdcomp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Codes-

The document contains multiple Android application code snippets demonstrating various functionalities including displaying the current location on Google Maps, routing to a specific location, sending and receiving SMS, sending emails, and capturing images from the camera. Each section includes XML layout files and corresponding Java code for implementing these features. Permissions required for each functionality are also specified in the code.

Uploaded by

gautamsp22hdcomp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Codes-

1 Google Map show current loc with map


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/f1"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

package com.example.displaygooglemapwithcurrloc;

import android.Manifest;
import android.os.*;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.google.android.gms.location.*;
import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;

public class MainActivity extends AppCompatActivity implements OnMapReadyC


allback {
GoogleMap mMap;
FusedLocationProviderClient flpc;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flpc = LocationServices.getFusedLocationProviderClient(this);

SupportMapFragment frag = (SupportMapFragment) getSupportFragmentMa


nager().findFragmentById(R.id.f1);
if (frag != null) {
frag.getMapAsync(this);
}
}

@Override
public void onMapReady(GoogleMap gmap) {
mMap = gmap;
if (checkPermission()) {
getCurrentLoc();
}
}

private void getCurrentLoc() {


if (checkPermission()) {
mMap.setMyLocationEnabled(true);
flpc.getCurrentLocation(
100,
null
).addOnSuccessListener(
this, loc -> {
if (loc != null) {
LatLng uLoc = new LatLng(loc.getLatitude(), loc.
getLongitude());
mMap.addMarker(new MarkerOptions()
.position(uLoc)
.title("Current Loc"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZo
om(uLoc, 15));
}
}
);

}
}

public boolean checkPermission() {


if (ContextCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != 0) {
ActivityCompat.requestPermissions(
this,
new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
},
100
);
return false;
}
return true;
}

@Override
public void onRequestPermissionsResult(int rc, String[] p, int[] gr) {
super.onRequestPermissionsResult(rc, p, gr);
if (rc == 100 && gr.length > 0 && gr[0] == 0) {
getCurrentLoc();
}
}
}

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


<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
/>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyBk4wXYf4tBiBTw" />

2 User loc to bandra MSBTE


package com.example.displaygooglemapwithcurrloc;

import android.Manifest;
import android.graphics.Color;
import android.os.*;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.google.android.gms.location.*;
import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
public class MainActivity extends AppCompatActivity implements OnMapReadyC
allback {
GoogleMap mMap;
FusedLocationProviderClient flpc;
private final LatLng MSBTE_LOCATION = new LatLng(19.063767, 72.8427585);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flpc = LocationServices.getFusedLocationProviderClient(this);

SupportMapFragment frag = (SupportMapFragment) getSupportFragmentMa


nager().findFragmentById(R.id.f1);
if (frag != null) {
frag.getMapAsync(this);
}
}

@Override
public void onMapReady(GoogleMap gmap) {
mMap = gmap;
if (checkPermission()) {
getRoute();
}
}

private void getRoute() {


if (checkPermission()) {
mMap.setMyLocationEnabled(true);
mMap.addMarker(new MarkerOptions()
.position(MSBTE_LOCATION)
.title("MSBTE Bandra"));

flpc.getCurrentLocation(
100,
null
).addOnSuccessListener(
this, loc -> {
if (loc != null) {
LatLng uLoc = new LatLng(loc.getLatitude(), loc.
getLongitude());
mMap.addMarker(new MarkerOptions()
.position(uLoc)
.title("Current Location"));

mMap.addPolyline(new PolylineOptions()
.add(uLoc, MSBTE_LOCATION)
.width(5)
.color(Color.BLUE));

mMap.moveCamera(CameraUpdateFactory.newLatLngZo
om(
new LatLng(
(uLoc.latitude + MSBTE_LOCATION.
latitude) / 2,
(uLoc.longitude + MSBTE_LOCATIO
N.longitude) / 2
), 12)
);
}
}
);
}
}

public boolean checkPermission() {


if (ContextCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != 0) {
ActivityCompat.requestPermissions(
this,
new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
},
100
);
return false;
}
return true;
}

@Override
public void onRequestPermissionsResult(int rc, String[] p, int[] gr) {
super.onRequestPermissionsResult(rc, p, gr);
if (rc == 100 && gr.length > 0 && gr[0] == 0) {
getRoute();
}
}
}

3 Send and Receive SMS -

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<EditText
android:id="@+id/e1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone Number" />

<EditText
android:id="@+id/e2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message" />

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

<TextView
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

package com.example.displaygooglemapwithcurrloc;
import android.Manifest;
import android.content.*;
import android.os.*;
import android.provider.Telephony;
import android.telephony.*;
import android.widget.*;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity {


String act = Telephony.Sms.Intents.SMS_RECEIVED_ACTION;
BroadcastReceiver br = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent it) {
if (it.getAction().equals(act)) {
SmsMessage[] mess = Telephony.Sms.Intents.getMessagesFromIn
tent(it);
String pno = mess[0].getOriginatingAddress();
String mes = mess[0].getMessageBody();
((TextView) findViewById(R.id.t1)).setText(pno + "\n" + me
s);
}
}
};

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

registerReceiver(br, new IntentFilter(act));


SmsManager sms = SmsManager.getDefault();
checkPermission();

findViewById(R.id.btn).setOnClickListener(v -> {
String pno = ((EditText) findViewById(R.id.e1)).getText().toStr
ing();
String mess = ((EditText) findViewById(R.id.e2)).getText().toSt
ring();
sms.sendTextMessage(pno, null, mess, null, null);
Toast.makeText(this, "SMS sent!", Toast.LENGTH_SHORT).show();
});
}
public void checkPermission() {
if (ContextCompat.checkSelfPermission(
this,
Manifest.permission.SEND_SMS
) == -1 && ContextCompat.checkSelfPermission(
this,
Manifest.permission.RECEIVE_SMS
) == -1) {
ActivityCompat.requestPermissions(
this,
new String[]{
Manifest.permission.SEND_SMS,
Manifest.permission.RECEIVE_SMS
},
100
);
}
}

@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(br);
}
}

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


<uses-permission android:name="android.permission.SEND_SMS" />
<uses-feature android:name="android.hardware.telephony" />

4 Send Email

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<EditText
android:id="@+id/e1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email ID" />

<EditText
android:id="@+id/e2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Subject" />

<EditText
android:id="@+id/e3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message" />

<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send Email" />

</LinearLayout>

package com.example.displaygooglemapwithcurrloc;

import android.content.Intent;
import android.os.Bundle;
import android.widget.*;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn).setOnClickListener(v -> {
String to = ((EditText) findViewById(R.id.e1)).getText().toStri
ng();
String sub = ((EditText) findViewById(R.id.e2)).getText().toStr
ing();
String mess = ((EditText) findViewById(R.id.e3)).getText().toSt
ring();

Intent it = new Intent(Intent.ACTION_SEND);


it.setType("message/rfc822");

it.putExtra(Intent.EXTRA_EMAIL, new String[]{to});


it.putExtra(Intent.EXTRA_SUBJECT, sub);
it.putExtra(Intent.EXTRA_TEXT, mess);
startActivity(it);
});
}
}

5 Click and Image and display in Image View -

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/img"
android:layout_width="300dp"
android:layout_height="300dp" />

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Image"/>
</LinearLayout>

package com.example.displaygooglemapwithcurrloc;

import android.Manifest;
import android.content.*;
import android.graphics.*;
import android.os.Bundle;
import android.provider.*;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity {


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

findViewById(R.id.btn).setOnClickListener(v -> {
if(ContextCompat.checkSelfPermission(
this,
Manifest.permission.CAMERA
) == -1) {
ActivityCompat.requestPermissions(
this,
new String[]{
Manifest.permission.CAMERA
},
100
);
}
else {
startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_C
APTURE), 1);
}
});
}

@Override
protected void onActivityResult(int req, int res, Intent it) {
super.onActivityResult(req, res, it);
if (req == 1 && res == -1) {
Bitmap map = (Bitmap) it.getExtras().get("data");
((ImageView) findViewById(R.id.img)).setImageBitmap(map);
}
}
}

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


<uses-feature android:name="android.hardware.camera" />

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy