Android广播机制:当某个事件发生时,Android系统会像所有跟它注册过的对象发送广播
方法:编写BroadcastReceiver,注册到Android系统,当事件发生,会通知所有的BroadcastReceiver,具体如下
1.编写广播接收类(继承BroadcastReceive)
2.注册到系统中(两种方式,见下面)
3.发送Intent
4.系统过滤到对应的Action,实例化接收类
5.调用接受类的构造方法和OnResume方法
6.注销接受类(下次再重新实例化)
示例一:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testbroadcastreceive"
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.testbroadcastreceive.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.EDIT"/>
</intent-filter>
</activity>
</application>
</manifest>
TestBroadcastReceive.java
<span style="font-family:SimHei;font-size:14px;">package com.example.testbroadcastreceive;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class TestBroadcastReceive extends BroadcastReceiver{
public TestBroadcastReceive(){
Log.e("Potato", "TestBroadcastReceive的构造方法");
}
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Log.e("Potato", "TestBroadcastReceive的OnReceive方法");
}
}
</span>
MainActivity.java
<span style="font-family:SimHei;font-size:14px;">package com.example.testbroadcastreceive;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent=new Intent();
intent.setAction(Intent.ACTION_EDIT);
this.sendBroadcast(intent);
}
}
</span>
注册BroadcastReceive的两种方法
1.在Androidmanifest.xml中注册
详见示例一:
<intent-filter>
<action android:name="android.intent.action.EDIT"/>
</intent-filter>
2.在Java代码中注册
注册BroadcastReceive:registerReceiver(receiver,filter);
取消注册BroadcastReceive:unregisterReceive(receiver);
下面附上例子
示例二:
Androidmanifest,xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="36dp"
android:layout_marginTop="36dp"
android:text="注册" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="28dp"
android:text="取消" />
</RelativeLayout>
TestTestBroadcastReceive.java与示例一一样
MainActivity.java
package com.example.testbroadcastreceive;
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 MainActivity extends Activity {
Button buttonRegister;
Button buttonCannel;
TestBroadcastReceive receiver=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonRegister=(Button)findViewById(R.id.button1);
buttonCannel=(Button)findViewById(R.id.button2);
buttonRegister.setOnClickListener(new MyListener());
buttonCannel.setOnClickListener(new MyListener());
}
class MyListener implements OnClickListener{
@Override
public void onClick(View view) {
if(view.getId()==R.id.button1){//注册
//实例化一个BroadcastReceive
receiver=new TestBroadcastReceive();
//实例化一个Intent
IntentFilter filter=new IntentFilter();
filter.addAction("android.intent.action.EDIT");
MainActivity.this.registerReceiver(receiver, filter);
//发送广播
Intent intent=new Intent();
intent.setAction(Intent.ACTION_EDIT);
MainActivity.this.sendBroadcast(intent);
}
if(view.getId()==R.id.button2){//取消注册
MainActivity.this.unregisterReceiver(receiver);
}
}
}
}