在服务中使用广播接收器
Using Broadcast Reciever inside Service
我正在尝试在服务 class 中使用广播接收器。但是没有调用 Broadcast Receiver 中的 onReceive 方法。我不知道问题在哪里occurs.Below是我的代码,
public class CallService extends Service {
CallReceiver callReceiver=null;
String TAG=CallService.class.getSimpleName();
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
CallReceiver callReceiver=new CallReceiver();
IntentFilter intentFilter=new IntentFilter();
intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
intentFilter.addAction("a");
this.registerReceiver(callReceiver,intentFilter);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate: started in call service");
CallReceiver callReceiver=new CallReceiver();
IntentFilter intentFilter=new IntentFilter();
intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
intentFilter.addAction("a");
this.registerReceiver(callReceiver,intentFilter);
}
@Override
public void onDestroy() {
super.onDestroy();
this.unregisterReceiver(callReceiver);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public class CallReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equalsIgnoreCase("a"))
Log.d("CallService", "onReceive: aa");
}
}
}
我在 onCreate 和 onStart 中为 Intent Filter 添加了 Action。注册后会进入onReceive方法,需要勾选Action。我对吗?但是在我的情况下没有 onReceive 方法 called.Anybody 帮助我找到更好的解决方案。
我的事情是你不能在服务上使用接收器,因为它通常使用你必须创建接收器来调用服务如果你想要稳定的更新服务你可以使用意图服务。
但是,如果您使用接收器进行系统更新,如 wifi off/on 等,您可以创建接收器并从清单中注册,这样它总是有效,您可以从它调用服务或在接收方法
中执行代码
我正在尝试在服务 class 中使用广播接收器。但是没有调用 Broadcast Receiver 中的 onReceive 方法。我不知道问题在哪里occurs.Below是我的代码,
public class CallService extends Service {
CallReceiver callReceiver=null;
String TAG=CallService.class.getSimpleName();
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
CallReceiver callReceiver=new CallReceiver();
IntentFilter intentFilter=new IntentFilter();
intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
intentFilter.addAction("a");
this.registerReceiver(callReceiver,intentFilter);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate: started in call service");
CallReceiver callReceiver=new CallReceiver();
IntentFilter intentFilter=new IntentFilter();
intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
intentFilter.addAction("a");
this.registerReceiver(callReceiver,intentFilter);
}
@Override
public void onDestroy() {
super.onDestroy();
this.unregisterReceiver(callReceiver);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public class CallReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equalsIgnoreCase("a"))
Log.d("CallService", "onReceive: aa");
}
}
}
我在 onCreate 和 onStart 中为 Intent Filter 添加了 Action。注册后会进入onReceive方法,需要勾选Action。我对吗?但是在我的情况下没有 onReceive 方法 called.Anybody 帮助我找到更好的解决方案。
我的事情是你不能在服务上使用接收器,因为它通常使用你必须创建接收器来调用服务如果你想要稳定的更新服务你可以使用意图服务。 但是,如果您使用接收器进行系统更新,如 wifi off/on 等,您可以创建接收器并从清单中注册,这样它总是有效,您可以从它调用服务或在接收方法
中执行代码