/ / Register the static broadcast
Lane in AndroidManifest.xml
/ / Define broadcast receiver class
public class TestBroadcast extends BroadcastReceiver {
public static final String RECEIVER = "com.lpj.test";
@ Override
public void onReceive (Context context, Intent intent) {
if (intent.getAction (). equals (RECEIVER)) {
Log.i ("BroadcastTest", "Broadcast Start!");
Toast.makeText (context, "Broadcast Start!",
. Toast.LENGTH_LONG) show ();
}
}
}
/ / Send broadcast
in the main interfacepublic class replyer extends Activity {
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_replyer);
this.findViewById (R.id.button). setOnClickListener (new OnClickListener () {
@ Override
public void onClick (View v) {
sendBroadcast (new Intent ("com.lpj.test"));
}
});
}
}
The results no response (neither pop Toast, logcat where did things), 4.2 system, fax machine and simulators are tested<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
sendBroadcast (new Intent ("com.lpj.test"));
Wrong.
Write
sendBroadcast (new Intent (this, TestBroadcast.class));
Reply:
Intent intent = new Intent ();
intent.setAction ("com.lpj.test");
sendBroadcast (intent);
Reply:
Intent intent = new Intent ();
intent.setAction ("com.lpj.test");
sendBroadcast (intent);
Like upstairs to say so that you can, if you are not in the activity inside the receiver you put into context getApplication () getContext ().;
Reply:
sendBroadcast (new Intent (replyer.this, "com.lpj.test"));
Or
sendBroadcast (new Intent (replyer.this.getApplicationContext (), "com.lpj.test"));
Upstairs that are also OK!
Reply:
Sorry ha, this is wrong my carelessness, I put
No comments:
Post a Comment