首页 > 学院 > 开发设计 > 正文

有序广播的知识运用

2019-11-10 18:48:04
字体:
来源:转载
供稿:网友

版权声明:本文为博主原创文章,未经博主允许不得转载。

之前练习的是无序广播,这次实现有序广播。

具体代码实现如下(发送者):

public class MainActivity extends AppCompatActivity {

PRivate EditText et_main_content; private Intent intent;

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_main_content = (EditText) findViewById(R.id.et_main_content);

intent = new Intent();//设置广播的名字(设置Action)intent.setAction("com.example.android20_sender.1008");

}

public void send(View view){ String content=et_main_content.getText().toString();

//携带数据intent.putExtra("data",content);//发送广播(无序广播)//sendBroadcast(intent);//发送黏性广播//sendStickyBroadcast(intent);//发送广播(有序)sendOrderedBroadcast(intent,null);

} }

这里写图片描述

静态广播接受者 public class MyReceived01 extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(“com.example.android20_sender.1008”.equals(intent.getAction())){ String data=intent.getStringExtra(“data”); Log.i(“test”,”01号接受者收到广播了:”+data);

//改变内容 Bundle bundle=new Bundle(); bundle.putString("content",data+"吗?,骗你的"); setResultExtras(bundle); /*if("中奖了".equals(data)){ //取消广播传播 abortBroadcast(); }*/ }}

}

动态广播注册 public class MainActivity extends AppCompatActivity {

private MyReceived02 myReceived02;private IntentFilter intentFilter;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myReceived02 = new MyReceived02(); intentFilter = new IntentFilter(); intentFilter.addAction("com.example.android20_sender.1008"); //Integer.MAX_VALUE intentFilter.setPriority(998);}@Overrideprotected void onResume() { super.onResume(); //动态注册广播 registerReceiver(myReceived02,intentFilter);}@Overrideprotected void onDestroy() { super.onDestroy(); //注销动态广播 unregisterReceiver(myReceived02);}

}

动态广播接受者 public class MyReceived02 extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(“com.example.android20_sender.1008”.equals(intent.getAction())){ // String data=intent.getStringExtra(“data”); Bundle bundle=getResultExtras(true); String content=bundle.getString(“content”); Log.i(“test”,”02号接受者收到广播了:”+content); } } }

广播优先级(priority)-1000~1000

系统广播(短信SMS) 有序

这里写图片描述 这里写图片描述

自定义短信接受(清单文件配置) 这里写图片描述

自定义短信接受代码实现 public class MySMSReceived extends BroadcastReceiver {

private String body;@Overridepublic void onReceive(Context context, Intent intent) { if("android.provider.Telephony.SMS_RECEIVED".equals(intent.getAction())){ Log.i("test","有短信进来了"); //获取短信的内容 Bundle bundle=intent.getExtras(); Object[] objects= (Object[]) bundle.get("pdus"); SmsMessage[] smsMessages=new SmsMessage[objects.length]; for (int i = 0; i < objects.length; i++) { smsMessages[i]=SmsMessage.createFromPdu((byte[])objects[i]); } for (SmsMessage message : smsMessages) { String address = message.getDisplayOriginatingAddress(); body = message.getDisplayMessageBody(); Log.i("test","发送人:"+ address); Log.i("test","内容:"+ body); } if(body.contains("中奖了")){ //拦截 Log.i("test","拦截..."); abortBroadcast(); } }}

}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表