1

我需要在启动完成时启动 2 个服务。第一个服务正确启动,但第二个服务似乎没有启动。我不知道我是否必须创建两个 BroadcastReceiver 或者一个就足够了。这是我的代码。我已将这两个服务放在一个 BroadcastReceiver 中。拜托,你能告诉我我做错了什么吗?

先感谢您

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pruebas.appservicelocator"
    android:versionCode="1"
    android:versionName="1.0"
    android:installLocation="internalOnly"  >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <!-- Startup service -->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <!-- GPS -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <!-- UUID -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <!-- Acceso a web service -->
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.pruebas.appservicelocator.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".Servicio">
            <intent-filter>
                <action android:name="com.pruebas.appservicelocator.Servicio"/>
            </intent-filter>
        </service> 

        <service android:name=".ServicioBD">
            <intent-filter>
                <action android:name="com.pruebas.appservicelocator.ServicioBD"/>
            </intent-filter>
        </service> 

        <receiver android:name=".Recibidor" android:enabled="true" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

Recibidor.java:

package com.pruebas.appservicelocator;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class Recibidor extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        android.os.Debug.waitForDebugger(); 
        Toast.makeText(context, "Iniciando Recibidor", Toast.LENGTH_LONG).show();
        final String TAG = "Recibidor";
        Log.i(TAG, "Iniciando Recibidor");

        if (intent.getAction().equalsIgnoreCase("android.intent.action.BOOT_COMPLETED")) {
            Toast.makeText(context, "Iniciando Intent", Toast.LENGTH_LONG).show();
            Log.i(TAG, "Iniciando Intent");

            Intent servicio = new Intent();
            servicio.setAction("com.pruebas.appservicelocator.Servicio");
            context.startService(servicio);

            Intent servicioBD = new Intent();
            servicio.setAction("com.pruebas.appservicelocator.ServicioBD");
            context.startService(servicioBD);

            Log.i(TAG, "Iniciando Servicios");
            Toast.makeText(context, "Iniciando Servicio", Toast.LENGTH_LONG).show();
         } 
    }

}

“Servicio”服务工作正常,所以我不写代码。如果你需要,请告诉我,我会写的。

ServicioBD.java:

package com.pruebas.appservicelocator;

import com.pruebas.utils.UsersLocationsDBHelper;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class ServicioBD extends Service{

    private UsersLocationsDBHelper locDBHelper = null;
    private static String TAG = "ServicioBD";

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "SERVICIOBD ON CREATE", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        startForeground(0, null);

        Toast.makeText(this, "SERVICIOBD ON START COMMAND", Toast.LENGTH_LONG).show();


        return START_STICKY;
    }
}
4

1 回答 1

2

我发现了错误。

  Intent servicio = new Intent();
  servicio.setAction("com.pruebas.appservicelocator.Servicio");
  context.startService(servicio);

  Intent servicioBD = new Intent();
  servicio.setAction("com.pruebas.appservicelocator.ServicioBD");
  context.startService(servicioBD);

必须

  Intent servicio = new Intent();
  servicio.setAction("com.pruebas.appservicelocator.Servicio");
  context.startService(servicio);

  Intent servicioBD = new Intent();
  servicioBD.setAction("com.pruebas.appservicelocator.ServicioBD");
  context.startService(servicioBD);
于 2013-04-07T11:27:21.483 回答