Android启动服务永远不会启动

时间:2013-03-25 08:24:20

标签: android service broadcastreceiver startup

我是Android编程的新手。 我有一个接收器在启动时启动服务但似乎从未启动过。你能告诉我我做错了什么吗? 我不知道如何调试它。你能解释一下我如何调试Android启动服务吗?

这是我的代码。提前谢谢

Recibidor.java:

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) {
      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.Servicio");
         context.startService(servicio);

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

   }
}

Servicio.java

package com.pruebas;

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

public class Servicio extends Service {
   private final String TAG = "Servicio";


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

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

   @Override
   public void onDestroy() {
      super.onDestroy();
      Log.i(TAG, "ON DESTROY");
      Toast.makeText(this, "ON DESTROY", Toast.LENGTH_LONG).show();
   }

   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
      startForeground(0, null);
      Log.i(TAG, "ON START COMMAND");
      Toast.makeText(this, "ON START COMMAND", Toast.LENGTH_LONG).show();
      return START_STICKY;
   }
}

的AndroidManifest.xml

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

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

   <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <application>
        <service android:name=".Servicio">
            <intent-filter>
                <action android:name="com.pruebas.Servicio"/>
            </intent-filter>
        </service> 

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

    </application>

</manifest>

3 个答案:

答案 0 :(得分:4)

您发布的代码永远不会适用于更高版本的Android。为了防止恶意软件,使用更高版本的Android,无法在用户从应用启动器手动启动应用的清单 UNTIL 中自动注册BroadcastReceiver

您需要使用MAIN / LAUNCHER Activity条目创建<intent-filter>。用户手动启动应用程序一次后,BroadcastReceiver的清单注册将会发生,并且除非用户使用“设置”的“管理应用程序”部分中的“强制停止”,否则它将保持注册状态。

答案 1 :(得分:1)

应用程序需要安装在内部存储中才能接收BOOT_COMPLETED意图。请参阅此问题:My BroadcastReceiver is not receiving the BOOT_COMPLETED intent after my N1 boots. Help Please!

答案 2 :(得分:0)

尝试移动

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

超出“接收者”标签。

http://developer.android.com/guide/topics/manifest/manifest-intro.html#perms