<application android:icon="@drawable/icon" android:label="broadcast"> <activity android:name="com.example.app.BroadcastSenderDemo"> <intent-filter> <action android:name="android.intent.action.MAIN"></action> <category android:name="android.intent.category.LAUNCHER"></category> </intent-filter> </activity> <!-- android:name是包名+类 --> <receiver android:name="com.example.demo.BroadcastReceiverDemo"> <intent-filter> <action android:name="com.receiver.action.TestAction"/> </intent-filter> </receiver> </application>
如果要给应用程序添加 BroadCast 则需要配置相关规则
<receiver android:name="com.example.demo.BroadcastReceiverDemo"> <intent-filter> <action android:name="com.receiver.action.TestAction"/> </intent-filter> </receiver>
其中接收 receiver 的name是一个继承了BroadcastReceiver的类
package com.android.demo; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class BroadcastReceiverDemo extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { //创建通知管理器 NotificationManager nm = (NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE); //创建通知 Notification notification = new Notification(com.example.R.drawable.icon,"hello",System.currentTimeMillis()); //重新定义该方法中的intent参数对象 //该Intent使得当用户点击该通知后发出这个Intent intent = new Intent(context,BroadcastSenderDemo.class); //请注意,如果要以该Intent启动一个Activity,一定要设置 Intent.FLAG_ACTIVITY_NEW_TASK 标记 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // PendingIntent.FLAG_UPDATE_CURRENT表示:如果描述的PendingIntent已存在,则改变已存在的PendingIntent的Extra数据为新的PendingIntent的Extra数据 PendingIntent contentIntent = PendingIntent.getActivity(context,0,intent, PendingIntent.FLAG_UPDATE_CURRENT); notification.setLatestEventInfo(context, "xxx", "helloworld", contentIntent); //FLAG_ONGOING_EVENT使得通知出现在"正在运行"栏目中 notification.flags = Notification.FLAG_ONGOING_EVENT; nm.notify(0x0, notification); } }
其中如果想要实现 单击状态栏里的通知 跳转到程序界面
则需要配置 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
传播 broadcast 的源码:
package com.android.demo; import android.app.Activity; import android.app.NotificationManager; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.LinearLayout; /** * 发送广播 * @author wangtao */ public class BroadcastSenderDemo extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* 在AndroidManifest中需要配置 <receiver android:name="com.example.demo.BroadcastReceiverDemo"> <intent-filter> <action android:name="com.receiver.action.TestAction"/> </intent-filter> </receiver> */ //发送广播 final Intent intent = new Intent("com.receiver.action.TestAction"); //布局 LinearLayout lineLayout = new LinearLayout(this); //组件 Button button = new Button(this); button.setText("发送广播,通知接受响应"); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //发送广播 sendBroadcast(intent); } }); lineLayout.addView(button); button = new Button(this); button.setText("退出"); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { BroadcastSenderDemo.this.finish(); // System.exit(0); NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //消除通知 nm.cancelAll(); } }); lineLayout.addView(button); this.addContentView(lineLayout, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); } }