我正在开发一款Android应用程序,可在应用程序运行时跟踪背景中的一些内容。我是Android的初学者,所以我会尽力描述我想要做的事情。Android服务在10秒后关闭
的设置: MainActivity,TrackerActivity,TrackerService(该服务包含一个激活锁定)和TrackerServiceWakeupReciever
这将运行时TrackerService是闭合。
public class TrackerServiceWakeupReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.example.android.TrackerService");
serviceIntent.putExtra("action", "checkSound");
context.startService(serviceIntent);
}
}
从TrackerActivity启动服务的代码:
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.example.android.TrackerService");
startService(serviceIntent);
当进入应用程序在MainActivity开始,那么当TrackerActivity会从在MainActivity开始TrackerService获取TrackerActivity的OnCreate开始。该服务正好运行10秒并关闭。然后TrackerServiceWakeupReciever再次启动该服务,并再次运行10秒a并循环。
它为什么只在10秒内运行?我注意到,如果我快速回到MainActivity并按下睡眠按钮,服务将继续运行。即使当设备退出睡眠状态,服务仍在运行,正如我希望的那样。
如何解决这个问题?已经用了一个星期的时间弄清楚了。我想这是simpel :)
在此先感谢。 Martin
你是否想让你的服务停留更长时间或说一段时间? – Rohit 2012-06-01 09:27:40