01.
<?xml version=
"1.0"
encoding=
"utf-8"
?>
02.
<manifest xmlns:android=
"http://schemas.android.com/apk/res/android"
03.
package
=
"com.xjp.mybroadcast"
>
04.
05.
<application
06.
android:allowBackup=
"true"
07.
android:icon=
"@drawable/ic_launcher"
08.
android:label=
"@string/app_name"
09.
android:theme=
"@style/AppTheme"
>
10.
<activity
11.
android:name=
".MainActivity"
12.
android:label=
"@string/app_name"
>
13.
<intent-filter>
14.
<action android:name=
"android.intent.action.MAIN"
/>
15.
16.
<category android:name=
"android.intent.category.LAUNCHER"
/>
17.
</intent-filter>
18.
</activity>
19.
20.
<!--静态注册广播,此处需要添加广播的action-->
21.
<receiver android:name=
".BroadCastReceive1"
>
22.
<intent-filter>
23.
<action android:name=
"com.xjp.mybroadcast.BroadCastReceive1"
></action>
24.
</intent-filter>
25.
</receiver>
26.
</application>
27.
28.
</manifest>
01.
package
com.xjp.mybroadcast;
02.
03.
import
android.content.BroadcastReceiver;
04.
import
android.content.Context;
05.
import
android.content.Intent;
06.
07.
/**
08.
* Description:静态广播接收器
09.
* User: xjp
10.
* Date: 2015/5/14
11.
* Time: 14:56
12.
*/
13.
14.
public
class
BroadCastReceive1
extends
BroadcastReceiver {
15.
16.
@Override
17.
public
void
onReceive(Context context, Intent intent) {
18.
//TODO 接收到广播之后处理自己的事情
19.
String action = intent.getAction();
20.
String result = intent.getStringExtra(
"key"
);
21.
MyLog.d(
"the BroadCast action is "
+ action +
" the BroadCast receive result is "
+ result);
22.
}
23.
}
01.
package
com.xjp.mybroadcast;
02.
03.
import
android.content.Intent;
04.
import
android.os.Bundle;
05.
import
android.support.v7.app.ActionBarActivity;
06.
import
android.view.View;
07.
import
android.widget.Button;
08.
09.
10.
public
class
MainActivity
extends
ActionBarActivity
implements
View.OnClickListener {
11.
12.
private
Button btnSend;
13.
14.
@Override
15.
protected
void
onCreate(Bundle savedInstanceState) {
16.
super
.onCreate(savedInstanceState);
17.
setContentView(R.layout.activity_main);
18.
19.
btnSend = (Button) findViewById(R.id.button);
20.
btnSend.setOnClickListener(
this
);
21.
22.
}
23.
24.
@Override
25.
public
void
onClick(View v) {
26.
switch
(v.getId()) {
27.
case
R.id.button:
28.
sendBraodCast();
29.
break
;
30.
}
31.
}
32.
33.
private
void
sendBraodCast() {
34.
35.
/**
36.
* 申明静态广播的 action 行为
37.
*/
38.
String action =
"com.xjp.mybroadcast.BroadCastReceive1"
;
39.
Intent intent =
new
Intent(action);
40.
intent.putExtra(
"key"
,
"静态广播测试"
);
41.
sendBroadcast(intent);
42.
}
43.
}
001.
<pre name=
"code"
class
=
"java"
>
package
com.xjp.mybroadcast;
002.
003.
import
android.content.BroadcastReceiver;
004.
import
android.content.Context;
005.
import
android.content.Intent;
006.
import
android.content.IntentFilter;
007.
import
android.os.Bundle;
008.
import
android.support.v4.content.LocalBroadcastManager;
009.
import
android.support.v7.app.ActionBarActivity;
010.
import
android.view.View;
011.
import
android.widget.Button;
012.
013.
014.
public
class
MainActivity
extends
ActionBarActivity
implements
View.OnClickListener {
015.
016.
private
Button btnSend;
017.
018.
private
BroadCastReceive2 myReceive;
019.
020.
private
IntentFilter filter;
021.
022.
private
final
static
String ACTION =
"com.xjp.mybroadcast.BroadCastReceive2"
;
023.
024.
@Override
025.
protected
void
onCreate(Bundle savedInstanceState) {
026.
super
.onCreate(savedInstanceState);
027.
setContentView(R.layout.activity_main);
028.
029.
btnSend = (Button) findViewById(R.id.button);
030.
btnSend.setOnClickListener(
this
);
031.
032.
myReceive =
new
BroadCastReceive2();
033.
filter =
new
IntentFilter();
034.
filter.addAction(ACTION);
035.
036.
}
037.
038.
039.
@Override
040.
protected
void
onResume() {
041.
/**
042.
* 注册广播
043.
*/
044.
045.
LocalBroadcastManager.getInstance(
this
).registerReceiver(myReceive, filter);
//官方建议使用
046.
047.
// this.registerReceiver(myReceive, filter);
048.
049.
super
.onResume();
050.
}
051.
052.
053.
@Override
054.
protected
void
onPause() {
055.
/**
056.
* 注销广播
057.
*/
058.
059.
LocalBroadcastManager.getInstance(
this
).unregisterReceiver(myReceive);
//官方建议使用
060.
061.
// this.unregisterReceiver(myReceive);
062.
063.
super
.onPause();
064.
}
065.
066.
@Override
067.
public
void
onClick(View v) {
068.
switch
(v.getId()) {
069.
case
R.id.button:
070.
sendBraodCast();
071.
break
;
072.
}
073.
}
074.
075.
private
void
sendBraodCast() {
076.
077.
/**
078.
* 申明广播的 action 行为
079.
*/
080.
Intent intent =
new
Intent(ACTION);
081.
intent.putExtra(
"key"
,
"动态广播测试"
);
082.
083.
/**
084.
* 官方提倡使用如下发送广播,原因是更快,更安全,不会导致内存泄漏
085.
*/
086.
LocalBroadcastManager.getInstance(
this
).sendBroadcast(intent);
087.
088.
// this.sendBroadcast(intent);
089.
}
090.
091.
092.
/**
093.
* 内部类实现广播接收器
094.
*/
095.
private
class
BroadCastReceive2
extends
BroadcastReceiver {
096.
097.
@Override
098.
public
void
onReceive(Context context, Intent intent) {
099.
//TODO 接收到广播之后处理自己的事情
100.
String action = intent.getAction();
101.
String result = intent.getStringExtra(
"key"
);
102.
MyLog.d(
"the BroadCast action is "
+ action +
" the BroadCast receive result is "
+ result);
103.
}
104.
}
105.
}
1.
001.
<pre name=
"code"
class
=
"java"
>
package
com.xjp.mybroadcast;
002.
003.
import
android.content.BroadcastReceiver;
004.
import
android.content.Context;
005.
import
android.content.Intent;
006.
import
android.content.IntentFilter;
007.
import
android.os.Bundle;
008.
import
android.support.v7.app.ActionBarActivity;
009.
import
android.view.View;
010.
import
android.widget.Button;
011.
012.
013.
public
class
MainActivity
extends
ActionBarActivity
implements
View.OnClickListener {
014.
015.
private
Button btnSend;
016.
017.
private
BroadCastReceive2 myReceive;
018.
019.
private
BroadCastReceive3 myReceive3;
020.
021.
private
IntentFilter filter;
022.
023.
private
IntentFilter filter3;
024.
025.
private
final
static
String ACTION =
"com.xjp.mybroadcast.BroadCastReceive2"
;
026.
027.
@Override
028.
protected
void
onCreate(Bundle savedInstanceState) {
029.
super
.onCreate(savedInstanceState);
030.
setContentView(R.layout.activity_main);
031.
032.
btnSend = (Button) findViewById(R.id.button);
033.
btnSend.setOnClickListener(
this
);
034.
035.
myReceive =
new
BroadCastReceive2();
036.
filter =
new
IntentFilter();
037.
filter.addAction(ACTION);
038.
filter.setPriority(
2
);
//设置广播的优先级, -1000~1000 ,数字越大,优先级越高。
039.
040.
myReceive3 =
new
BroadCastReceive3();
041.
filter3 =
new
IntentFilter();
042.
filter3.addAction(ACTION);
043.
filter3.setPriority(
1
);
044.
045.
046.
}
047.
048.
049.
@Override
050.
protected
void
onResume() {
051.
/**
052.
* 注册广播
053.
*/
054.
registerReceiver(myReceive, filter);
055.
registerReceiver(myReceive3, filter3);
056.
057.
058.
super
.onResume();
059.
}
060.
061.
062.
@Override
063.
protected
void
onPause() {
064.
/**
065.
* 注销广播
066.
*/
067.
068.
unregisterReceiver(myReceive);
069.
unregisterReceiver(myReceive3);
070.
071.
super
.onPause();
072.
}
073.
074.
@Override
075.
public
void
onClick(View v) {
076.
switch
(v.getId()) {
077.
case
R.id.button:
078.
sendBraodCast();
079.
break
;
080.
}
081.
}
082.
083.
private
void
sendBraodCast() {
084.
085.
/**
086.
* 申明广播的 action 行为
087.
*/
088.
Intent intent =
new
Intent(ACTION);
089.
intent.putExtra(
"key"
,
"有序广播测试"
);
090.
091.
this
.sendOrderedBroadcast(intent,
null
);
092.
}
093.
094.
095.
/**
096.
* 内部类实现广播接收器
097.
*/
098.
private
class
BroadCastReceive2
extends
BroadcastReceiver {
099.
100.
@Override
101.
public
void
onReceive(Context context, Intent intent) {
102.
//TODO 接收到广播之后处理自己的事情
103.
String action = intent.getAction();
104.
String result = intent.getStringExtra(
"key"
);
105.
MyLog.d(
"the BroadCast action is "
+ action +
" the BroadCast receive result is "
+ result);
106.
107.
Bundle bundle =
new
Bundle();
108.
bundle.putString(
"key"
,
"有序广播处理之后"
+
"\n"
+
"再次发送给下一个广播接收者"
);
109.
intent.putExtra(
"bundle"
, bundle);
110.
setResultExtras(bundle);
111.
//切断广播,不再让此广播继续往下发送。
112.
// abortBroadcast();
113.
}
114.
}
115.
116.
/**
117.
* 内部类实现广播接收器
118.
*/
119.
private
class
BroadCastReceive3
extends
BroadcastReceiver {
120.
121.
@Override
122.
public
void
onReceive(Context context, Intent intent) {
123.
//TODO 接收到广播之后处理自己的事情
124.
String action = intent.getAction();
125.
//要不要接受上一个广播接收器receiver2传来的的数据
126.
Bundle bundle = getResultExtras(
true
);
127.
MyLog.d(
"the BroadCast action is "
+ action +
" the BroadCast receive result is "
+ bundle.getString(
"key"
));
128.
}
129.
}
130.
}
1.
1.
<uses-permission android:name=
"android.permission.BROADCAST_STICKY"
></uses-permission>
01.
package
com.xjp.mybroadcast;
02.
03.
import
android.content.Intent;
04.
import
android.os.Bundle;
05.
import
android.support.v7.app.ActionBarActivity;
06.
import
android.view.View;
07.
import
android.widget.Button;
08.
09.
10.
/**
11.
* 发送广播的Activity
12.
*/
13.
public
class
MainActivity
extends
ActionBarActivity
implements
View.OnClickListener {
14.
15.
private
Button btnSend;
16.
17.
18.
private
final
static
String ACTION =
"com.xjp.mybroadcast.BroadCastReceive1"
;
19.
private
final
static
String ACTION1 =
"com.xjp.mybroadcast.BroadCastReceive2"
;
20.
21.
@Override
22.
protected
void
onCreate(Bundle savedInstanceState) {
23.
super
.onCreate(savedInstanceState);
24.
setContentView(R.layout.activity_main);
25.
26.
btnSend = (Button) findViewById(R.id.button);
27.
btnSend.setOnClickListener(
this
);
28.
}
29.
30.
31.
@Override
32.
public
void
onClick(View v) {
33.
switch
(v.getId()) {
34.
case
R.id.button:
35.
sendBraodCast();
36.
break
;
37.
}
38.
}
39.
40.
private
void
sendBraodCast() {
41.
42.
/**
43.
* 申明广播的 action 行为
44.
*/
45.
Intent intent =
new
Intent(ACTION);
46.
intent.putExtra(
"key"
,
"普通广播测试"
);
47.
sendBroadcast(intent);
48.
49.
Intent intent1 =
new
Intent(ACTION1);
50.
intent1.putExtra(
"key"
,
"粘性广播测试"
);
51.
sendStickyBroadcast(intent1);
52.
startActivity(
new
Intent(
this
, RecevieActivity.
class
));
53.
}
54.
}
01.
package
com.xjp.mybroadcast;
02.
03.
import
android.app.Activity;
04.
import
android.content.BroadcastReceiver;
05.
import
android.content.Context;
06.
import
android.content.Intent;
07.
import
android.content.IntentFilter;
08.
import
android.os.Bundle;
09.
10.
/**
11.
* Description:接受广播的Activity
12.
* User: xjp
13.
* Date: 2015/5/14
14.
* Time: 17:03
15.
*/
16.
17.
public
class
RecevieActivity
extends
Activity {
18.
19.
private
final
static
String ACTION1 =
"com.xjp.mybroadcast.BroadCastReceive1"
;
20.
private
final
static
String ACTION2 =
"com.xjp.mybroadcast.BroadCastReceive2"
;
21.
22.
private
Receive receive;
23.
24.
private
IntentFilter filter1;
25.
26.
@Override
27.
protected
void
onCreate(Bundle savedInstanceState) {
28.
super
.onCreate(savedInstanceState);
29.
30.
receive =
new
Receive();
31.
32.
filter1 =
new
IntentFilter();
33.
filter1.addAction(ACTION1);
34.
filter1.addAction(ACTION2);
35.
36.
}
37.
38.
@Override
39.
protected
void
onResume() {
40.
super
.onResume();
41.
registerReceiver(receive, filter1);
42.
}
43.
44.
@Override
45.
protected
void
onPause() {
46.
super
.onPause();
47.
unregisterReceiver(receive);
48.
}
49.
50.
private
class
Receive
extends
BroadcastReceiver {
51.
@Override
52.
public
void
onReceive(Context context, Intent intent) {
53.
String action = intent.getAction();
54.
String result = intent.getStringExtra(
"key"
);
55.
MyLog.d(
"the BroadCast action is "
+ action +
" the BroadCast receive result is "
+ result);
56.
}
57.
}
58.
}
1.
<uses-permission android:name=
"android.permission.BROADCAST_STICKY"
></uses-permission>
01.
// Register for the battery changed event
02.
IntentFilter filter =
new
IntentFilter(Intent.ACTION_BATTERY_CHANGED);
03.
04.
/ Intent is sticky so using
null
as receiver works fine
05.
// return value contains the status
06.
Intent batteryStatus =
this
.registerReceiver(
null
, filter);
07.
08.
// Are we charging / charged?
09.
int
status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -
1
);
10.
boolean
isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING
11.
|| status == BatteryManager.BATTERY_STATUS_FULL;
12.
13.
boolean
isFull = status == BatteryManager.BATTERY_STATUS_FULL;
14.
15.
// How are we charging?
16.
int
chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -
1
);
17.
boolean
usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
18.
boolean
acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
Event | Description |
---|---|
Intent.ACTION_BOOT_COMPLETED | Boot completed. Requires the android.permission.RECEIVE_BOOT_COMPLETEDpermission. |
Intent.ACTION_POWER_CONNECTED | Power got connected to the device. |
Intent.ACTION_POWER_DISCONNECTED | Power got disconnected to the device. |
Intent.ACTION_BATTERY_LOW | Triggered on low battery. Typically used to reduce activities in your app which consume power. |
Intent.ACTION_BATTERY_OKAY | Battery status good again. |