Android 从ADB安装后静态广播接收器不工作
我正在做一个项目,需要通过adb广播一个定制的意图“com.example.demo.action.launch”来启动一个应用程序 我的计划是静态注册一个广播接收器“LaunchAppReceiver”,当接收到定制的意图时,它将启动应用程序 我通过调用安装了.apkAndroid 从ADB安装后静态广播接收器不工作,android,android-intent,broadcastreceiver,adb,Android,Android Intent,Broadcastreceiver,Adb,我正在做一个项目,需要通过adb广播一个定制的意图“com.example.demo.action.launch”来启动一个应用程序 我的计划是静态注册一个广播接收器“LaunchAppReceiver”,当接收到定制的意图时,它将启动应用程序 我通过调用安装了.apk adb install -r <pakcageName> 然而,在发送意图后,什么也没有发生。广播接收器似乎根本没有收到目的。我是否需要以某种方式实例化接收者,然后它才能接收意图 注意:由于android设备是远程
adb install -r <pakcageName>
然而,在发送意图后,什么也没有发生。广播接收器似乎根本没有收到目的。我是否需要以某种方式实例化接收者,然后它才能接收意图
注意:由于android设备是远程设备,我必须使用adb来处理安装和启动
谢谢
我宣布广播接收机如下
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中静态注册它
<?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发布以来,所有新安装的应用程序都将进入“停止”阶段,直到它们至少启动一次。Android为所有广播意图添加了一个标志“flag\u EXCLUDE\u STOPPED\u PACKAGES”,这将阻止它们访问停止的应用程序。 要解决这个问题,只需在我们发送的意向中添加标志“flag\u INCLUDE\u STOPPED\u PACKAGES”。在本例中,我将adb命令修改为
adb shell am broadcast -a com.example.demo.action.LAUNCH --include-stopped-packages
似乎您需要至少运行一次应用程序才能在系统中注册广播。是的,这似乎是真的。。。现在的情况是,我计划从广播接收器启动应用程序,但需要运行应用程序才能注册。。。陷入困境。嗯。。。我认为那应该行得通。。您在日志上看到任何错误吗?LaunchAppReceiver是否在正确的包(com.example.demo.LaunchAppReceiver)中?最终解决了这个问题。请看下面我的回答:)
adb shell am broadcast -a com.example.demo.action.LAUNCH --include-stopped-packages