I am using an IntentService
to call on a webservice.
我正在使用IntentService来调用Web服务。
How I am calling to start the intent service:
我如何打电话来启动意向服务:
if (isMyConServiceRunning(TaskService.class,context) == false) {
Log.d(TAG,"STARTING TaskService");
context.startService(TasksIntentUploadDownload);
}
isMyConServiceRunning method:
private static boolean isMyConServiceRunning(Class<?> serviceClass,Context context)
{
try {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().toLowerCase(Locale.ENGLISH).contains(service.service.getClassName().toLowerCase(Locale.ENGLISH))) {
return true;
}
}
}
catch(Exception e)
{}
return false;
}
isMyConServiceRunning
is always returning false
(the service is not running) and thus service is being started multiple times but I noticed that when I override the method OnStartCommand
and place the webservice call in OnStartCommand
and not in OnHandleIntent
and making OnStartCommand
return START_STICKY
, then isMyConServiceRunning
will recognize that the service is running.
isMyConServiceRunning总是返回false(服务没有运行)因此服务正在多次启动但是我注意到当我覆盖方法OnStartCommand并将webservice调用放在OnStartCommand而不是OnHandleIntent并使OnStartCommand返回START_STICKY时,isMyConServiceRunning将认识到该服务正在运行。
Is it bad to run tasks in OnStartCommand
?
在OnStartCommand中运行任务是不是很糟糕?
If yes, how can I fix the multiple calls to run IntentService
?
如果是,我如何修复多次调用以运行IntentService?
1 个解决方案
#1
A better way would be:
更好的方法是:
in your service class:
在您的服务类中:
private void sendMessageStart() {
Intent intent = new Intent("Service-Started");
intent.putExtra("message", "This is my message!");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
private void sendMessageStop() {
Intent intent = new Intent("Service-Stopped");
intent.putExtra("message", "This is my message!");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
in the class you want to check if the service is running or not:
在类中,您要检查服务是否正在运行:
private BroadcastReceiver mMessageReceiverStart = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("message");
}
};
private BroadcastReceiver mMessageReceiverStop = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("message");
}
}
};
#1
A better way would be:
更好的方法是:
in your service class:
在您的服务类中:
private void sendMessageStart() {
Intent intent = new Intent("Service-Started");
intent.putExtra("message", "This is my message!");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
private void sendMessageStop() {
Intent intent = new Intent("Service-Stopped");
intent.putExtra("message", "This is my message!");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
in the class you want to check if the service is running or not:
在类中,您要检查服务是否正在运行:
private BroadcastReceiver mMessageReceiverStart = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("message");
}
};
private BroadcastReceiver mMessageReceiverStop = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("message");
}
}
};