我有两个关于 Android 服务的问题。
首先,我想知道哪个计算成本更低?线程还是服务?
其次,在我的主要 android 应用程序中,我尝试触发 2 个服务:
Intent intent = new Intent("android.intent.action.MAIN");
intent.setComponent(ComponentName
.unflattenFromString("com.mrlite.service1"));
intent.addCategory("android.intent.category.LAUNCHER");
startService(intent);
intent.setComponent(ComponentName
.unflattenFromString("com.mrlite.service2"));
intent.addCategory("android.intent.category.LAUNCHER");
startService(intent);
服务1:
package com.mrlite.service1;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class Service1Activity extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
//Intent intent = new Intent("android.intent.action.MAIN");
//String value = getIntent().getExtras().getString("1")
Log.e(getClass().getSimpleName(), "Service 1 Started");
}
@Override
public void onDestroy()
{
super.onDestroy();
Log.e(getClass().getSimpleName(), "Service 1 Destroyed");
}
}
服务二:
package com.mrlite.service2;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class Service2Activity extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
// Intent intent = new Intent("android.intent.action.MAIN");
// String value = getIntent().getExtras().getString("1")
Log.e("Service 2", "Service 2 Started");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("Service 2", "Service 2 Destroyed");
}
}
每当我尝试执行此代码时,只会调用服务 1。服务 2 永远不会被调用。
如何同时触发 2 个服务?