安卓broadcastreceiver怎么接收的广播
1个回答
展开全部
1.广播接收者(BroadcastReceiver)
广播接收者(BroadcastReceiver)继承BroadcastReceiver类接收广播意图的Java类,重写:
public void onReceive(Context context,Intent intent),其中intent可以获得传递的数据;
广播意图就是通过Context.sendBroadcast(Intent intent)或Context.sendOrderedBroadcast(Intentintent)发送的意图,通过这个语句,能够广播给所有满足条件的组件,比如intent设置了action="Receiver",则所有在AndroidManifest.xml中设置过<actionandroid:name="Receiver"/>的广播接收者都能够接收到广播;
2.广播发送者sendBroadcast()
通常广播发送方就是调用Context.sendBroadcast()的程序,通常广播发送方都是通过隐式意图发送出去;
广播发送方分为普通广播和有序广播;
同步广播:发送方发出后,几乎同时到达多个广播接收者处,某个接收者不能接收到广播后进行一番处理后传给下一个接收者,并且无法终止广播继续传播;Context.sendBroadcast(intent);
有序广播:广播接收者需要提前设置优先级,优先级高的先接收到广播,优先级数值为-1000~1000,在AndroidManifest.xml的<intent-filterandroid:priority="1">设置;比如存在3个广播接收者A、B、C、D,优先级A>B>C>D,因此A最先收到广播,当A收到广播后,可以向广播中添加一些数据给下一个接收者(intent.putExtra()),或者终止广播 abortBroadcast();Context.sendOrderedBroadcast(intent);
一、同步广播发送方核心代码
Intent intent = new Intent();
intent.setAction("Receiver");
Context.sendBroadcast(intent);
有序广播发送方核心代码:
Intent intent = new Intent();
intent.setAction("Receiver");
Context.sendOrderedBroadcast(intent,null);
二、广播接收者核心代码:
public class MyReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();...
}
}
三 注册该广播
AndroidManifest.xml 注册方式
<receiver android:name=".MyReceiver">
<intent-filter android:priority="1000">
<action android:name="Receiver"/>
</intent-filter>
</receiver>
Java类注册方式
publicvoid registerBoradcastReceiver()
{
IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction("Receiver");
registerReceiver(广播类对象, myIntentFilter);
}
简单例子
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
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;
import android.widget.LinearLayout;
import android.widget.Toast;
public class Test extends Activity{
private final String ACTION_NAME = "Receiver";
private Button mBtnEvent = null;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//注册广播
registerBoradcastReceiver();
LinearLayout mLinearLayout = new LinearLayout(this);
mBtnEvent= new Button(this);
mBtnEvent.setText("发送广播");
mLinearLayout.addView(mBtnMsgEvent);
setContentView(mLinearLayout);
mBtnEvent.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sendTestBroadcast();
}
});
}
//发送广播
private void sendTestBroadcast()
{
Intent mIntent = new Intent(ACTION_NAME);
mIntent.putExtra("strData", "发送广播,在这里传送数据");
sendBroadcast(mIntent);
}
private BroadcastReceiver myReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(ACTION_NAME)){
Toast.makeText(Test.this, "接收测试", 200);
}
}
};
//注册广播
public void registerBoradcastReceiver(){
IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction(ACTION_NAME);
registerReceiver(myReceiver, myIntentFilter);
}
}
广播接收者(BroadcastReceiver)继承BroadcastReceiver类接收广播意图的Java类,重写:
public void onReceive(Context context,Intent intent),其中intent可以获得传递的数据;
广播意图就是通过Context.sendBroadcast(Intent intent)或Context.sendOrderedBroadcast(Intentintent)发送的意图,通过这个语句,能够广播给所有满足条件的组件,比如intent设置了action="Receiver",则所有在AndroidManifest.xml中设置过<actionandroid:name="Receiver"/>的广播接收者都能够接收到广播;
2.广播发送者sendBroadcast()
通常广播发送方就是调用Context.sendBroadcast()的程序,通常广播发送方都是通过隐式意图发送出去;
广播发送方分为普通广播和有序广播;
同步广播:发送方发出后,几乎同时到达多个广播接收者处,某个接收者不能接收到广播后进行一番处理后传给下一个接收者,并且无法终止广播继续传播;Context.sendBroadcast(intent);
有序广播:广播接收者需要提前设置优先级,优先级高的先接收到广播,优先级数值为-1000~1000,在AndroidManifest.xml的<intent-filterandroid:priority="1">设置;比如存在3个广播接收者A、B、C、D,优先级A>B>C>D,因此A最先收到广播,当A收到广播后,可以向广播中添加一些数据给下一个接收者(intent.putExtra()),或者终止广播 abortBroadcast();Context.sendOrderedBroadcast(intent);
一、同步广播发送方核心代码
Intent intent = new Intent();
intent.setAction("Receiver");
Context.sendBroadcast(intent);
有序广播发送方核心代码:
Intent intent = new Intent();
intent.setAction("Receiver");
Context.sendOrderedBroadcast(intent,null);
二、广播接收者核心代码:
public class MyReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();...
}
}
三 注册该广播
AndroidManifest.xml 注册方式
<receiver android:name=".MyReceiver">
<intent-filter android:priority="1000">
<action android:name="Receiver"/>
</intent-filter>
</receiver>
Java类注册方式
publicvoid registerBoradcastReceiver()
{
IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction("Receiver");
registerReceiver(广播类对象, myIntentFilter);
}
简单例子
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
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;
import android.widget.LinearLayout;
import android.widget.Toast;
public class Test extends Activity{
private final String ACTION_NAME = "Receiver";
private Button mBtnEvent = null;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//注册广播
registerBoradcastReceiver();
LinearLayout mLinearLayout = new LinearLayout(this);
mBtnEvent= new Button(this);
mBtnEvent.setText("发送广播");
mLinearLayout.addView(mBtnMsgEvent);
setContentView(mLinearLayout);
mBtnEvent.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sendTestBroadcast();
}
});
}
//发送广播
private void sendTestBroadcast()
{
Intent mIntent = new Intent(ACTION_NAME);
mIntent.putExtra("strData", "发送广播,在这里传送数据");
sendBroadcast(mIntent);
}
private BroadcastReceiver myReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(ACTION_NAME)){
Toast.makeText(Test.this, "接收测试", 200);
}
}
};
//注册广播
public void registerBoradcastReceiver(){
IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction(ACTION_NAME);
registerReceiver(myReceiver, myIntentFilter);
}
}
全测科技
2024-12-19 广告
2024-12-19 广告
N5173B信号源是深圳市全测科技有限公司引进的一款高性能微波模拟信号发生器。它覆盖了9kHz至40GHz的宽广频率范围,为宽带滤波器、放大器、接收机等器件的参数测试提供了必要的信号。该信号源凭借低成本、600s的快速频率切换速度以及大输出...
点击进入详情页
本回答由全测科技提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询