PS.不知不觉间发现自己已经做了很久很久的Android开发了,过去对知识块的梳理总是放在云笔记里面.主要的原因还是自己的笔记太杂乱,没有脉络.本着开源的精神,也趁着这段时间有空将之前云笔记里面的文章梳理下.同时将一些知识点整理出来和大家共同分享.
转载请注明出处:http://blog.csdn.net/unreliable_narrator?viewmode=contents
android 的广播接收者主要分为两个方面:一个方面是广播发送者另一个方面是广播接受者.广播发送者可以自定义广播进行发送.而广播接收者则是对广播进行接收并且做出相应的事件逻辑.广播接收者的主要作用就是Android组件间的通信和不同进程间的通信.
Intent intent = new Intent();
intent.setAction("test");
sendBroadcast(intent);
Intent intent = new Intent();
intent.setAction("com.dapeng");
sendOrderedBroadcast(intent, null);
<receiver
android:name=".MyReciverTwo"
android:priority="1000">
<intent-filter>
<action android:name="com.dapeng"/>
</intent-filter>
</receiver>
<receiver
android:name=".MyReciver">
<intent-filter android:priority="300">
<action android:name="com.dapeng"/>
</intent-filter>
</receiver>
public class ReciverTwo extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
abortBroadcast();
Log.d("--", "ReciverTwo被执行了");
}
}
public class ReciverTwo extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = new Bundle();
bundle.putString("add", "新加的数据");
setResultExtras(bundle);
Log.d("--", "ReciverTwo被执行了");
}
}
public class ReciverOne extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle resultExtras = getResultExtras(true);
String add = resultExtras.getString("add");
Log.d("--", "ReciverOne被执行了");
Log.d("--", "ReciverOne_add==" + add);
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnOne).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.test");
sendStickyBroadcast(intent);
}
});
findViewById(R.id.btnTwo).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
});
}
}
public class SecondActivity extends AppCompatActivity {
private MyReciver mMyReciver;
private IntentFilter mIntentFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//注册广播事件
mMyReciver = new MyReciver();
mIntentFilter = new IntentFilter();
mIntentFilter.addAction("com.test");
registerReceiver(mMyReciver, mIntentFilter);
}
public class MyReciver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("--", "我接收到了异步广播");
}
}
@Override
protected void onDestroy() {
unregisterReceiver(mMyReciver);
super.onDestroy();
}
}
<uses-permission android:name="android.permission.BROADCAST_STICKY"/>
public class ReciverOne extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("--", "ReciverOne被执行了");
}
}
<receiver android:name=".ReciverOne">
<intent-filter android:priority="100">
<action android:name="com.test"/>
</intent-filter>
</receiver>
Intent intent = new Intent();
intent.setAction("com.test");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.putExtra("name", "dapeng");
sendBroadcast(intent);
public class SecondActivity extends AppCompatActivity {
private MyReciver mMyReciver;
private IntentFilter mIntentFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//注册广播事件
mMyReciver = new MyReciver();
mIntentFilter = new IntentFilter();
mIntentFilter.addAction("com.test");
registerReceiver(mMyReciver, mIntentFilter);
}
public class MyReciver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//TODO do your work
}
}
@Override
protected void onDestroy() {
unregisterReceiver(mMyReciver);
super.onDestroy();
}
}
(1.)如果当前App intent-filter匹配了其他应用发出的广播,会导致当前App不断接收到广播并处理;
解决的方式是:将清单文件中广播接收者的exported属性人为设置成false,使得非本App内部发出的此广播不被接收;
<receiver
android:name=".Myreciver"
android:exported="false">
<intent-filter>
<action android:name="com.test"/>
</intent-filter>
</receiver>
(2)其他App可以通过注册与当前App一致的intent-filter用于接收广播,获取广播具体信息。
解决的方式是:.发送广播时,指定特定广播接收器所在的包名,具体是通过intent.setPackage(packageName)指定在,这样此广播将只会发送到此包中的App内与之相匹配的有效广播接收器中。
Intent intent = new Intent();
intent.setAction("com.test");
intent.setPackage(getPackageName());
sendOrderedBroadcast(intent, null);