如何自动启动Android应用程序?

时间:2009-06-29 04:36:16

标签: android

我不确定如何在android模拟器完成启动后自动启动Android应用程序。有没有人有任何可以帮助我的代码片段?

4 个答案:

答案 0 :(得分:46)

您必须添加清单权限条目:

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

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

然后,实现BroadcastReceiver类,它应该是简单快速的可执行文件。最好的方法是在此接收器中设置一个警报以唤醒您的服务(如果没有必要像Prahast写的那样保持它运行)。

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类添加到清单文件中:

    <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>

答案 1 :(得分:22)

修改您的AndroidManifest.xml以添加RECEIVE_BOOT_COMPLETED权限

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

编辑AndroidManifest.xml申请表部分以获得以下权限

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

现在在活动中写下。

public class BootUpReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
    Intent i = new Intent(context, MyActivity.class);  
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);  
}
}

答案 2 :(得分:12)

如果通过自动启动你的意思是手机启动时自动启动,那么你应该为BOOT_COMPLETED意图注册一个BroadcastReceiver。一旦启动完成,Android系统就会广播该意图。

一旦收到该意图,您就可以启动一个可以执行任何操作的服务。

请注意,尽管在手机上一直运行服务通常是一个坏主意,因为它即使在空闲时也会占用系统资源。您应该仅在需要时启动服务/应用程序,然后在不需要时停止它。

答案 3 :(得分:0)

对于这个话题,我总是在这里。我会把我的代码放在这里,所以我(或其他)下次可以使用它。 (Phew讨厌搜索我的存储库代码)。

添加权限:

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

添加接收者和服务:

    <receiver android:enabled="true" android:name=".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>
    <service android:name="Launcher" />

创建类Launcher:

public class Launcher extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        new AsyncTask<Service, Void, Service>() {

            @Override
            protected Service doInBackground(Service... params) {
                Service service = params[0];
                PackageManager pm = service.getPackageManager();
                try {
                    Intent target = pm.getLaunchIntentForPackage("your.package.id");
                    if (target != null) {
                        service.startActivity(target);
                        synchronized (this) {
                            wait(3000);
                        }
                    } else {
                        throw new ActivityNotFoundException();
                    }
                } catch (ActivityNotFoundException | InterruptedException ignored) {
                }
                return service;
            }

            @Override
            protected void onPostExecute(Service service) {
                service.stopSelf();
            }

        }.execute(this);

        return START_STICKY;
    }
}

创建类BootUpReceiver以在android重启后执行操作。

例如,启动MainActivity:

public class BootUpReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent target = new Intent(context, MainActivity.class);  
        target.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(target);  
    }
}
相关问题