我正在研究一个需要通过adb广播自定义意图“ com.example.demo.action.LAUNCH”来启动应用程序的项目。
我的计划是静态注册一个广播接收器“ LaunchAppReceiver”,该接收器在接收到自定义的意图时将启动应用程序。
我通过调用安装了.apk
adb install -r <pakcageName>
然后我打电话给我
adb shell am broadcast -a com.example.demo.action.LAUNCH
但是,在发送意图之后,什么也没有发生。广播接收者似乎根本没有收到意图。我需要以某种方式实例化接收器才能接收到意图吗?
注意:由于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开始,所有新安装的应用程序将进入STOP阶段,直到至少启动一次。Android为所有广播意图添加一个标志“ FLAG_EXCLUDE_STOPPED_PACKAGES ”,这将阻止它们到达已停止的应用程序。http://droidyue.com/blog/2014/01/04/package-stop-state-since-android-3-dot-1/
要解决此问题,只需将标记“ FLAG_INCLUDE_STOPPED_PACKAGES ”添加到我们发送的意图中即可。就我而言,我将adb命令修改为
adb shell am broadcast -a com.example.demo.action.LAUNCH --include-stopped-packages
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句