Broadcast Reveiver作用
Broadcast Reveiver作用以及为何要引入(用来接收系统以及自定义消息的)
在系统内通知和判定执行状态
1,系统执行状态,开机了,TF卡插拔,准备关机,电量低了,
2,自定义执行状态,发消息提示更新界面
点击后会打log:顺序是testbroadcast2,testbroadcast
activity代码-----
[java]
package com.paoyx;
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 BroadcastActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//设置按钮
Button button = new Button(this);
button.setText("send message");
setContentView(button);
button.setOnClickListener(new OnClickListener() {
//按钮点击事件
public void onClick(View v) {
//设置intent传递数值
Intent intent = new Intent();
intent.setAction("com.paoyx.broadcast");
sendBroadcast(intent);
}
});
}
}
package com.paoyx;
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 BroadcastActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//设置按钮
Button button = new Button(this);
button.setText("send message");
setContentView(button);
button.setOnClickListener(new OnClickListener() {
//按钮点击事件
public void onClick(View v) {
//设置intent传递数值
Intent intent = new Intent();
intent.setAction("com.paoyx.broadcast");
sendBroadcast(intent);
}
});
}
}
2个receiver代码---
[html]
package com.paoyx;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals("com.paoyx.broadcast")) {
Log.i("log","testbroadcast");
}
}
}
package com.paoyx;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals("com.paoyx.broadcast")) {
Log.i("log","testbroadcast");
}
}
}
[html]
package com.paoyx;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver2 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals("com.paoyx.broadcast")) {
Log.i("log","testbroadcast2");
}
}
}
package com.paoyx;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver2 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals("com.paoyx.broadcast")) {
Log.i("log","testbroadcast2");
}
}
}
menifest注册监听代码---
[html]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.paoyx"
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=".BroadcastActivity"
android:label="@string/app_name" >
<inten
补充:移动开发 , Android ,