0% found this document useful (0 votes)
219 views25 pages

Android Dev: Key Coding Concepts

The document discusses important concepts for coding in Android including the AndroidManifest.xml file, activity_main.xml layout file, a basic MainActivity class, adding button click listeners, and creating custom toasts. It also provides code examples for geocoding, sending and receiving SMS messages, and using an SQLite database.

Uploaded by

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

Android Dev: Key Coding Concepts

The document discusses important concepts for coding in Android including the AndroidManifest.xml file, activity_main.xml layout file, a basic MainActivity class, adding button click listeners, and creating custom toasts. It also provides code examples for geocoding, sending and receiving SMS messages, and using an SQLite database.

Uploaded by

Mrugesh Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

V2V EdTech LLP | MAD VIMP CODES (CO/IT) 22617

Important Concepts for Coding:

1. [Link]

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


<manifest xmlns:android="[Link]
xmlns:tools="[Link]
package="[Link]"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="27" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/[Link]"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]"/>
</intent-filter>
</activity>
</application>
</manifest>

<manifest xmlns:android="[Link]
package="[Link]">

<application
...>

<service android:name=".MyService" />

<activity android:name=".MainActivity">
...
</activity>
</application>

</manifest>
V2V EdTech LLP | MAD VIMP CODES (CO/IT) 22617

2. Activity_main.xml

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


<RelativeLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

</RelativeLayout>

3. [Link]

import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
}
}

4. Button onClick

Button myButton = findViewById([Link]);

[Link](new [Link]() {
@Override
public void onClick(View v) {
[Link]([Link], "Button clicked!",
Toast.LENGTH_SHORT).show();
}
});

5. Creating Custom Toast Message

Toast toast = new Toast(getApplicationContext());


[Link](Toast.LENGTH_SHORT);
[Link](layout);
[Link]();
V2V EdTech LLP | MAD VIMP CODES (CO/IT) 22617

1. Write a Program for implementing Geocoding and reverse Geocoding.

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];
import [Link];

import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

private Geocoder geocoder;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

geocoder = new Geocoder(this, [Link]());

String locationName = "1600 Amphitheatre Parkway, Mountain View,


CA";
try {
List<Address> addresses =
[Link](locationName, 1);
if (![Link]()) {
Address address = [Link](0);
double latitude = [Link]();
double longitude = [Link]();
displayToast("Geocoding Result: Latitude - " + latitude +
", Longitude - " + longitude);
} else {
displayToast("Location not found!");
}
} catch (IOException e) {
[Link]();
displayToast("Geocoding failed: " + [Link]());
}
V2V EdTech LLP | MAD VIMP CODES (CO/IT) 22617
double latitude = 37.423021;
double longitude = -122.083739;
try {
List<Address> addresses = [Link](latitude,
longitude, 1);
if (![Link]()) {
Address address = [Link](0);
String addressText = [Link](0);
displayToast("Reverse Geocoding Result: " + addressText);
} else {
displayToast("Address not found!");
}
} catch (IOException e) {
[Link]();
displayToast("Reverse geocoding failed: " + [Link]());
}
}

private void displayToast(String message) {


[Link](this, message, Toast.LENGTH_SHORT).show();
}
}
2.

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


<uses-permission
android:name="[Link].ACCESS_COARSE_LOCATION" />
<uses-permission android:name="[Link]" />

3. Write a Program to send and receive SMS.

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


<uses-permission android:name="[Link].SEND_SMS"/>
<uses-permission android:name="[Link].READ_SMS"/>
<uses-permission android:name="[Link].WRITE_SMS"/>
<receiver
android:name=".SmsReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="[Link].SMS_RECEIVED" />
</intent-filter>
</receiver>

package [Link];

