`
wen742538485
  • 浏览: 229322 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

android 广播机制

阅读更多
在android下,要想接受广播信息,那么这个广播接收器就得我们自己来实现了,我们可以继承BroadcastReceiver,就可以有一个广播接受器了。有个接受器还不够,我们还得重写BroadcastReceiver里面的onReceiver方法,当来广播的时候我们要干什么,这就要我们自己来实现,不过我们可以搞一个信息防火墙。具体的代码:

public class SmsBroadCastReceiver extends BroadcastReceiver 
{

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Bundle bundle = intent.getExtras();
        Object[] object = (Object[])bundle.get("pdus");
        SmsMessage sms[]=new SmsMessage[object.length];
        for(int i=0;i<object.length sms smsmessage.createfrompdu toast.maketext toast.length_short abortbroadcast smsbroadcastreceiver="new" intentfilter intentfilter.addaction broadcastreceiveractivity.this.registerreceiver version="1.0" encoding="utf-8"><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="spl.broadCastReceiver" android:versioncode="1" android:versionname="1.0"><application android:icon="@drawable/icon" android:label="@string/app_name"><activity android:name=".BroadCastReceiverActivity" android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN"></action><category android:name="android.intent.category.LAUNCHER"></category></intent-filter></activity><!--广播注册--><receiver android:name=".SmsBroadCastReceiver"><intent-filter android:priority="20"><action android:name="android.provider.Telephony.SMS_RECEIVED"></action></intent-filter></receiver></application><uses-sdk android:minsdkversion="7"></uses-sdk><!-- 权限申请 --><uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission></manifest> 


两种注册类型的区别是:

1)第一种不是常驻型广播,也就是说广播跟随程序的生命周期。

2)第二种是常驻型,也就是说当应用程序关闭后,如果有信息广播来,程序也会被系统调用自动运行。

</object.length>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics