2011-04-21 90 views
1

我已经完全阅读了关于服务的文档,但对于如何使用/编码服务我仍然完全困惑。Android服务感到困惑

我试图做到: 我的主要活动: 用户选择了定时器的所有选项,然后点击一个按钮来启动一个定时器(在服务)与点击。我试图通过putExtra将选项传递给服务。

该服务将收集变量到服务使用的新变量中。

我有一个onCreate节调用我公开的计数器,这是我在服务中声明的,但不是在onCreate节中。我还声明了所有需要从这里的活动传递的选项中枚举的变量。

我是否需要绑定到服务才能真正通过putExtra传递变量?

我有一个onStart,这是我试图通过一系列获取来枚举变量值的地方。这由活动前面提到的按钮触发。里面的活动 - 实例:

mSet.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      //if toggled on 
      if (mEnableDisable.isChecked()){ 
       getCurrentTime(); 
       //start passing our variables to the service 
       Intent passvars = new Intent(getBaseContext(),TimerService.class); 
       passvars.putExtra("mHour24", mHour24); 
        //start the service now 
        startService(new Intent(TimerService.class.getName())); 
      } 
     } 
    }); 

里面的服务 - 例如:

public void onStart(Intent intent, int startId) { 
    super.onStart(intent, startId); 
    //Collecting data from Activity for calcs 
    Bundle getvars = intent.getExtras(); 
    mHour24 = getvars.getInt("mHour24"); 
      //start the counter now that I have my values 
      countDown.start(); 

}

另外,如果我能得到开始没有崩溃,我需要传递一个值的服务回到活动。所以我想我正在用putExtra在服务和getExtra中做这件事。再一次,我是否需要绑定到服务以获取新值?

要绑定,如果我需要我会做这个按钮按下。如果程序退出,我还会在主要活动的一部分中添加一段,但随后会恢复重新绑定并收集该值。我试图拉的价值是柜台剩余的时间。因此,在计数器服务中,我需要一个onTick将剩余时间放入一个变量中,并对该变量执行putExtra,在活动中,我希望收集该值并将其显示在UI中。

这是我第一次尝试服务,我不确定如何正确创建和使用。预先感谢所有的帮助,对于如此冗长的复杂帖子感到抱歉。

更新: 所以我现在有服务工作。我只是无法从服务中获取信息回到主要活动。

我添加了一个功能,试图取回从中按钮点击后推出该服务的数据启动服务:

startService(passvars); 
       //Init. variable for getDisplayInf() function to update the display with counter strings 
       Running = 1; 
       getDisplayInf(); 

而且getDisplayInf()例如:

public void getDisplayInf() { 
     //Intent stIntent = new Intent(MainActivity.this,MainActivity.class); 
     //Intent passvars = new Intent(MainActivity.this,Service.class); 
     Bundle getvars = getIntent().getExtras(); 
     String Remaining; 
     String CountDown; 
     do { 
      Remaining = getvars.getString("Remaining"); 
      CountDown = getvars.getString("CountDown"); 
      mRemaining.setText(Remaining); 
      mCountDown.setText(CountDown); 
      Running = getvars.getInt("Running"); 
     } 
     while (Running == 1); 
    }; 

计时器将完成时将Running变量设置为0。 只要我点击这个新功能的按钮就会崩溃应用程序。

在服务上,我在柜台上的onTick这样做:

  @Override 
     public void onTick(long millisUntilFinished) { 
      Running = 1; 
      Remaining = "Time Remaining: "; 
      CountDown = formatTime(millisUntilFinished); 
      ticker(); 
     } 

股票代码:

public void ticker() { 
     Intent passvars = new Intent(Service.this,MainActivity.class); 
     //this is for the activity to keep looping to grab the countdown values while running this timer 

     //now send it to the activity 
     passvars.putExtra("Running", Running); 
     //String Values to be passed to the activity 

     //now send it to the activity 
     passvars.putExtra("mRemaining", Remaining); 


     //now send it to the activity 
     passvars.putExtra("mCountDown", CountDown); 
    }; 
+0

查看[this](http://stackoverflow.com/questions/3197335/android-restful-api-service/3197456#3197456)对服务/活动“通信”用例的回答。 – FrVaBe 2011-04-21 15:59:00

+1

'startService(new Intent(TimerService.class.getName()));' 你创建一个新的Intent,当你创建一个叫做passvars的。请尝试使用那个!另外,您应该将'getBaseContext()'改为'this'(或者例如'MainActivity.this') – 2011-04-21 16:03:12

回答

0

我意识到我可以通过使用通知管理器来获取用户想要的意识量。所以我实现了通知管理器,并在那里显示我的倒计时信息()。

这对我来说最适合我的原因是因为我的服务在技术上是一个远程服务(安装在自己的进程中运行,它在清单文件中定义)。连接到远程服务以及远程服务同步获取数据(这正是我想要的。)通知管理器选项在我这里最适合我,因为该程序停靠图标并在每秒更新通知。

1

尝试使用passvars意图开始您的服务,而不是创建一个新的第二意图:

Intent passvars = new Intent(MainActivity.this,TimerService.class); 
passvars.putExtra("mHour24", mHour24); 
//start the service now 
startService(passvars); 
+1

另外,就像我上面提到的那样,您应该更改'getBaseContext() )''this'或'MainActivity.this' – 2011-04-21 16:04:57

+0

+1对,谢谢。 – djg 2011-04-21 16:17:43

+0

这工作!我将启动计数器的函数移到onStart部分,并返回了我的countDown.start();到函数的结尾。我这样做是因为当我第一次测试了 .this的意图时,如上所述,它没有计数,而是在倒数结束时做了它应该做的事情。 – Andrew 2011-04-21 17:36:19

0

如果您的活动想要从您的Serv中获取一个值冰,你有几个选择:

  1. 发送一个意图从活动到服务,并使用ResultReceiver异步获取结果。 Claszen给出的链接非常好。

  2. 使用活页夹。您可以创建一个与结果一起返回的同步调用。

  3. 实施内容提供者。这种选择是为了制作自己的数据检索和存储组件。

+0

我将尝试其中一个选项,以查看是否可以获得我需要的显示。我会发布我找到的。 – Andrew 2011-04-21 17:37:04

+0

我有一些进一步的问题将结果返回到主要活动中,我将作为答案张贴,以便我可以添加更多详细信息。 – Andrew 2011-04-21 20:03:07

+0

@djg @Jon更新了问题。几乎得到我想要的。只是无法从UI更新服务中获取数据。 – Andrew 2011-04-21 20:39:41