broadcastreceiver - 安卓启动应用程序使用多个服务来启动,

  显示原文与译文双语对照的内容
132 2

我需要在bootcompleted上启动 2个服务。 第一个服务启动正确,但是第二个服务似乎没有。 我不知道我是否必须创建两个栏,或者它已经足够了。 这是我的代码。我把这两个服务放在一个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;
 }
}

时间: 原作者:

91 4

我发现了这个错误。


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

原作者:
...