开机完成后服务未启动

5

我有一个服务,希望在设备启动时启动。

当服务启动时,我会显示一个Toast消息。

我的问题是,当设备启动时,Toast消息被显示并卡在屏幕上,而服务没有正确启动。

然而,如果我通过一个活动来启动我的服务,服务就会正常启动,并且Toast消息会在几秒钟后正确消失。

我的清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tfl.extprotocolservice"
    android:versionCode="7"
    android:versionName="1.6" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name="com.tfl.extprotocolservice.ExtProtocolBroadcastReceiver"
            android:enabled="true" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <service android:name=".ExtProtocolService" >
            <intent-filter>
                <action android:name="com.tfl.extprotocolservice.ISetIpPort" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.tfl.extprotocolservice.IExtMessage" />
            </intent-filter>
        </service>
 <!-- 
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

我的广播接收器:

public class ExtProtocolBroadcastReceiver extends BroadcastReceiver {


    /* broadcast receiver to start on BOOT COMPLETE*/
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent StartServiceIntent=new Intent(context,ExtProtocolService.class);
        context.startService(StartServiceIntent);

    }

}

顺便说一下,清单中的活动被注释掉了,因为我并不真正需要它,这只是为了测试从活动启动服务。


如果您首先通过启动器启动应用程序,然后重新启动设备,会发生什么?重新启动是否会触发您的服务? - HappyCactus
@HappyCactus 不幸的是不行。还是一样。 - yanish
3个回答

4
如果您的应用程序没有活动,那么您的广播接收器将无法被调用。
当您安装一个应用程序时,它是在“停止状态”下安装的。处于“停止状态”的应用程序不会接收到广播意图。
为了使您的应用程序退出“停止状态”,用户必须手动启动您的应用程序(至少一次)。为此,您必须提供一个他可以用来启动您的应用程序的“活动”。
一旦您的应用程序不再处于“停止状态”,Android 就会向其传递广播意图。也就是说,直到用户“强制停止”您的应用程序为止。
如果用户“强制停止”您的应用程序,它将回到“停止状态”,并且不再接收广播 Intent。除非用户再次手动启动您的应用程序。

这是您需要的!此行为已在Android 3.1中添加/更改:http://developer.android.com/about/versions/android-3.1.html#launchcontrols - David Wasser

0

我尝试使用am broadcast -a android.intent.action.BOOT_COMPLETED,然后它重新启动设备。

你可以尝试<action android:name="android.intent.action.USER_PRESENT"/>

经过更多的研究,我认为是快速启动模式不会广播BOOT_COMPLETE。


什么是 fastboot 模式? - yanish
Fastboot与此问题无关,Fastboot甚至不会启动Android操作系统。您可能将其与“快速启动”混淆了。 - Rolf ツ
1
一些HTC设备可以启用“快速启动”功能,它更像是深度休眠而不是真正的重启,因此不应该提供BOOT_COMPLETE意图。要恢复此操作,请在接收器意图过滤器中添加此操作"<action android:name="android.intent.action.QUICKBOOT_POWERON" />"。 - Md. Sajedul Karim

-1

您的服务正在过滤操作,但您的意图没有提供任何操作。 请使用以下方法进行修复:

StartServiceIntent.setAction("com.tfl.extprotocolservice.IExtMessage");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接