说明 | |
---|---|
首次发布 | 2019年02月15日 |
最近更新 | 2019年02月15日 |
广播的生命周期
BroadcastReceiver生命周期只有十秒左右,如果在onReceive()内做超过十秒的事情,就会报错。所以广播中不要执行耗时操作,可以考虑启动一个Service来完成操作。
广播的分类
根据注册方式,可以分为静态广播和动态广播。
静态广播
需要在AndroidManifest.xml文件中配置
,具有常驻性广播,无论程序启动与否,广播依然存在
的特点。比如:我们可以接收开机的广播,当收到这条广播时就可以在 onReceive()方法里执行相应的逻辑,从而实现开机启动的功能。
由于谷歌从Android 8开始,对静态广播做了限制,具体请查看。需要在发送广播时,给intent添加setComponent()
,ComponentName(@NonNull String pkg, @NonNull String cls)
有两个参数:参数一为包名,参数二为接收器receiver在包内的路径。具体请参考代码。
动态广播
代码中动态指定广播地址并注册,具有非常驻型,广播会跟随程序的生命周期的结束而结束
的特点。需要特别注意的是:需要在onCreate()
中进行注册,在onDestroy()
中进行注销。
AndroidManifest.xml
<application
....
<!--注册静态广播-->
<receiver android:name=".other.DynamicBroadcastReceiver">
<intent-filter>
<action android:name="com.tencent.testand.StaticBroadcast"/>
</intent-filter>
</receiver>
</application>
BroadcastActivity
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.tencent.testand.R;
import com.tencent.testand.other.DynamicBroadcastReceiver;
public class BroadcastActivity extends Activity implements View.OnClickListener {
public static final String DYNAMIC_BROADCAST_ACTION = "com.tencent.testand.DynamicBroadcast";
public static final String STATIC_BROADCAST_ACTION = "com.tencent.testand.StaticBroadcast";
public static final String LOCAL_BROADCAST = "my_local_broadcast";
private Button mSendDynamicBroadcastButton;
private DynamicBroadcastReceiver mReceiver;
private Button mSendStaticBroadcastButton;
private Button mSendLocalBroadcastButton;
private LocalReceiver mLocalReceiver;
private LocalBroadcastManager mLocalBroadcastManager;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_braodcast);
findViews();
registerBroadcast();
registerLocalBroadcast();
}
/**
* 注册本地广播
*/
private void registerLocalBroadcast() {
mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(LOCAL_BROADCAST);
mLocalReceiver = new LocalReceiver();
mLocalBroadcastManager.registerReceiver(mLocalReceiver, intentFilter);
}
private void findViews() {
mSendDynamicBroadcastButton = findViewById(R.id.send_dynamic_broadcast_button);
mSendDynamicBroadcastButton.setOnClickListener(this);
mSendStaticBroadcastButton = findViewById(R.id.send_static_broadcast_button);
mSendStaticBroadcastButton.setOnClickListener(this);
mSendLocalBroadcastButton = findViewById(R.id.send_local_broadcast_button);
mSendLocalBroadcastButton.setOnClickListener(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
// 注销
unregisterReceiver(mReceiver);
// 注销本地广播
mLocalBroadcastManager.unregisterReceiver(mLocalReceiver);
}
/**
* 注册广播
*/
private void registerBroadcast() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(DYNAMIC_BROADCAST_ACTION);
mReceiver = new DynamicBroadcastReceiver();
registerReceiver(mReceiver, intentFilter);
}
/**
* 内部类:本地广播的接收者
*/
public class LocalReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "received local broadcast",
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.send_dynamic_broadcast_button: {
// 注意:intent里面必须要写action的名字
Intent intent = new Intent(DYNAMIC_BROADCAST_ACTION);
intent.putExtra("key", "I'm dynamic broadcast");
sendBroadcast(intent);
break;
}
case R.id.send_static_broadcast_button: {
// 注意:intent里面必须要写action的名字
Intent intent = new Intent(STATIC_BROADCAST_ACTION);
intent.setComponent(new ComponentName(getPackageName(), getPackageName() + ".other.DynamicBroadcastReceiver"));
intent.putExtra("key", "I'm static broadcast");
sendBroadcast(intent);
break;
}
case R.id.send_local_broadcast_button: {
// 注意:intent里面必须要写action的名字
Intent intent = new Intent(LOCAL_BROADCAST);
intent.putExtra("key", "I'm local broadcast");
// 注意:这里一定要用mLocalBroadcastManager,这也是本地广播与其他广播的区别
mLocalBroadcastManager.sendBroadcast(intent);
break;
}
}
}
}
DynamicBroadcastReceiver
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import com.tencent.testand.activity.DynamicBroadcastActivity;
public class DynamicBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 根据action来区分广播
if (TextUtils.equals(intent.getAction(), DynamicBroadcastActivity.DYNAMIC_BROADCAST_ACTION)
|| TextUtils.equals(intent.getAction(), DynamicBroadcastActivity.STATIC_BROADCAST_ACTION))
{
Toast.makeText(context, intent.getStringExtra("key"), Toast.LENGTH_LONG).show();
Log.i(getClass().getSimpleName(), "onReceive: ");
}
}
}
网友评论