import [Link];
V2V EdTech LLP | MAD VIMP CODES (CO/IT) 22617
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {


SmsReceiver sms = new SmsReceiver();
EditText et1, et2;
Button b1;

@Override
protected void onCreate (Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
et1=findViewById([Link]);
et2=findViewById([Link]);
b1=findViewById([Link]);
if([Link]([Link],[Link].
SEND_S
MS)!=
PackageManager.PERMISSION_GRANTED)
{
[Link]([Link],new
String[]{[Link].SEND_SMS},100);
}
[Link](new [Link]() {
@Override
public void onClick(View v) {
try {
String phno= [Link]().toString();
String msg=[Link]().toString();
SmsManager smsManager= [Link]();
[Link](phno,null,msg,null,null);
[Link]([Link],"Sms sent successfully",
Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
[Link]([Link],"Sms failed to send... try again",
Toast.LENGTH_LONG).show();
}
V2V EdTech LLP | MAD VIMP CODES (CO/IT) 22617
}
});
}

@Override
protected void onStart() {
[Link]();
IntentFilter filter = new
IntentFilter("[Link].SMS_RECEIVED");
registerReceiver(sms, filter);
}

@Override
protected void onStop() {
[Link]();
unregisterReceiver(sms);
}
}

package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class SmsReceiver extends BroadcastReceiver {


SmsReceiver() {
}

@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = [Link]();
if (bundle != null) {
// Retrieve the SMS Messages received
Object[] sms = (Object[]) [Link]("pdus");
// For every SMS message received
for (int i=0; i < [Link]; i++) {
// Convert Object array
SmsMessage smsMessage = [Link]((byte[]) sms[i]);
String phone = [Link]();
String message = [Link]().toString();
[Link](context, “Received from “+ phone + ": " + message,
Toast.LENGTH_SHORT).show();
}
}
V2V EdTech LLP | MAD VIMP CODES (CO/IT) 22617
}
}

4. SQLite Database.

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


<RelativeLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:text="Insert Customer Details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/textView"
android:gravity="center"
android:textSize="20dp"
android:textColor="#000000"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="ID"
android:id="@+id/editid"
android:layout_below="@+id/textView"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:id="@+id/editname"
android:layout_below="@+id/editid"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Mobile No."
android:id="@+id/editmobile"
android:layout_below="@+id/editname"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Address"
android:lines="3"
android:id="@+id/editaddress"
android:layout_below="@+id/editmobile"/>
<EditText
android:layout_width="fill_parent"
V2V EdTech LLP | MAD VIMP CODES (CO/IT) 22617
android:layout_height="wrap_content"
android:hint="Pin Code"
android:id="@+id/editpincode"
android:layout_below="@+id/editaddress"/>
<Button
android:text="Insert Data"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editpincode"
android:layout_centerHorizontal="true"
android:id="@+id/button" />
<TextView
android:text="Search Customer Details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:id="@+id/textView1"
android:gravity="center"
android:textSize="20dp"
android:layout_below="@+id/button"
android:textColor="#000000"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter ID"
android:id="@+id/editsearchid"
android:layout_below="@+id/textView1"/>
<Button
android:text="Search Data"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editsearchid"
android:layout_centerHorizontal="true"
android:id="@+id/button1" />
</RelativeLayout>

package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
V2V EdTech LLP | MAD VIMP CODES (CO/IT) 22617

public class MainActivity extends AppCompatActivity {


SQLiteDatabase sqLiteDatabaseObj;
EditText editTextID, editTextName, editMobileNo, editAddress, editPincode,
editSearchid;
String cid, cname, cmobile, caddress, cpincode, sql_query, sid;
Button EnterData, SearchData;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
EnterData = (Button)findViewById([Link]);
SearchData = (Button)findViewById([Link].button1);
editTextID = (EditText)findViewById([Link]);
editTextName = (EditText)findViewById([Link]);
editMobileNo = (EditText)findViewById([Link]);
editAddress = (EditText)findViewById([Link]);
editPincode = (EditText)findViewById([Link]);
editSearchid = (EditText)findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View view) {
sqLiteDatabaseObj = openOrCreateDatabase("AndroidJSonDataBase",
Context.MODE_PRIVATE, null);
[Link]("CREATE TABLE IF NOT EXISTS
AndroidJSonTable(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, cid
VARCHAR, name VARCHAR, mobile VARCHAR, address VARCHAR, pincode
VARCHAR);");
cid = [Link]().toString();
cname = [Link]().toString() ;
cmobile = [Link]().toString();
caddress = [Link]().toString();
cpincode = [Link]().toString();
sql_query = "INSERT INTO AndroidJSonTable (cid, name, mobile, address,
pincode) VALUES('"+cid+"', '"+cname+"', '"+cmobile+"', '"+caddress+"',
'"+cpincode+"');";
[Link](sql_query);
[Link](getApplicationContext(), "Data Inserted Successfully",
Toast.LENGTH_LONG).show();
}
});
[Link](new [Link]() {
@Override
public void onClick(View view) {
sid = [Link]().toString();
Cursor cursor = [Link]( "select * from
AndroidJSonTable
V2V EdTech LLP | MAD VIMP CODES (CO/IT) 22617
where cid="+sid+"", null );
StringBuffer buffer= new StringBuffer();
while ([Link]())
{
String cid =[Link](1);
String name =[Link](2);
String mob =[Link](3);
String addr =[Link](4);
String pcode =[Link](5);
[Link](cid+ " " + name + " " + mob +" " + addr +" " + pcode +"
\n");
[Link](getApplicationContext(), buffer,
Toast.LENGTH_LONG).show();
} }); } }

5. Demonstrate Date and Time Picker

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

private TextView dateTextView;


private TextView timeTextView;
private DatePicker datePicker;
private TimePicker timePicker;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

dateTextView = findViewById([Link]);
timeTextView = findViewById([Link]);
datePicker = findViewById([Link]);
timePicker = findViewById([Link]);
Button fetchDateTimeButton =
findViewById([Link]);
V2V EdTech LLP | MAD VIMP CODES (CO/IT) 22617
[Link](new [Link]() {
@Override
public void onClick(View v) {
fetchDateTime();
}
});
}

