如何自动启动一个Android应用程序?应用程序、自动启动、Android

2023-09-12 21:43:36 作者:谢谢你,赐我万劫不复

我不知道如何将Android模拟器完成其启动后自动启动一个Android应用程序。没有人有任何code片段,这将帮助我吗?

I'm not sure how to autostart an android application after the android emulator completes its booting. Does anyone have any code snippets that will help me?

推荐答案

您必须添加一个明显的权限项:

You have to add a manifest permission entry:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

(当然,你应该列出所有其他权限,您的应用程序使用)。

(of course you should list all other permissions that your app uses).

然后,实施BroadcastReceiver的类,它应该是简单而快速的可执行文件。最好的办法是在这个接收器设置闹钟唤醒你的服务(如果它没有必要保持运行强麦时间为Prahast写)。

Then, implement BroadcastReceiver class, it should be simple and fast executable. The best approach is to set an alarm in this receiver to wake up your service (if it's not necessary to keep it running ale the time as Prahast wrote).

public class BootUpReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context, MyService.class), PendingIntent.FLAG_UPDATE_CURRENT);
    am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + interval, interval, pi);
}}

然后,接收器类添加到您的清单文件:

Then, add a Receiver class to your manifest file:

    <receiver android:enabled="true" android:name=".receivers.BootUpReceiver"
        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>