如何处理循环服务?
How to deal with a loop Service?
我的 Android 应用程序正在激活调用 'Activity
的 service
。
在 mainActivity 上:
startService(new Intent(getBaseContext(),MyService.class));
然后在服务上:
public int onStartCommand(Intent intent,int flage,int startId){
// Toast.makeText(this, "Yes please", Toast.LENGTH_LONG).show();
Intent mIntent=new Intent(MyService.this,trackingActivity.class);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(mIntent);
return START_STICKY; }
在 trackingActivity 的末尾,这行写成(再次):
startService(new Intent(getBaseContext(),MyService.class));
这创造了很多 Services
。有没有更好的方法来创建一个总是重复自身的后台服务,而不是每次都创建一个新的 Service
?
我试图在 Activity
中做一个 while 循环:
while(true){
Actions on activity
}
但是没有成功。
Context.startService(Intent)
不会为每次调用创建新的 Service
。
如果已经有一个匹配的服务 运行,它会将意图传递给那个 运行 服务,但不会每次都创建一个新的。
请参阅 Android 开发者文档 Context.startService(Intent):
If this service is not already running, it will be instantiated and
started (creating a process for it if needed); if it is running then
it remains running.
Every call to this method will result in a corresponding call to the target service's onStartCommand(Intent, int, int) method, with the intent given here.
Android 开发者文档中有关于 Starting a Service 的类似信息:
[When starting a service using an intent] the startService() method returns immediately and the Android system
calls the service's onStartCommand() method. If the service is not
already running, the system first calls onCreate(), then calls
onStartCommand().
...
Multiple requests to start the service result in multiple
corresponding calls to the service's onStartCommand(). However, only
one request to stop the service (with stopSelf() or stopService()) is
required to stop it.
我的 Android 应用程序正在激活调用 'Activity
的 service
。
在 mainActivity 上:
startService(new Intent(getBaseContext(),MyService.class));
然后在服务上:
public int onStartCommand(Intent intent,int flage,int startId){
// Toast.makeText(this, "Yes please", Toast.LENGTH_LONG).show();
Intent mIntent=new Intent(MyService.this,trackingActivity.class);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(mIntent);
return START_STICKY; }
在 trackingActivity 的末尾,这行写成(再次):
startService(new Intent(getBaseContext(),MyService.class));
这创造了很多 Services
。有没有更好的方法来创建一个总是重复自身的后台服务,而不是每次都创建一个新的 Service
?
我试图在 Activity
中做一个 while 循环:
while(true){
Actions on activity
}
但是没有成功。
Context.startService(Intent)
不会为每次调用创建新的 Service
。
如果已经有一个匹配的服务 运行,它会将意图传递给那个 运行 服务,但不会每次都创建一个新的。
请参阅 Android 开发者文档 Context.startService(Intent):
If this service is not already running, it will be instantiated and started (creating a process for it if needed); if it is running then it remains running.
Every call to this method will result in a corresponding call to the target service's onStartCommand(Intent, int, int) method, with the intent given here.
Android 开发者文档中有关于 Starting a Service 的类似信息:
[When starting a service using an intent] the startService() method returns immediately and the Android system calls the service's onStartCommand() method. If the service is not already running, the system first calls onCreate(), then calls onStartCommand().
...
Multiple requests to start the service result in multiple corresponding calls to the service's onStartCommand(). However, only one request to stop the service (with stopSelf() or stopService()) is required to stop it.