如何自动启动Android应用程序? [英] How to Auto-start an Android Application?

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

问题描述

我不确定在android模拟器完成启动后如何自动启动android应用程序.有人有任何代码片段对我有帮助吗?

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);
}}

然后,将Receiver类添加到清单文件中:

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天全站免登陆