Last active
November 11, 2018 05:07
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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