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

Android_Unit5_Unit6_Code_Examples

The document outlines important Android methods and classes with code examples, including explicit and implicit intents, activity lifecycle logging, broadcast receivers, and services. It also covers functionalities like audio playback, text-to-speech, camera capture, SQLite database operations, and fragment transactions. Each example includes a brief description of the expected output when executed.

Uploaded by

PALLAVI KAKADE
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)
3 views

Android_Unit5_Unit6_Code_Examples

The document outlines important Android methods and classes with code examples, including explicit and implicit intents, activity lifecycle logging, broadcast receivers, and services. It also covers functionalities like audio playback, text-to-speech, camera capture, SQLite database operations, and fragment transactions. Each example includes a brief description of the expected output when executed.

Uploaded by

PALLAVI KAKADE
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
You are on page 1/ 2

Important Android Methods & Classes with Code Examples (Unit 5 & 6)

1. Explicit Intent
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
Output: Opens SecondActivity screen.

2. Implicit Intent
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.google.com"));
startActivity(intent);
Output: Opens Google in the browser.

3. Activity Lifecycle Logging


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("ActivityState", "onCreate Called");
}
Output: Logcat shows 'onCreate Called'.

4. BroadcastReceiver
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Broadcast Received", Toast.LENGTH_SHORT).show();
}
}
Intent intent = new Intent("MY_CUSTOM_BROADCAST");
sendBroadcast(intent);
Output: Toast saying 'Broadcast Received'.

5. Start Service
Intent i = new Intent(this, MyService.class);
startService(i);

public class MyService extends Service {


public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("ServiceDemo", "Service Started");
return START_STICKY;
}
}
Output: Logcat shows 'Service Started'.

6. Play Audio
MediaPlayer mp = MediaPlayer.create(this, R.raw.soundfile);
mp.start();
Output: Plays audio file.

7. Text to Speech
TextToSpeech tts = new TextToSpeech(this, status -> {
if (status == TextToSpeech.SUCCESS) {
tts.speak("Hello World", TextToSpeech.QUEUE_FLUSH, null, null);
}
});
Output: Speaks 'Hello World'.

8. Camera Capture
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
Output: Opens camera and captures image.

9. SQLite Insert & Query


SQLiteDatabase db = openOrCreateDatabase("StudentDB", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS student(name VARCHAR);");
db.execSQL("INSERT INTO student VALUES('Alex');");

Cursor c = db.rawQuery("SELECT * FROM student", null);


if (c.moveToFirst()) {
Toast.makeText(this, c.getString(0), Toast.LENGTH_SHORT).show(); // Shows "Alex"
}
Output: Toast with 'Alex'.

10. Fragment Transaction


FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.frameLayout, new MyFragment());
ft.commit();
Output: Replaces layout with MyFragment.

You might also like