安卓自动启动应用程序[英] Android autostart application

本文是小编为大家收集整理的关于安卓自动启动应用程序的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

感谢

推荐答案

本权限使用

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

元素中(请务必为broadcasteceiver提供完全限定的[或相对]类名):

<receiver android:name="com.example.MyBroadcastReceiver">  
    <intent-filter>  
        <action android:name="android.intent.action.BOOT_COMPLETED" />  
    </intent-filter>  
</receiver>

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



对于更多帮助:: http://www.androidcompetencyCenter. COM/2009/06/START-SERVICE-AT-BOOT/ http://blog.gregfiumara.com/archives/82 http://androidgps.blogspot.com/2008/09/starting-android-service-at-boot-time.html

本文地址:https://www.itbaoku.cn/post/102386.html

问题描述

How can I autostart my application's service at device boot (with the possibility of enabling/disabling this feature)? What permissions do I have to include in AndroidManifest?

Thanks

推荐答案

this permission are use

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

In your <application> element (be sure to use a fully-qualified [or relative] class name for your BroadcastReceiver):

<receiver android:name="com.example.MyBroadcastReceiver">  
    <intent-filter>  
        <action android:name="android.intent.action.BOOT_COMPLETED" />  
    </intent-filter>  
</receiver>

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



for more help :: http://www.androidcompetencycenter.com/2009/06/start-service-at-boot/ http://blog.gregfiumara.com/archives/82 http://androidgps.blogspot.com/2008/09/starting-android-service-at-boot-time.html