BroadCast 以及 Notification的应用

本文详细介绍了Android平台上的广播机制,包括如何定义BroadcastReceiver类接收广播、如何通过Intent发送广播,以及如何配置AndroidManifest.xml文件使应用能够接收特定类型的广播。此外还讲解了如何使用PendingIntent配合Notification实现点击通知跳转到应用的功能。

<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)); } }

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值