如何自动启动一个Android应用程序? [英] How to Autostart an Android Application?

查看:111
本文介绍了如何自动启动一个Android应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何将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>

这篇关于如何自动启动一个Android应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