【问题标题】:How to start an app on boot completed in android?如何在启动时启动应用程序在android中完成?
【发布时间】:2021-09-26 14:51:13
【问题描述】:

我已经尝试了互联网上的所有解决方案。但他们都没有为我工作。你能检查我哪里做错了吗? 也许它不再适用于更高的 Api。 这是我的广播接收器类:

public class ReceiverToStartAnApp extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, Intent intent) {
        Toast.makeText(context, "Home Task 3 Version ", Toast.LENGTH_LONG).show();
        Intent myIntent = new Intent(context, CameraRecorderActivity.class);
        myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(myIntent);
    }
}

这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hometaskversion3">
    <uses-permission android:name="android.permission.RECORD_VIDEO" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:requestLegacyExternalStorage="true"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.HomeTaskVersion3">
        <activity
            android:name=".ui.CameraRecorderActivity"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".service.RecorderService" />
        <receiver
            android:name=".receiver.ReceiverToStartAnApp"
            android:directBootAware="true"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="1000">
                <action android:name="android.intent.action.BOOT" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

【问题讨论】:

标签: java android android-studio kotlin


【解决方案1】:

首先,从未以某种方式启动的应用程序不会接收系统广播。因此,在收到 ACTION_BOOT_COMPLETED 之前,您需要启动应用程序的一些活动。 其次,如 cmets 中所述,除非满足某些条件,否则不再允许从接收器开始活动。首选的方法是显示一个通知,其中包含您需要开始的活动的未决意图。这种方法对用户的干扰较小。

【讨论】:

    猜你喜欢
    • 2012-02-15
    • 2012-05-12
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 2013-10-22
    • 1970-01-01
    相关资源
    最近更新 更多