private void fetchDateTime() {


int day = [Link]();
int month = [Link]() + 1;
int year = [Link]();

int hour = [Link]();


int minute = [Link]();

// Display the selected date and time in TextViews


[Link](formatDate(day, month, year));
[Link](formatTime(hour, minute));
}

private String formatDate(int day, int month, int year) {


Calendar calendar = [Link]();
[Link](year, month - 1, day); // month is zero-based
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy",
[Link]());
return [Link]([Link]());
}

private String formatTime(int hour, int minute) {


return [Link]([Link](), "%02d:%02d", hour,
minute);
}
}

6. Display Users Current Location

<fragment xmlns:android="[Link]
xmlns:map="[Link]
xmlns:tools="[Link]
android:id="@+id/map"
android:name="[Link]"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="[Link]" />

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


V2V EdTech LLP | MAD VIMP CODES (CO/IT) 22617
<uses-permission android:name="[Link].ACCESS_COARSE_LOCATION"
/>
<uses-permission android:name="[Link]" />

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MapsActivity extends FragmentActivity implements


OnMapReadyCallback,
LocationListener, [Link],
[Link] {

private GoogleMap mMap;


Location mLastLocation;
Marker mCurrLocationMarker;
GoogleApiClient mGoogleApiClient;
LocationRequest mLocationRequest;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_maps);
// Obtain the SupportMapFragment and get notified when the map is
ready to be
V2V EdTech LLP | MAD VIMP CODES (CO/IT) 22617
// used.
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager()
.findFragmentById([Link]);
[Link](this);

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

