开启 - >通知。通知关闭 - >开关必须关闭

时间:2014-11-15 06:45:58

标签: android service notifications

我有一个活动(比如说...... MainActivity.java)。在那次活动中,我有一个开关。当我打开该开关时,它将启动服务和通知。在那个通知中我添加了动作(),以便我可以通过通知终止服务,当然我的开关必须关掉。

我的解决方案:我创建了一个接口(名为IServiceKilled),让我的MainActivity实现它。所以当服务被杀时,我的开关也会关闭。我的代码:

MainActivity.java

     public class MainActivity extends ActionBarActivity implements IServiceKilled
     {
        //start service and let my activity implement the interface
        startService(new Intent(MainActivity.this,
                            AppLaunchCheckService.class));
        AppLaunchCheckService.mServiceKilled = MainActivity.this;
     ........
     @Override
     public void onServiceKilled() {
       //service is killed, switch should be off too
       swtCheck.setChecked(false);
     }
    }

AppLaunchCheckService

    public static IServiceKilled mServiceKilled = null;
    ......
   @Override
   public void onDestroy() {
    super.onDestroy();
    if (mServiceKilled != null) {
        mServiceKilled.onServiceKilled();
        mServiceKilled=null;
    }        
}

我上面所做的工作非常好!没问题!现在我有一个侦听Intent.ACTION_BOOT_COMPLETED的BroadcastReceiver并启动该服务。 现在这里遇到问题!!!!!!! 我怎样才能把AppLaunchCheckService.mServiceKilled = MainActivity.this;在接收器中,以便开关应该关闭。这是我的广播接收器:

MyTestingBroadcastReceiver

    @Override
public void onReceive(final Context context, Intent intent) {
  String action = intent.getAction();
  if (action != null) {
     .......
      else if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
          context.startService(new Intent(context,
                        AppLaunchCheckService.class));
          AppLaunchCheckService.mServiceKilled = MainActivity.this;// error here
      }
   }
}

0 个答案:

没有答案