The broadcast intent mechanism can be used to send an intent to any broadcastreceiver interested in this intent.
Use new intent (action_1) to create an intent whose action is action_1.
Broadcast the intent through sendbroadcast (intent. The Code is as follows:
Activitymain code:
1: package com.eoeandroid.broadcastReceiver;
2: import android.app.NotificationManager;
3: import android.content.BroadcastReceiver;
4: import android.content.Context;
5: import android.content.Intent;
6: public class EoeAndroidReceiver2 extends BroadcastReceiver {
7: Context context;
8: @Override
9: public void onReceive(Context context, Intent intent) {
10: // TODO Auto-generated method stub
11: this.context = context;
12: DeleteNotification();
13: }
14: private void DeleteNotification() {
15: NotificationManager notificationManager = (NotificationManager) context
16: .getSystemService(android.content.Context.NOTIFICATION_SERVICE);
17: notificationManager.cancel(EoeAndroidReceiver1.NOTIFICATION_ID);
18:
19: }
20: }
After you click the first item of menu, the program runs eoeandroidreceiver1 and displays a notification in the status bar through the onrecievie method. Shownotification () is used to display a notification. The Code is as follows:
Eoeandroidreceiver1 code
1: package com.eoeandroid.broadcastReceiver;
2: import android.app.Notification;
3: import android.app.NotificationManager;
4: import android.app.PendingIntent;
5: import android.content.BroadcastReceiver;
6: import android.content.Context;
7: import android.content.Intent;
8: public class EoeAndroidReceiver1 extends BroadcastReceiver {
9: Context context;
10: public static int NOTIFICATION_ID = 21321;
11: @Override
12: public void onReceive(Context context, Intent intent) {
13: this.context = context;
14: showNotification();
15: }
16: private void showNotification() {
17: NotificationManager notificationManager = (NotificationManager) context
18: .getSystemService(android.content.Context.NOTIFICATION_SERVICE);
19: Notification notification = new Notification(R.drawable.icon,
20: "In eoeandroidreceiver1", system. currenttimemillis ());
21: PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
22: new Intent(context, ActivityMain.class), 0);
23: notification. setlatesteventinfo (context, "In eoeandroidreceiver1", null,
24: contentIntent);
25: notificationManager.notify(NOTIFICATION_ID, notification);
26: }
27: }
Click the second button and the program starts broadcasting. the broadcast is intercepted by eoeandroidreceiver2 and then the onreceive method in eoeandroidreceiver2 is executed. The deletenotification () method deletes the generated notification from the status bar. The Code is as follows:
Eoeandroidreceiver2 code
1: package com.eoeandroid.broadcastReceiver;
2: import android.app.NotificationManager;
3: import android.content.BroadcastReceiver;
4: import android.content.Context;
5: import android.content.Intent;
6: public class EoeAndroidReceiver2 extends BroadcastReceiver {
7: Context context;
8: @Override
9: public void onReceive(Context context, Intent intent) {
10: // TODO Auto-generated method stub
11: this.context = context;
12: DeleteNotification();
13: }
14: private void DeleteNotification() {
15: NotificationManager notificationManager = (NotificationManager) context
16: .getSystemService(android.content.Context.NOTIFICATION_SERVICE);
17: notificationManager.cancel(EoeAndroidReceiver1.NOTIFICATION_ID);
18: }
19: }
Android has come to an end after a week of study (strictly speaking, the time for learning and writing papers is one week, and the time for summing up and writing blogs is much longer than one week) I can only have a general understanding of Android. I still have many questions about how to study android in depth due to time. I have not even been touched by many questions. I will try again later.