2013-05-06 90 views
0

我有一个服务,它由活动中的按钮运行。该服务注册一个广播接收器。我知道当onStartCommand方法启动时(bundle bundle = intent.getExtras()),从活动中获取数据的方式;Android - 如何将数据发送到服务的onDestroy方法

我的问题:如何在onDestroy方法启动时从活动中获取数据?换句话说:我想关注当调用onDestroy方法时用户选中的复选框,因为这些操作取决于复选框。

任何想法?

感谢

+0

东西听起来很奇怪。为什么服务关心复选框被检查?服务如何具有复选框?该信息是否不应该以意向方式发送?当你说“行动”你的意思是什么服务? – dmon 2013-05-06 02:50:39

+0

该服务可以注册(使用按钮)3个BroadcastReceivers(它取决于Activit中的3个复选框)。我在这个活动中也有一个“停止”按钮。当我点击右边的复选框并点击这个按钮时,我停止了服务,然后我在onDestroy方法中。我希望这个方法在服务类中知道我的活动中的复选框是否被选中。这更清楚吗?如您所说,我使用Intent将数据发送到onStartCommand方法。 stopservice方法在其声明中不包含意图。 – frontal 2013-05-06 03:08:36

回答

0

与其说stopService你可以调用startService并传入一个布尔“关机”等于要传递真实和其它信息,在onStartCommand您可以检查“关机”标志并在信息中处理另一个通行证,然后调用stopSelf()。示例代码

Intent intent = new Intent(this, MyService.class); 
intent.putExtra("shutdown", true); 
intent.putExtra(otherdata); 
startService(intent); 

在为MyService onStartCommand

@Override 
public int onStartCommand(Intent intent, int flags, int startId) 
{ 
     if (intent != null) 
     { 
      // get the data and process 
      if (intent.getBooleanExtra("shutdown", false); 
      { 
       stopSelf(); 
      } 
      } 
    return START_STICKY; 
} 
0

您可以在sharedperference保存数据,并检查它在你的服务ondestory。

相关问题