- public class BroadcastActivity extends Activity {
- private Button send = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- send = (Button)findViewById(R.id.sned);
- send.setOnClickListener(new BroadcastListener());
- }
- class BroadcastListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- System.out.println("------------");
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_EDIT);
- BroadcastActivity.this.sendBroadcast(intent);
- }
- }
- }
- public class TestReceiver extends BroadcastReceiver {
- public TestReceiver() {
- System.out.println("TestReceiver create......");
- }
- @Override
- public void onReceive(Context arg0, Intent arg1) {
- System.out.println("receive......");
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <Button
- android:id="@+id/sned"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/send"
- />
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, TestReceiver!</string>
- <string name="app_name">broadcast</string>
- <string name="send">发送</string>
- </resources>
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.gem.activity"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".BroadcastActivity"
- 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 -->
- <receiver android:name=".TestReceiver">
- <intent-filter>
- <action android:name="android.intent.action.EDIT"></action>
- </intent-filter>
- </receiver>
- </application>
- <uses-sdk android:minSdkVersion="8" />
- </manifest>
当用户点击发送的时候,程序会调用onReceive()方法 |
- public class BroadcastActivity extends Activity {
- private Button send;
- private Button registerReceiver;
- private Button unregisterReceiver;
- private MyReceiver myReceiver;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- send = (Button)findViewById(R.id.send);
- send.setOnClickListener(new SendButtontListener());
- registerReceiver = (Button)findViewById(R.id.registerReceiver);
- registerReceiver.setOnClickListener(new RegisterReceiverButtonListener());
- unregisterReceiver = (Button)findViewById(R.id.unregisterReceiver);
- unregisterReceiver.setOnClickListener(new UnregisterReceiverButtonListener());
- }
- class SendButtontListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- Intent intent = new Intent();
- intent.setAction("Intent.ACTION_EDIT");
- BroadcastActivity.this.sendBroadcast(intent);
- System.out.println("send-----");
- }
- }
- class RegisterReceiverButtonListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- myReceiver = new MyReceiver();
- IntentFilter filter = new IntentFilter();
- filter.addAction("Intent.ACTION_EDIT");
- //动态注册BroadcastReceiver
- registerReceiver(myReceiver, filter);
- }
- }
- class UnregisterReceiverButtonListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- //注销BroadcastReceiver
- unregisterReceiver(myReceiver);
- System.out.println("close-----");
- }
- }
- }
- public class MyReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- System.out.println("onReceive......");
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <Button
- android:id="@+id/send"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/send"
- />
- <Button
- android:id="@+id/registerReceiver"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/registerReceiver"
- />
- <Button
- android:id="@+id/unregisterReceiver"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/unregisterReceiver"
- />
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, TestReceiver!</string>
- <string name="app_name">broadcast</string>
- <string name="send">发送广播</string>
- <string name="registerReceiver">注册广播接收器</string>
- <string name="unregisterReceiver">注销广播接收器</string>
- </resources>
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.gem.activity"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".BroadcastActivity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- <uses-sdk android:minSdkVersion="8" />
- </manifest>
当用户点击发送的时候,程序没有注册BroadcastReceiver,当用户点击注册广播接收器之后在点击发送会调用MyReceiver中的onReceive()方法,当用户点击注销广播接收器之后程序执行unregisterReceiver()方法 |