一个Activity A通过sendBroadcast来发送广播,Service B和Service C来监听该广播。正常情况下,Service B 和 Service C都可以收到该广播。要实现的效果是,Service B先收到该广播,然后停止广播,Service C无法接受。(注:Activity A 和 Service C 无法修改,只能在Service B里加处理方法)请问该如何实现?
解决方案 »
- 求助!在android端使用openssl生成的rsapublicKey解密的相关问题
- 关于android的button控件
- android频闪如何实现,需要和歌曲同步,做过的来。没做的可以讨论。
- 简单问题,如何取得网页内容!
- 有在用phone gap开发的吗
- 求教android桌面增强软件的分页动画是如何实现的?
- 【求助】android ril层移植到通用linux平台
- 各位大牛,在AndroidManifest中<receiver/>可以放在<activity/>内部吗?
- android客户端与服务器通信
- 关于android socket 客户端 与服务器通讯!
- Ubuntu12.04下载android源码出错
- Android相机预览时,屏幕背景有个矩形框,或者有几个关键点 怎么弄?
this.abortBroadcast();
<intent-filter android:priority="900">
发送有序广播,并在AndroidManifest.xml的Service B增加
<intent-filter android:priority="max">
优先级数值越大,接受广播顺序越靠前
Intent intent = new Intent("action.string");
sendOrderdBroadcast(intent, "permission.string");
}B{
@Override
public void onReceive(Context context, Intent intent) {
abortBroadcast();
}
}AndroidManifest.xml
<manifest>
<permission
android:name="permission.string"
android:protectionLevel="dangerous" >
</permission>
<uses-permission android:name="permission.string" />
<application>
...
<receiver android:name=".B">
<intent-filter android:priority="3">
<action android:name="action.string" />
</intent-filter>
</receiver>
<receiver android:name=".C">
<intent-filter android:priority="1">
<action android:name="action.string" />
</intent-filter>
</receiver>
</application>
</manifest>
Intent intent = new Intent("action.string");
sendOrderdBroadcast(intent, "permission.string");
}B{
@Override
public void onReceive(Context context, Intent intent) {
abortBroadcast();
}
}AndroidManifest.xml
<manifest>
<permission
android:name="permission.string"
android:protectionLevel="dangerous" >
</permission>
<uses-permission android:name="permission.string" />
<application>
...
<receiver android:name=".B">
<intent-filter android:priority="3">
<action android:name="action.string" />
</intent-filter>
</receiver>
<receiver android:name=".C">
<intent-filter android:priority="1">
<action android:name="action.string" />
</intent-filter>
</receiver>
</application>
</manifest>
MainFest.xml中注册一下两个服务代码如下
package com.csdn.receiver;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;import com.csdn.receiver.sevice.A;
import com.csdn.receiver.sevice.B;public class MainActivity extends Activity implements OnClickListener {
private Intent a;
private Intent b; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // 启动service
a = new Intent(this, A.class);
b = new Intent(this, B.class);
System.out.println("oncreate");
this.startService(b);
this.startService(a); findViewById(R.id.button1).setOnClickListener(this);
// 写一个广播 } @Override
protected void onDestroy() { super.onDestroy(); this.stopService(a);
this.stopService(b);
} public void onClick(View v) {
System.out.println("onclick");
Intent intent = new Intent();
intent.setAction("com.tcd.love");
this.sendOrderedBroadcast(intent, null); }
}服务Apublic class A extends Service { private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
this.abortBroadcast();
System.out.print("aa");
}
};
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
System.out.println("A sevice");
IntentFilter filter = new IntentFilter();
filter.setPriority(Integer.MAX_VALUE);
filter.addAction("com.tcd.love");
this.registerReceiver(receiver, filter);
}
@Override
public void onDestroy() {
super.onDestroy();
this.unregisterReceiver(receiver);
}
}
服务Bpublic class A extends Service { private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
this.abortBroadcast();
System.out.print("aa");
}
};
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
System.out.println("A sevice");
IntentFilter filter = new IntentFilter();
filter.setPriority(Integer.MAX_VALUE);
filter.addAction("com.tcd.love");
this.registerReceiver(receiver, filter);
}
@Override
public void onDestroy() {
super.onDestroy();
this.unregisterReceiver(receiver);
}
}