静态和动态注册广播接收者 72集
2015-04-26 18:15
309 查看
转载请注明出处:/article/3711651.html
笔记来源:尚学堂_尧玮_072集-静态和动态注册广播接收者
两种广播发送的方式,清单中注册广播接收者是永久的,代码注册是临时的
笔记来源:尚学堂_尧玮_072集-静态和动态注册广播接收者
两种广播发送的方式,清单中注册广播接收者是永久的,代码注册是临时的
public class MainActivity extends Activity { MyReceive2 myReceive2; Button btnSendBroadcast_1; Button btnSendBroadcast_2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnSendBroadcast_1 = (Button) findViewById(R.id.btnSendBroadcast_1); btnSendBroadcast_2 = (Button) findViewById(R.id.btnSendBroadcast_2); //封装成一个方法,该代码来注册清单文件 registerReceiver(); //第一种方法发送广播(清单里注册的) btnSendBroadcast_1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent("com.example.broadcast.MyReceive1"); intent.putExtra("key", "Broadcast_1发送的数据"); sendBroadcast(intent); } }); //第二种方法发送广播(内部类,在代码里注册) btnSendBroadcast_2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent("com.example.broadcast.MyReceive2"); intent.putExtra("key", "Broadcast_2发送的数据"); sendBroadcast(intent); } }); } //代码注册BroadcastReceiver的方法 private void registerReceiver() { myReceive2 = new MyReceive2(); IntentFilter filter = new IntentFilter(); filter.addAction("com.example.broadcast.MyReceive2"); registerReceiver(myReceive2, filter); } class MyReceive2 extends BroadcastReceiver{ //一种是在清单里面注册,另一种用java代码注册即如下内部类 @Override public void onReceive(Context context, Intent intent) { Log.i("main", "MyReceive2.onReceive() : " + intent.getStringExtra("key")); } } @Override protected void onDestroy() { super.onDestroy(); //当退出时,取消广播接收者的注册 unregisterReceiver(myReceive2); } }
public class MyReceive1 extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //收到广播时调用这个方法 Log.i("main", "MyReceive1.onReceive: " + intent.getStringExtra("key")); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.broadcast" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.broadcast.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="com.example.broadcast.MyReceive1"> <intent-filter > <action android:name="com.example.broadcast.MyReceive1"/> <!-- 只要发送广播的字符串是这个name,就可以收到广播 --> </intent-filter> </receiver> </application> </manifest>
相关文章推荐
- 3.9 采用代码的方式动态注册广播接收者
- Android的广播Receiver动态注册和静态注册
- 静态广播接收者、动态广播接收者对比(文末附面试题)
- Android静态注册广播和动态注册广播的区别
- 静态/动态注册广播的区别
- BroadCastReceiver(广播接收者)动态注册简单例子
- Android广播之注册广播(包括静态广播和动态广播的注册)源码分析
- android 注册静态广播接收器VS注册动态广播接收器
- Android静态/动态注册广播的区别
- Android静态注册和动态注册广播的区别
- BroadCastReceiver(广播接收者的静态注册)简单使用例子
- Android中的静态系统广播与动态系统广播及其相应的注册与接收
- 旺仔:发送一般广播,静态注册和动态注册
- Android的广播Receiver动态注册和静态注册示例
- 一般广播,静态注册,动态注册
- Android中广播的使用(动态、静态注册的区别,有序无序广播的使用)
- 静态/动态注册广播的区别
- android_广播者_只能用代码注册(动态注册)的广播接收者_监听屏幕的状态
- 静态注册广播接收者
- 静态/动态注册广播的区别,静态动态广播区别