从ADB安装后,静态BroadcastReceiver无法正常工作 [英] Static BroadcastReceiver not Working after Installation from ADB

查看:116
本文介绍了从ADB安装后,静态BroadcastReceiver无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个需要启动应用程序的项目上工作,该项目通过adb广播自定义的意图 com.example.demo.action.LAUNCH。

I working on a project that need to launch an app by broadcasting a customized intent "com.example.demo.action.LAUNCH" via adb.

我的计划是静态注册一个广播接收器 LaunchAppReceiver,该接收器在接收到自定义的意图时将启动该应用程序。

My plan is to statically register a broadcast receiver "LaunchAppReceiver" that will launch the app when receiving the customized intent.

我通过调用

adb install -r <pakcageName>

然后我通过致电

adb shell am broadcast -a com.example.demo.action.LAUNCH

但是,发送意图后没有任何反应。广播接收者似乎根本没有收到意图。我需要以某种方式实例化接收器才能接收到意图吗?

However, nothing happened after the intent was sent. It seemed the broadcast receiver didn't receive the intent at all. Do I need to somehow instantiate the receiver before it can receive the intent?

注意:由于Android设备是远程设备,因此我必须使用adb来处理安装和启动。

Note: Since the android device is remote, I have to use adb to handle the installation and the launch.

谢谢!

我宣布广播接收者如下

public class LaunchAppReceiver extends BroadcastReceiver{

    public LaunchAppReceiver () {}

    @Override
    public void onReceive(Context context, Intent intent) {

        Intent newIntent = new Intent(context, MainActivity.class);
        newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(newIntent);
    }
}

并将其静态注册到AndroidManifest.xml中。

and statically registered it in the AndroidManifest.xml.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.demo" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:enabled="true">
        <receiver
            android:name="com.example.demo.LaunchAppReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.demo.action.LAUNCH"/>
            </intent-filter>
        </receiver>
        <activity
            android:name=".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>
    </application>
</manifest>


推荐答案

最后得到了解决。

自从Honeycomb以来,所有新安装的应用程序将进入 STOP 阶段,直到至少启动一次。 Android为所有广播意图添加一个标志 FLAG_EXCLUDE_STOPPED_PACKAGES ,这将阻止它们到达已停止的应用程序。
http: //droidyue.com/blog/2014/01/04/package-stop-state-since-android-3-dot-1/

Since Honeycomb, all the freshly installed apps will get into a STOP stage until they are launched for at least one time. Android adds a flag "FLAG_EXCLUDE_STOPPED_PACKAGES" for all broadcast intents, which stops them from reaching the stopped apps. http://droidyue.com/blog/2014/01/04/package-stop-state-since-android-3-dot-1/

到要解决此问题,只需将标记 FLAG_INCLUDE_STOPPED_PACKAGES 添加到我们发送的意图中即可。就我而言,我将adb命令修改为

To solve this issue, just simply add the flag "FLAG_INCLUDE_STOPPED_PACKAGES" to the intents we send. In my case, I modify the adb command as

adb shell am broadcast -a com.example.demo.action.LAUNCH --include-stopped-packages

这篇关于从ADB安装后,静态BroadcastReceiver无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