问题:在重启后启动的应用程序编写困难。

huangapple go评论81阅读模式
英文:

Problem writing an app that can startup after reboot

问题

AndroidManifest.xml

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

    <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/Theme.HelloWorld">
        <activity android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".StartActivityOnBootReceiver"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

StartActivityOnBootReceiver.java

public class StartActivityOnBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Intent i = new Intent(context, MainActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
            Log.d("Receiver", "Received");
        }
    }
}

一开始,我以为是因为StartActivityOnBootReceiver没有接收到"Intent.ACTION_BOOT_COMPLETED"的操作,所以我添加了"Log.d("Receiver", "Received");"

以下是Logcat的结果
问题:在重启后启动的应用程序编写困难。

所以,StartActivityOnBootReceiver接收到了操作,但是它没有启动应用程序。我做错了什么吗?

英文:

I am trying to auto start an app after rebooting the phone. After looking into a few tutorials, none of the codes starts the app after rebooting the phone, and I have tested if StartActivityOnBootReceiver.java have received the action. This is my code

AndroidManifest.xml

?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    package=&quot;com.example.helloworld&quot;
    &gt;

    &lt;uses-permission android:name=&quot;android.permission.RECEIVE_BOOT_COMPLETED&quot; /&gt;

    &lt;application
        android:allowBackup=&quot;true&quot;
        android:icon=&quot;@mipmap/ic_launcher&quot;
        android:label=&quot;@string/app_name&quot;
        android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
        android:supportsRtl=&quot;true&quot;
        android:theme=&quot;@style/Theme.HelloWorld&quot;&gt;
        &lt;activity android:name=&quot;.MainActivity&quot;
            android:exported=&quot;true&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;

        &lt;receiver android:name=&quot;.StartActivityOnBootReceiver&quot;
            android:enabled=&quot;true&quot;
            android:exported=&quot;true&quot;
            android:permission=&quot;android.permission.RECEIVE_BOOT_COMPLETED&quot;
            &gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.BOOT_COMPLETED&quot; /&gt;
                &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/receiver&gt;

    &lt;/application&gt;

&lt;/manifest&gt;

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

StartActivityOnBoostReceiver.java

public class StartActivityOnBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Intent i = new Intent(context, MainActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
            Log.d(&quot;Receiver&quot;, &quot;Received&quot;);
        }
    }
}

At first, I thought it is due to StartActivityOnBootReceiver haven't gotten the action of Intent.ACTION_BOOT_COMPLETED, so I added Log.d(&quot;Receiver&quot;, &quot;Received&quot;);

And this is the result of the logcat
问题:在重启后启动的应用程序编写困难。

So, StartActivityOnBootReceiver have received the action, but it didn't turn on the app. Is there something I have done wrong?

答案1

得分: 0

最新版本的Android中,除非您具有root权限,或者甚至将您的应用标记为必要服务(一些默认应用由公司标记为必要服务),否则我认为这是不可能的,我认为通常情况下不可能。

英文:

With latest version of androids, I think this is not possible, unless you have root access or maybe even mark your app as a necessary service (some default apps are marked so by the company), i don't think it's possible normally

huangapple
  • 本文由 发表于 2023年5月31日 23:58:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定