【问题标题】:Why isn't my BroadcastReceiver starting on the Android Emulator?为什么我的 BroadcastReceiver 没有在 Android 模拟器上启动?
【发布时间】:2016-10-20 16:21:09
【问题描述】:

我是 Android 开发的新手,我正在尝试创建一个简单的“概念验证应用程序”,它将作为后台服务运行。我正在尝试将 IntentService 与 BroadcastReceiver 一起使用来启动该过程(目前在启动期间,有时我可能会将其切换到 Screen on / user present)。

我在 Android Studio 中创建了一个没有任何活动的新项目。然后我添加了以下 Java 文件并对 AndroidManifest.xml 进行了以下更改。

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.circlesquires.netcountable.netcountable">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <service android:name=".SnapshotService" android:exported="true">
        </service>
        <receiver android:name=".ServiceStarter" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

ServiceStarter.java

package com.circlesquires.netcountable.netcountable;

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

/**
 * Created by camha on 6/18/2016.
 */
public class ServiceStarter extends BroadcastReceiver{
    static final String ACTION = "android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("output", "onReceive occured!");

        if(intent.getAction().equals(ACTION)) {
            Intent serviceIntent = new Intent(context, SnapshotService.class);

            context.startService(serviceIntent);
        }
    }
}

SnapshotService.java

package com.circlesquires.netcountable.netcountable;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

/**
 * Created by camha on 6/18/2016.
 */
public class SnapshotService extends IntentService {

    public SnapshotService() {
        super("SnapshotService");
    }

    @Override
    protected void onHandleIntent(Intent workIntent) {
        while(true) {

            Log.i("output", "I'm running!");

            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

我将应用程序部署到模拟器,确保单击“调试”按钮。但是我从来没有看到 logcat 中输出的任何日志。

我确定我做错了什么——帮我弄清楚是什么:D。谢谢!

【问题讨论】:

  • 您的应用需要有一个Activity,您在安装后至少启动一次以使其脱离已停止状态。在此之前,您的启动接收器将不会收到广播(从 API 3.1 开始)。
  • 好的,如果我添加一个活动,它只需要运行一次(不管手机是否重启等?)
  • 是的,安装后基本上需要运行一次。但是,它可以恢复到 stopped 状态 - 例如,如果用户从设置中强行停止您的应用程序 - 在这种情况下,他们必须再次运行它才能让您的启动接收器工作。但是,简单地重新启动不会让它回到 stopped 状态。

标签: java android android-intent background-service


【解决方案1】:
 if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
        Intent i = new Intent(context, SnapshotService.class);
        context.startService(i);
    }

直接与意图值比较而不是制作字符串 还有一件事

在 Android 3.1 及更高版本上,用户必须先启动您的一项活动,然后任何清单注册的 BroadcastReceiver 才能工作。 检查这个https://stackoverflow.com/a/17067671/3288890

【讨论】:

  • 好点,但是 Log.i("output", "onReceive 发生了!");永远不会发生。
  • 你在手机上试过了吗
【解决方案2】:

android.intent.action.BOOT_COMPLETED 顾名思义,是在设备启动时间之后发送,但在设备启动时间之后发送。您要重新启动设备吗?

您可以直接通过 adb 发送系统广播,而不是重新启动设备来测试您的应用程序:

./adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -n <your.package.name>/.<YourReceiverClass>

您可以找到更多信息here

【讨论】:

    猜你喜欢
    • 2011-08-23
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 2015-06-14
    • 1970-01-01
    • 2017-07-31
    • 2012-12-27
    相关资源
    最近更新 更多