android 广播机制
Android广播机制指的是,在一个应用程序运行的时候可以自定义一个消息类型,让相应的接收器去处理这个消息或者是系统消息,比如来电话了、来短信了、手机没电了等等系统发送的消息;
Android提供了两种注册广播接受者的形式,分别是在程序中动态注册和在xml中指定。他们之间的区别就是作用的范围不同,程序动态注册的接收者只在程序运行过程中有效,而在xml注册的接收者不管你的程序有没有启动都会起作用;
基于XML配置的注册广播形式为:
点击发送按钮,打印信息为:
代码如下:
activity_main.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"
tools:context="com.xiaozhang.broadcast1.MainActivity" >
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击发送" />
</RelativeLayout>
MainActivity.java
package com.xiaozhang.broadcast1;
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 MainActivity extends Activity {
private Button sendButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendButton = (Button) findViewById(R.id.button);
sendButton.setOnClickListener(new BroadcastListener());
}
class BroadcastListener implements OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_EDIT);
MainActivity.this.sendBroadcast(intent);
}
}
}
TestReceiver.java
package com.xiaozhang.broadcast1;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class TestReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
System.out.println("接收到了信息:" + action);
}
}
在AndroidManifest.xml中配置为:
在<application>中加入:
<receiver android:name=".TestReceiver" >
<intent-filter>
<action android:name="android.intent.action.EDIT" />
</intent-filter>
</receiver>
在程序中动态指定为:
直接点击发送广播,没反应,要先注册,再发送,再取消注册;
代码实现:
activity_main.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="com.xiaozhang.broadcast2.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册广播" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/button1"
android:text="发送广播" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/button2"
android:text="取消注册" />
</RelativeLayout>
MainActivity.java
package com.xiaozhang.broadcast2;
import java.util.Calendar;
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 {
private Button button1;
private Button button2;
private Button button3;
private ReceiveBroadCast receiveBroadCast = null;
private static final String ACTION = "android:provider.Telephony.SMS_RECEIVE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
receiveBroadCast = new ReceiveBroadCast();
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION);
registerReceiver(receiveBroadCast, filter);
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("data", "message: "
+ Calendar.getInstance().get(Calendar.SECOND));
intent.setAction(ACTION);
sendBroadcast(intent);
}
});
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
unregisterReceiver(receiveBroadCast);
}
});
}
}
ReceiveBroadCast.java
package com.xiaozhang.broadcast2;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class ReceiveBroadCast extends BroadcastReceiver {
private static final String TAG = "MyReceiver";
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra("data");
Log.i(TAG, message);
}
}
推荐阅读:
http://www.cnblogs.com/qianlifeng/archive/2011/03/06/1972305.html
http://blog.csdn.net/abc5382334/article/details/15796823
http://www.cnblogs.com/totem1990/archive/2012/09/10/2679391.html
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。