android中的广播
来源:互联网 发布:itunes发生网络错误 编辑:程序博客网 时间:2024/06/06 16:00
android中的广播有有序广播和普通广播两种网上有很多介绍的,这里只是简单介绍一下普通广播的实现
首先在主页面放置一个按钮然后再在按钮的监听之后在监听里实现广播因为只是实现一个简单的普通广播所以我注释了有序广播的实现,没有写有序广播的接收
public class MainActivity extends Activity {private Button btn1;private Button btn2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn1 = (Button) findViewById(R.id.button1);btn2 = (Button) findViewById(R.id.button2);OnClickListener litener = new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.button1:Intent intent = new Intent();intent.setAction("android.intent.action.CaseReceiver");//设置要接收的classintent.putExtra("msg", "这是一条普通的receiver");sendBroadcast(intent);break;//case R.id.button2://Intent intent1 = new Intent();//intent1.setAction("android.intent.action.CaseReceiver");//intent1.putExtra("msg", "这是一条有序的receiver");//sendOrderedBroadcast(intent1, null);//break;default:break;}}};btn1.setOnClickListener(litener);btn2.setOnClickListener(litener);}}普通广播的接收实现
public class CaseReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubString str = intent.getStringExtra("msg");Toast.makeText(context, "-->" + str, Toast.LENGTH_LONG).show();Log.i("tag", "-------------->接收");}}其中值得我们注意的是他们的注册在<application>的标签里添加服务
<receiver android:name="cm.example.helloserverd.CaseReceiver" > <intent-filter android:priority="200" > <action android:name="android.intent.action.CaseReceiver" /> <category android:name="android.intent.category.DEFAULT" > </category> </intent-filter> </receiver> 其中第一个receiver标签里的的android:name是广播接收者的class的类名而第二个action里的android:name是一个相当于映射的别名,在发送页面里的setaction所写的就是第二个action的名字,
值得注意的是第一个action是类的路径,而第二个action则是android.intent.action.xxxxxx
0 0
- android中的广播
- Android 中的广播机制
- android中的系统广播
- Android中的广播机制
- android中的广播机制
- android中的广播
- android中的广播机制
- Android中的广播监听
- Android中的广播
- android中的系统广播
- Android中的广播
- Android中的广播
- android中的自定义广播
- Android中的广播机制
- android中的广播机制
- android中的广播接收器
- Android中的广播机制
- android中的广播
- C++中int型与string型互相转换
- UVA-Knights of the Round Table La3523
- 数据重复导致创建唯一索引失败,删除重复数据的SQL语句
- CSU-ACM2016暑假集训比赛5
- getset_descriptor
- android中的广播
- spring配置文件详解(applicationContext.xml)(2)
- Android面试题集锦(四)
- UNIX环境编程基本函数——标准IO
- Double precision
- 科大讯飞语音合成android studio 21001错误
- 151. Reverse Words in a String 翻转字符串中的单词
- upnp 端口映射
- ThinkPHP怪现象:数据表新增字段后开发模式可更新运行模式无法更新