if ([Link].SDK_INT >= Build.VERSION_CODES.M) {


if ([Link](this,
[Link].ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
buildGoogleApiClient();
[Link](true);
}
} else {
buildGoogleApiClient();
[Link](true);
}

protected synchronized void buildGoogleApiClient() {


mGoogleApiClient = new [Link](this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi([Link]).build();
[Link]();
}

@Override
public void onConnected(Bundle bundle) {

mLocationRequest = new LocationRequest();


[Link](1000);
[Link](1000);

[Link](LocationRequest.PRIORITY_BALANCED_POW
ER_ACCURACY);
if ([Link](this,
V2V EdTech LLP | MAD VIMP CODES (CO/IT) 22617
[Link].ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
[Link](mGoog
leApiClient,
mLocationRequest, this);
}

@Override
public void onConnectionSuspended(int i) {

@Override
public void onLocationChanged(Location location) {

mLastLocation = location;
if (mCurrLocationMarker != null) {
[Link]();
}
// Place current location marker
LatLng latLng = new LatLng([Link](),
[Link]());
MarkerOptions markerOptions = new MarkerOptions();
[Link](latLng);
[Link]("Current Position");
[Link]([Link](BitmapDes
criptorFactory.HUE_GREEN));
mCurrLocationMarker = [Link](markerOptions);

// move map camera


[Link]([Link](latLng));
[Link]([Link](11));

// stop location updates


if (mGoogleApiClient != null) {
[Link](mGoogl
eApiClient,
this);
}
}

@Override
V2V EdTech LLP | MAD VIMP CODES (CO/IT) 22617
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}

7. Develop a program for Bluetooth Connectivity

<uses-permission android:name="[Link]" />


<uses-permission android:name="[Link].BLUETOOTH_ADMIN" />

<RelativeLayout
xmlns:androclass="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView android:text=""
android:id="@+id/out"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="49dp"
android:text="TURN_ON" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="27dp"
android:text="DISCOVERABLE" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:layout_marginTop="28dp"
android:text="TURN_OFF" />
V2V EdTech LLP | MAD VIMP CODES (CO/IT) 22617

</RelativeLayout>

package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends Activity {


private static final int REQUEST_ENABLE_BT = 0;
private static final int REQUEST_DISCOVERABLE_BT = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
final TextView out=(TextView)findViewById([Link]);
final Button button1 = (Button) findViewById([Link].button1);
final Button button2 = (Button) findViewById([Link].button2);
final Button button3 = (Button) findViewById([Link].button3);
final BluetoothAdapter mBluetoothAdapter =
[Link]();
if (mBluetoothAdapter == null) {
[Link]("device not supported");
}
[Link](new [Link]() {
public void onClick(View v) {
if (![Link]()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
});
[Link](new [Link]() {
@Override
public void onClick(View arg0) {
if (![Link]()) {
V2V EdTech LLP | MAD VIMP CODES (CO/IT) 22617
//[Link]("MAKING YOUR DEVICE DISCOVERABLE");
[Link](getApplicationContext(), "MAKING YOUR DEVICE DISCOVERA
BLE",
Toast.LENGTH_LONG);
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVER
ABLE);
startActivityForResult(enableBtIntent, REQUEST_DISCOVERABLE_BT);

}
}
});
[Link](new [Link]() {
@Override
public void onClick(View arg0) {
[Link]();
//[Link]("TURN_OFF BLUETOOTH");
[Link](getApplicationContext(), "TURNING_OFF BLUETOOTH", [Link]
H_LONG);
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate([Link].activity_main, menu);
return true;
}

8. Develop a program to implement Implicit and Explicit Intent.

package [Link];

import [Link];
import [Link];
import [Link];
import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {

@Override
V2V EdTech LLP | MAD VIMP CODES (CO/IT) 22617
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

Button explicitButton = findViewById([Link]);


[Link](new [Link]() {
@Override
public void onClick(View v) {
// Explicit Intent
Intent intent = new Intent([Link],
[Link]);
startActivity(intent);
}
});

Button implicitButton = findViewById([Link]);


[Link](new [Link]() {
@Override
public void onClick(View v) {
// Implicit Intent
Intent intent = new Intent(Intent.ACTION_VIEW);
[Link]([Link]("[Link]
om"));
startActivity(intent);
}
});
}
}

9. Develop a program to implement Sensors.

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {


private SensorManager mgr;
private TextView txtList;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
V2V EdTech LLP | MAD VIMP CODES (CO/IT) 22617
mgr = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
txtList = (TextView)findViewById([Link]);
List<Sensor> sensorList = [Link](Sensor.TYPE_ALL);
StringBuilder strBuilder = new StringBuilder();
for(Sensor s: sensorList){
[Link]([Link]()+"\n");
}
[Link]([Link]);
[Link](strBuilder);
}
}

10. Develop a program to capture Image with Camera and display it.

package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {


Button b1;
ImageView imageView;
int CAMERA_REQUEST = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
b1 = findViewById([Link]);
imageView = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CAMERA_REQUEST);
}
});
}

@Override
V2V EdTech LLP | MAD VIMP CODES (CO/IT) 22617
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
[Link](requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST) {
Bitmap image = (Bitmap) [Link]().get("data");
[Link](image);
}
}
}

11. Develop a program to implement Service.

import [Link];
import [Link];
import [Link];

public class MyService extends Service {

@Override
public void onCreate() {
[Link]();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

Runnable runnable = new Runnable() {


@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
[Link](1000);
} catch (InterruptedException e) {
[Link]();
}
}
stopSelf();
}
};

Thread thread = new Thread(runnable);


[Link]();

return START_STICKY;
V2V EdTech LLP | MAD VIMP CODES (CO/IT) 22617
}

