Requirement: Now there is application A and application B, I need to start an activity in the B application in a application
Implementation: A application in the activity to send broadcasts, the key code is as follows:
String broadcastintent = "com.example.android.notepad.NotesList";//Customize it Yourself
Intent Intent = new Intent (broadcastintent);
This.sendbroadcast (Intent);
b application needs a broadcastreceiver to receive the broadcast, named Testreceiver inherit Broadcastreceiver override OnReceive method to start an activity, the key code is as follows:
if (Intent.getaction (). Equals ("Com.example.android.notepad.NotesList")) {
Intent notelist = new Intent (context,noteslist.class);
Notelist.addflags (Intent.flag_activity_new_task);
Context.startactivity (notelist);
}
To this code is done, of course, in Androidmanifest.xml to register the Testreceiver, the code is as follows:
<receiver android:name= "Testreceiver" >
<intent-filter>
<action android:name= "Com.example.android.notepad.NotesList"/>
</intent-filter>
</receiver>
This completes the activation of another application via broadcast activity.
Note: There is a startactivity method in context where activity inherits from the context and overloads the StartActivity method. If you use the activity's StartActivity method, there is no limit, and if you use the StartActivity method of the context, you need to open a new task, the solution is to add a flag, This is the notelist.addflags (intent.flag_activity_new_task); If you do not add this sentence, you will be quoted Android.util.AndroidRuntimeException:Calling StartActivity () from outside of a activity,caused by: Android.util.AndroidRuntimeException:Calling startactivity () from outside of an Activity context requires the Flag_activ Ity_new_task flag. Is this really what do you want?
Android launches another app's activity by broadcasting