如何从BroadcastReceiver向正在运行的服务发送LocalBroadcast?

时间:2013-09-20 12:26:11

标签: java android android-service android-broadcast

我有一个使用外部应用程序触发的BroadcastReceiver,并成功触发其OnReceive方法。但我想从OnReceive向我的应用程序中的正在运行的服务发送另一个LocalBroadcast,问题是LocalBroadcastManager的SendBroadcast方法返回false并且不触发服务中的接收器。 android website中没有关于该布尔值的文档。我google了一下,发现这意味着没有注册的接收器。但我确信我已经在正在运行的服务中成功注册了intent,并且我已经将它作为Foreground服务来确保它正在运行。

另一个问题是,这是与BroadcastReceiver中正在运行的服务进行通信的最佳方式吗?

这是代码:

MyService.java

public class MyService extends Service {

private final IBinder mBinder = new OneSheeldBinder();

public static final String PLUGIN_MESSAGE = "PLUGIN_MESSAGE";
@Override
public void onCreate() {

    LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
              new IntentFilter(MyService.PLUGIN_MESSAGE));
            showNotification();
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    return START_REDELIVER_INTENT;
}

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

@Override
public void onDestroy() {
    hideNotifcation();
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
    super.onDestroy();
}

private void showNotification(){
    Notification.Builder build=new Notification.Builder(this);
    build.setSmallIcon(R.drawable.ic_launcher);
    build.setContentText("The service is running!");
    build.setContentTitle("The service is running!");
    build.setTicker("The service is running!");
    build.setWhen(System.currentTimeMillis());

    Notification notification=build.build();
    startForeground(1, notification);
}

private void hideNotifcation(){
    stopForeground(true);
}

 public class OneSheeldBinder extends Binder {
     public MyService getService() {
            return MyService.this;
        }

    }

 private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
     @Override
     public void onReceive(Context context, Intent intent) {
          Log.d("LocalBroadcast","Received!");
       }
     };

}

FireReceiver.java

public final class FireReceiver extends BroadcastReceiver
{

@Override
public void onReceive(final Context context, final Intent intent)
{
        Intent intent = new Intent();
        boolean test=LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
        Log.d("LocalBroadcast","Intent Fired!:"+test);//test is always false!
}


}

清单文件中的接收器和服务块

<receiver
        android:name=".FireReceiver"
        android:exported="true"
        android:process=":background"
        tools:ignore="ExportedReceiver" >
        <intent-filter>
            <action android:name="com.twofortyfouram.locale.intent.action.FIRE_SETTING" />
        </intent-filter>
</receiver>

    <service
        android:name=.MyService"
        android:exported="false" />

1 个答案:

答案 0 :(得分:2)

LocalBroadcastManager是流程的本地。无论出于何种原因,您选择使用两个不同的流程,因此LocalBroadcastManager将无效。

简单的解决方案是从android:process=":background"元素中删除<receiver>,因为它会浪费,不必要,并会干扰您对LocalBroadcastManager的使用。