Android之BroadcastReceiver
BroadcastReceiver是一种全局的监听器,用于监听系统的广播消息,可以很方便的实现系统中不同组件之间的通信。
1.发送广播
调用Context的sendBroadcast(Intent intent)方法即可,这条广播将会启动Intent参数对应的BroadcastReceiver。
2.接收广播(BroadcastReceiver)
BroadcastReceiver用于接收程序(包括用户程序和系统内建的程序)发出的广播消息Intent。
实现BroadcastReceiver能匹配的Intent有两种方式
(1)使用代码指定(非常驻广播)
IntentFilter filter = new IntentFilter("android.provider.Telephone.SMS_RECEIVED");
IncomingSMSReceiver receiver = new IncomingSMSReceiver();
registerReceiver(receiver,filter);
(2) 在AndroidManifest.xml文件中配置(常驻广播)
<receiver android:name=".IncomingSMSReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED">
<intent-filter>
</receiver>
实例如下:
i.常驻广播
package com.example.broadcast;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
static final String BROADCAST_STRING =
"com.example.action.SENT_BROADCAST";
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.example.action.SENT_BROADCAST");
intent.putExtra("msg", "TCL Alcatel One Touch!");
sendBroadcast(intent);
Toast.makeText(MainActivity.this, "广播已发送", Toast.LENGTH_SHORT).show();
finish();
}
});
}
}
package com.example.broadcast;
import com.example.broadcast.R;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.widget.CheckBox;
public class Receiver extends PreferenceActivity{
CheckBoxPreference checkBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.test);
checkBox = (CheckBoxPreference)findPreference("cb");
Intent intent = getIntent();
String s = intent.getStringExtra("msg");
checkBox.setTitle(s);
}
@Override
@Deprecated
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
Preference preference) {
// TODO Auto-generated method stub
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
}
package com.example.broadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
public class MyReceiver extends BroadcastReceiver{
static final String BROADCAST_STRING =
"com.example.action.SENT_BROADCAST";
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(BROADCAST_STRING)){
String message = intent.getStringExtra("msg");
Intent intent2 = new Intent(context,Receiver.class);
intent2.putExtra("msg", message);
intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent2);
}
}
}
<?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="19"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Test" >
<activity
android:name=".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=".MyReceiver">
<intent-filter>
<action android:name="com.example.action.SENT_BROADCAST"/>
</intent-filter>
</receiver>
<activity
android:name=".Receiver"
android:label="@string/app_name1" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="cb"
android:id="@+id/cb"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</PreferenceScreen>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Test" parent="@android:style/Theme.Holo">
</style>
</resources>
ii.非常驻广播
只需将上面的AndroidManifest.xml文件中的<receiver>...</receiver>删除,
并且在MainActivity.java文件中的onCreate()方法中添加如下代码即可
MyReceiver receiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter(BROADCAST_STRING);
registerReceiver(receiver, intentFilter);
3.接收系统广播
package com.test.project;
import android.R.string;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class UsbReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.hardware.usb.action.USB_STATE")){
if (intent.getExtras().getBoolean("connected")){
// usb 插入
Toast.makeText(context, "插入", Toast.LENGTH_SHORT).show();
Intent intent2 = new Intent(context,MainActivity.class);
intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent2);
}else{
// usb 拔出
Toast.makeText(context, "拔出", Toast.LENGTH_SHORT).show();
}
}
}
}
<receiver android:name=".UsbReceiver">
<intent-filter android:priority="800">
<!--监听系统的USB状态 -->
<action android:name="android.hardware.usb.action.USB_STATE"/>
</intent-filter>
</receiver>