首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android开机时启动服务,设备重启后如何重启服务类?

Android开机时启动服务,设备重启后如何重启服务类?
EN

Stack Overflow用户
提问于 2010-12-30 20:52:43
回答 5查看 157.7K关注 0票数 114

我需要在启动时启动一个服务。我找了很多。他们在谈论广播接收器。由于我是Android开发的新手,我对android上的服务没有一个清晰的了解。请提供一些源代码。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2010-12-30 21:03:17

票数 97
EN

Stack Overflow用户

发布于 2010-12-30 20:55:29

你的接收者:

public class MyReceiver extends BroadcastReceiver {   

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

     Intent myIntent = new Intent(context, YourService.class);
     context.startService(myIntent);

    }
}

您的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.broadcast.receiver.example"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">

        <activity android:name=".BR_Example"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    <!-- Declaring broadcast receiver for BOOT_COMPLETED event. -->
        <receiver android:name=".MyReceiver" android:enabled="true" android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>

    </application>

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

</manifest>
票数 195
EN

Stack Overflow用户

发布于 2016-12-22 15:15:22

您应该注册BOOT_COMPLETE并重新启动

<receiver android:name=".Services.BootComplete">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.REBOOT"/>
        </intent-filter>
    </receiver> 
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4562734

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档