Broadcast-----------自定义Action , 两种方式 实现广播Broadcast
掌握Broadcast,要掌握一下三点
1:注册广播:(有两种方式)
一是动态方式使用Context.registerReceiver()方法来注册;
二是静态方式,即在AndroidManifest.xml中通过<receiver> 。
2:发送广播:
在需要广播的地方sendBroadcast(intent); 之前要给Intent的对象intent 设置Action (Action可以被系统的或自定义的)和 data。
3:接收广播:
就是在extends Broadcast的那个类中, onReceive(Context context, Intent intent)方法要做的事情。
***静态方式:
下面代码是用静态方式和自定义action实现的广播:
运行结果如下:
具体代码:
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/b" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="发送" /> <Button android:id="@+id/cancle" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="exit" /> </LinearLayout>
TestActivity.java
package m.m.m; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class TestActivity extends Activity { private Button b; private Button cancle; public static final String MY_ACTION = "mxp"; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); b = (Button) findViewById(R.id.b); cancle = (Button) findViewById(R.id.cancle); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setAction(MY_ACTION); intent.putExtra("msg", "asdsdas"); sendBroadcast(intent); } }); cancle.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="m.m.m" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".TestActivity" 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=".BroadCastActivity"> <intent-filter > <action android:name="mxp"/> </intent-filter> </receiver> </application> </manifest>
总结 :自定义Action,就是定义一个静态常量如:public static final String MY_ACTION = "mxp";但是不要和系统的重复哦!
对我而言,关键是注册,即
<receiver android:name=".BroadCastActivity"> 注册广播
<intent-filter >
<action android:name="mxp"/> 定义action
</intent-filter>
</receiver>
***动态方式:
还针对上面的例子进行说明:这回程序运行结果如下:
1)不要androidManifest.xml文件中注册 ,即删除刚才注册的部分
2) 在启动服务的地方,即发送按钮的监听器里注册,再启动服务
TestActivity.java 多了注册的代码:myReceiver = new MyReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(MY_ACTION);
TestActivity.java:
package m.m.m; import android.app.Activity; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class TestActivity extends Activity { private Button bind; private Button unbind; private Button cancle; protected MyReceiver myReceiver; public static final String MY_ACTION = "mxp"; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); bind = (Button) findViewById(R.id.bind); unbind = (Button) findViewById(R.id.unbind); cancle = (Button) findViewById(R.id.cancle); bind.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub myReceiver = new BroadCastActivity(); IntentFilter filter = new IntentFilter(); filter.addAction(MY_ACTION); TestActivity.this.registerReceiver(myReceiver, filter); System.out.println("bind"); Intent intent = new Intent(); intent.setAction(MY_ACTION); intent.putExtra("msg", "asdsdas"); sendBroadcast(intent); } }); unbind.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { TestActivity.this.unregisterReceiver(myReceiver); System.out.println("unbind"); } }); cancle.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/bind" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="绑定" /> <Button android:id="@+id/unbind" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="取消绑定" /> <Button android:id="@+id/cancle" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="exit" /> </LinearLayout>
总结:我在测试时点击绑定怎么都不显示Toast的内容,原因是我只注册了广播,没有发送广播。要想广播响应,一定要注册还要发送!