【问题标题】:Android IntentService won't start on bootAndroid IntentService 不会在启动时启动
【发布时间】:2013-05-21 13:44:32
【问题描述】:

我有一个 IntentService,它在启动时启动并在后台保持运行。没有启动器 Activity,IntentService 由广播接收器调用。它适用于 2.3.4 和 4.0.4 设备,但不适用于 4.2.2。也许我的广播接收器没有在设备上被触发?

清单:

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

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <receiver android:name="br.com.xxx.RastreadorBroadcastReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <service
            android:name="br.com.xxx.ObtainAndPostLocationService"
            android:exported="true">
        </service>

    </application>

</manifest>

RastreadorBroadcastReceiver:

package br.com.xxx;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class RastreadorBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startServiceIntent = new Intent(context, ObtainAndPostLocationService.class);
        context.startService(startServiceIntent);
    }
}

【问题讨论】:

  • 您的接收方是否获得了 Intent?我是说,你的 onReceive 方法会被调用吗?
  • 我无法调试验证,因为它不是一个活动。如何检查?
  • 在创建 startServiceIntent 之前添加: Log.d("DEBUG", "Creating the intent");并查看此字符串是否写入 logcat
  • 为了使 Log.d() 工作,调试器必须附加到进程,在这种情况下不会发生。我按照stackoverflow.com/questions/4008081/debugging-a-service 上的说明进行操作,但调试器似乎也没有附加。
  • 没有。要使用 Log 类,您不需要附加调试器。你在使用 Eclipse 吗?如果是这样,打开 DDMS 透视图,您将获得 logcat 视图。里面有所有的日志。 Log.d 只是将一个字符串写入 logcat

标签: android android-intent broadcastreceiver boot intentservice


【解决方案1】:

尝试改变

&lt;receiver android:name="br.com.xxx.RastreadorBroadcastReceiver" &gt;

<receiver android:name=".RastreadorBroadcastReceiver" >

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多