Android启动服务从未启动。

5

我是Android编程的新手。我有一个接收器,在开机时启动服务,但似乎从未启动过。您能告诉我我做错了什么吗?

我不知道如何调试它。您能向我解释如何调试Android启动服务吗?

这是我的代码。谢谢您提前。

Receiver.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>

你在 Recibidor 中收到了意图吗? - Entreco
嗨Entreco。你能告诉我如何知道我是否收到了意图吗? - eloweyn
你可以在 onReceiver 方法中添加日志记录。 - tundundun
如何重新启动模拟器以使接收启动完成事件并执行我的服务?在Eclipse中,我运行服务并启动模拟器,但我认为事件是在我的服务安装之前接收到的。 - eloweyn
3个回答

4
您所发布的代码将无法在较新版本的Android上运行。为了防止恶意软件,在较新版本的Android中,除非用户手动从应用程序启动器中启动您的应用程序,否则不可能自动在清单中注册BroadcastReceiver
您需要创建一个带有MAIN/LAUNCHER <intent-filter>条目的Activity。一旦用户手动启动应用程序一次,您的BroadcastReceiver的清单注册将发生,并且它将保持注册状态,除非用户使用设置的“应用程序管理”部分中的“强制停止”。

1

android:installLocation="internalOnly" 表示应用仅安装在内部存储中。要接收任何广播,您必须先运行一次该应用程序。请确保也在其他地方记录接收器。 - Athul Harikumar
该应用程序具有android:installLocation="internalOnly"标签。它安装在内部存储中。 - eloweyn

0

确保将您的服务和广播接收器放在根包内。或者只需使用完全限定名称(带有包名)。 - tundundun

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接