我希望在设备启动时启动应用程序,但即使在我添加了权限,intent-filters和category之后它也无法运行。
我知道在android 3.1之后,app无法在用户手动启动之前通过广播启动。
所以我在安装后运行了几次应用程序,但它仍然无效。
以下是我的代码。
表现:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
...
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver
android:name=".Receiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
和广播接收器类。
public class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = "action : " + intent.getAction();
Log.d("MyTag", action);
Toast.makeText(context, action, Toast.LENGTH_SHORT).show();
context.startActivity(new Intent(context, MainActivity.class));
}
}
通常,由于明显的合并问题,这往往会发生。
将两个intent过滤器告诉单独的接收器,您的代码应该再次工作。如果它仍然无效,请告诉我们:)