0

I have two problems regarding Android Services.

First, I would like to know which is less computationally expensive? Threads or Services?

Secondly, In my main android application I have tried to trigger 2 services:

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);

Service1:

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");
    }
}

Service 2:

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");
    }
}

Whenever I try to execute this code, only service 1 is called. Service 2 is never called.

How to trigger 2 services simultaneously?

4

1 に答える 1

1

そのようにサービスを開始する理由はありますか?このようにしないのはなぜですか:

Intent intent = new Intent(this, Service1Activity.class);
startService(intent);

intent = new Intent(this, Service2Activity.class);
startService(intent);

おそらくあなたがやっているように機能しない理由は、android.intent.action.MAIN1 つのクラスにしか割り当てることができないためです。

于 2012-05-27T05:56:46.227 に答える