Skip to content

Instantly share code, notes, and snippets.

@girish3
Last active November 11, 2018 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save girish3/ad88c4940b2a4e56cf0852a72dff9afe to your computer and use it in GitHub Desktop.
Save girish3/ad88c4940b2a4e56cf0852a72dff9afe to your computer and use it in GitHub Desktop.
[Intent] Intent snippets to start Activity, Service or sending broadcast. #android_snippet #android
// starting an activity from another activity
// this is a Activity context
Intent intent = new Intent(this, AnotherActivity.java);
intent.putExtra(Intent.EXTRA_TEXT, "some text");
intent.putExtra("id", 4);
startActivity(intent);
// Receiving intent in AnotherActivity
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
// get data via the key
String value1 = extras.getString(Intent.EXTRA_TEXT);
if (value1 != null) {
// do something with the data
}
// Starting a service
Intent intent = new Intent(this, HelloService.class);
startService(intent);
// Sending a broadcast
Intent intent = new Intent();
intent.setAction("com.example.broadcast.MY_NOTIFICATION");
intent.putExtra("data", "Notice me senpai!");
sendBroadcast(intent);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment