您的位置:首页 > 其它

静态和动态注册广播接收者 72集

2015-04-26 18:15 309 查看
转载请注明出处:/article/3711651.html

笔记来源:尚学堂_尧玮_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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: