Android:BroadcastReceiver的练习使用

 1 package com.example.broadcastdemo1;

 2 

 3 import android.os.Bundle;

 4 import android.app.Activity;

 5 import android.content.Intent;

 6 import android.content.IntentFilter;

 7 import android.view.Menu;

 8 import android.view.View;

 9 

10 public class MainActivity extends Activity {

11     private MyBroadcastReceiver2 mbr2;

12     private MyBroadcastReceiver3 mbr3;

13     @Override

14     protected void onCreate(Bundle savedInstanceState) {

15         super.onCreate(savedInstanceState);

16         setContentView(R.layout.activity_main);

17         IntentFilter intentFilter = new IntentFilter("BC_One");

18         mbr2 = new MyBroadcastReceiver2();

19         registerReceiver(mbr2, intentFilter);    

20     }

21     

22     public void doClick(View v) {

23         switch (v.getId()) {

24         case R.id.normal_broadcast:

25             Intent intent = new Intent();

26             intent.putExtra("msg", "这是一条普通广播");

27             intent.setAction("BC_One");

28             sendBroadcast(intent);

29             break;

30         case R.id.ordered_broadcast:

31             Intent intent2 = new Intent();

32             intent2.putExtra("msg", "这是一条有序广播");

33             intent2.setAction("BC_One");

34             sendOrderedBroadcast(intent2, null);

35             break;

36         case R.id.sticky_broadcast:

37             Intent intent3 = new Intent();

38             intent3.putExtra("msg", "这是一条有序广播");

39             intent3.setAction("BC_Three");

40             sendStickyBroadcast(intent3);

41             IntentFilter intentFilter = new IntentFilter("BC_Three");

42             mbr3 = new MyBroadcastReceiver3();

43             registerReceiver(mbr3, intentFilter);

44             break;

45         }

46     }

47     

48 //    @Override

49 //    protected void onDestroy() {

50 //        // TODO Auto-generated method stub

51 //        super.onDestroy();

52 //        unregisterReceiver(mbr2);

53 //        unregisterReceiver(mbr3);

54 //    }

55 }
 1 package com.example.broadcastdemo1;

 2 

 3 import android.content.BroadcastReceiver;

 4 import android.content.Context;

 5 import android.content.Intent;

 6 import android.os.Bundle;

 7 import android.util.Log;

 8 

 9 public class MyBroadcastReceiver2 extends BroadcastReceiver{

10 

11     @Override

12     public void onReceive(Context context, Intent intent) {

13         // TODO Auto-generated method stub

14         String str = intent.getStringExtra("msg");

15         Log.i("info","我是2!信息为:"+str );

16         Bundle bundle = new Bundle();

17         bundle.putString("test", "经过处理的数据");

18         setResultExtras(bundle);

19 //        abortBroadcast();

20     }

21 

22 }
 1 package com.example.broadcastdemo1;

 2 

 3 import android.content.BroadcastReceiver;

 4 import android.content.Context;

 5 import android.content.Intent;

 6 import android.os.Bundle;

 7 import android.util.Log;

 8 

 9 public class MyBroadcastReceiver extends BroadcastReceiver{

10 

11     @Override

12     public void onReceive(Context context, Intent intent) {

13         // TODO Auto-generated method stub

14         String str = intent.getStringExtra("msg");

15         Log.i("info", "我是1!信息为:"+str);

16         Bundle bundle = getResultExtras(true);

17         String str2 = bundle.getString("test");

18         Log.i("info", "这是什么?"+str2);

19     }

20 

21 }
 1 package com.example.broadcastdemo1;

 2 

 3 import android.content.BroadcastReceiver;

 4 import android.content.Context;

 5 import android.content.Intent;

 6 import android.util.Log;

 7 

 8 public class MyBroadcastReceiver3 extends BroadcastReceiver{

 9 

10     @Override

11     public void onReceive(Context context, Intent intent) {

12         // TODO Auto-generated method stub

13         Log.i("info", "接收到了一条异步广播!");

14     }

15 

16 }
 1 <?xml version="1.0" encoding="utf-8"?>

 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"

 3     package="com.example.broadcastdemo1"

 4     android:versionCode="1"

 5     android:versionName="1.0" >

 6 

 7     <uses-sdk

 8         android:minSdkVersion="8"

 9         android:targetSdkVersion="17" />

10     <uses-permission android:name="android.permission.BROADCAST_STICKY"/>

11 

12     <application

13         android:allowBackup="true"

14         android:icon="@drawable/ic_launcher"

15         android:label="@string/app_name"

16         android:theme="@style/AppTheme" >

17         <activity

18             android:name="com.example.broadcastdemo1.MainActivity"

19             android:label="@string/app_name" >

20             <intent-filter>

21                 <action android:name="android.intent.action.MAIN" />

22 

23                 <category android:name="android.intent.category.LAUNCHER" />

24             </intent-filter>

25         </activity>

26         <receiver 

27             android:name="com.example.broadcastdemo1.MyBroadcastReceiver">

28             <intent-filter >

29                 <action android:name="BC_One"/>

30             </intent-filter>

31         </receiver>

32     </application>

33 

34 </manifest>

 

你可能感兴趣的:(Android:BroadcastReceiver的练习使用)