Android自动启动应用程序

Seb*_*wak 2 boot service android device autostart

如何在设备启动时自动启动应用程序的服务(可以启用/禁用此功能)?我必须在AndroidManifest中包含哪些权限?

谢谢

Nik*_*tel 5

此权限是有用的

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Run Code Online (Sandbox Code Playgroud)

在<application>元素中(确保为BroadcastReceiver使用完全限定的[或相对]类名称):

<receiver android:name="com.example.MyBroadcastReceiver">  
    <intent-filter>  
        <action android:name="android.intent.action.BOOT_COMPLETED" />  
    </intent-filter>  
</receiver>
Run Code Online (Sandbox Code Playgroud)

在MyBroadcastReceiver.java中:

package com.example;

public class MyBroadcastreceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startServiceIntent = new Intent(context, MyService.class);
        context.startService(startServiceIntent);
    }
}
Run Code Online (Sandbox Code Playgroud)



如需更多帮助:: http://www.androidcompetencycenter.com/2009/06/start-service-at-boot/ http://blog.gregfiumara.com/archives/82 http://androidgps.blogspot.com/ 2008/09 /启动功能的Android服务,在引导,为time.html