Android如何在我的设备开机时自行启动我的应用程序?

时间:2014-02-28 06:19:07

标签: android service broadcastreceiver android-broadcast

我正在研究Android应用程序,我只是想当我关闭设备时,当我再次启动我的设备我的应用程序应该自行启动,是否有可能到android?如果是,请帮助,任何帮助将是非常感谢。谢谢。

我的自定义广播接收器类:

package com.whizpool.nogademo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class PowerEventReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
        Intent i = new Intent(context, MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }

}
}

我的menifast文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.whizpool.nogademo"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="8" />

 <uses-permission android:name="android.permission.WAKE_LOCK" />
 <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.whizpool.nogademo.MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="landscape"
        android:windowSoftInputMode="adjustNothing" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name="com.whizpool.nogademo.AEScreenOnOffService" >
        <intent-filter>
            <action android:name="com.whizpool.nogademo.AEScreenOnOffService" />
        </intent-filter>
    </service>
    <receiver android:name="com.whizpool.nogademo.PowerEventReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.ACTION_SHUTDOWN" />
        </intent-filter>
    </receiver>

    <!--  <receiver
        android:name=".AutoRestart"
        android:enabled="true"
        android:exported="true" >
        <intent-filter >
            <action android:name="android.intent.action.PACKAGE_INSTALL" />
            <action android:name="android.intent.action.PACKAGE_CHANGED" />
            <action android:name="android.intent.action.PACKAGE_ADDED" />
            <action android:name="android.intent.action.PACKAGE_REPLACED" />
            <action android:name="android.intent.action.PACKAGE_RESTARTED" />

        </intent-filter>
    </receiver> -->

    <!-- <receiver android:name="com.whizpool.nogademo.MyBroadcastReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver> -->
 </application>


</manifest>

2 个答案:

答案 0 :(得分:3)

使用广播android.intent.action.BOOT_COMPLETED在您的清单中注册广播接收器。然后在onReceive()方法中,您可以完成必要的逻辑工作。这将需要权限android.permission.RECEIVE_BOOT_COMPLETED。

答案 1 :(得分:2)

添加了一个接收器,用于处理电源事件并在此

中开始您的活动
public class PowerEventReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

            if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
                Intent i = new Intent(context, HeliosActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i);
            }

        }
    }

并将其添加到您的清单

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

       <receiver android:name="com.RareMediaCompany.Helios.PowerEventReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.ACTION_SHUTDOWN" />
            </intent-filter>
        </receiver>