@Override
public IBinder onBind(Intent intent) {
return null;
}
}

import [Link];
import [Link];
import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
}

public void startService(View view) {


Intent serviceIntent = new Intent(this, [Link]);
startService(serviceIntent);
}
}

<service android:name=".MyService" />

12. Develop a program to implement Broadcast Receiver.


13. Develop a program to send and receive email.

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {


// define objects for edit text and button
Button button;
EditText sendto, subject, body;
V2V EdTech LLP | MAD VIMP CODES (CO/IT) 22617

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// Getting instance of edittext and button
sendto = findViewById([Link].editText1);
subject = findViewById([Link].editText2);
body = findViewById([Link].editText3);
button = findViewById([Link]);
[Link](view -> {
String emailsend = [Link]().toString();
String emailsubject = [Link]().toString();
String emailbody = [Link]().toString();
// define Intent object with action attribute as ACTION_SEND
Intent intent = new Intent(Intent.ACTION_SEND);
// add three fields to intent using putExtra function
[Link](Intent.EXTRA_EMAIL, new String[]{emailsend});
[Link](Intent.EXTRA_SUBJECT, emailsubject);
[Link](Intent.EXTRA_TEXT, emailbody);
// set type of intent
[Link]("message/rfc822");
// startActivity with intent with chooser as Email client using
createChooser function
startActivity([Link](intent, "Choose an Email client
:"));
});
}
}

import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {


GmailReceiver gml;
IntentFilter intf;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
gml = new GmailReceiver();
intf = new IntentFilter("[Link]");
}
V2V EdTech LLP | MAD VIMP CODES (CO/IT) 22617

@Override
protected void onResume() {
[Link]();
registerReceiver(gml, intf);
}

@Override
protected void onDestroy() {
[Link]();
unregisterReceiver(gml);
}
}

package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class GmailReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
[Link](context, "Email Received", Toast.LENGTH_LONG).show();

14. Develop a program to implement Animation.

<translate
xmlns:android="[Link]
android:duration="1000"
android:fromXDelta="-100%"
android:toXDelta="100%"
android:repeatCount="infinite"
android:interpolator="@android:anim/linear_interpolator"/>

import [Link];
import [Link];
V2V EdTech LLP | MAD VIMP CODES (CO/IT) 22617
import [Link];
import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

// Get the TextView


TextView animatedTextView = findViewById([Link]);

// Load the animation


Animation slideRightAnimation =
[Link](getApplicationContext(), [Link].slide_right);

// Start the animation


[Link](slideRightAnimation);
}
}
15.

16. Develop a program for implementing Asynchronous Tasks.

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private Button button;
private EditText time;
private TextView finalResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
time = (EditText) findViewById([Link].in_time);
button = (Button) findViewById([Link].btn_run);
finalResult = (TextView) findViewById([Link].tv_result);
[Link](new [Link]() {
@Override
V2V EdTech LLP | MAD VIMP CODES (CO/IT) 22617
public void onClick(View v) {
AsyncTaskRunner runner = new AsyncTaskRunner();
String sleepTime = [Link]().toString();
[Link](sleepTime);
}
});
}
privateclass AsyncTaskRunner extends AsyncTask<String, String, String>{
private String resp;
ProgressDialog progressDialog;
@Override
protected String doInBackground(String... params) {
publishProgress("Sleeping..."); // Calls onProgressUpdate()
try {
int time = [Link](params[0])*1000;
[Link](time);
resp = "Slept for " + params[0] + " seconds";
} catch (InterruptedException e) {
[Link]();
resp = [Link]();
} catch (Exception e) {
[Link]();
resp = [Link]();
}
return resp;
}
@Override
protected void onPostExecute(String result) {
// execution of result of Long time consuming operation
[Link]();
[Link](result);
}
@Override
protected void onPreExecute() {
progressDialog = [Link]([Link],
"ProgressDialog",
"Wait for "+[Link]().toString()+ " seconds");
}
@Override
protected void onProgressUpdate(String... text) {
[Link](text[0]);
}
}
}

You might also like