服务未在BOOT COMPLETE上启动 [英] service not started on BOOT COMPLETE
问题描述
我有一个我想在BOOT COMPLETE上启动的服务
I have a service that I would like to start on BOOT COMPLETE
启动时,我会显示一条敬酒消息。
when it is started , I have a toast message displayed.
我的问题是,当设备启动时,会显示吐司并卡在屏幕上,并且服务无法正确启动。
my problem is that when the device boots up , the toast is displayed and is stuck on screen, and the service is not starting correctly.
但是,如果我尝试通过一项活动来启动我的服务,则该服务会正常启动,并且吐司会在几秒钟后正确消失。
however if I am trying to start my service through an activity , the service is starting well and the toast disappears after a few seconds correctly.
我的清单:
<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);
}
}
btw,清单中的活动被注释,因为我并不是真正需要它,它只是为了测试从活动中启动服务。
btw, the activity in the manifest is commented because I don't really need it , it was just to test starting the service from an activity.
推荐答案
如果您的应用程序没有活动,则您的 BroadcastReceiver
将永远不会被调用。
If your application has no activities, your BroadcastReceiver
will never get called.
在安装应用程序时,它以停止状态安装。处于停止状态的应用程序不会广播意图
传递给他们。
When you install an application, it is installed in the "stopped state". applications in "stopped state" do not get broadcast Intent
s delivered to them.
为了获取您的应用程序在停止状态之外,用户必须手动启动您的应用程序(至少一次)。为此,您必须向他提供活动
,他可以使用该活动来启动您的应用程序。
In order to get your application out of "stopped state", the user must manually launch your application (at least once). In order to do this, you must offer him an Activity
that he can use to start your application.
一次您的应用程序不再处于停止状态,Android会向其提供广播 Intent
。也就是说,直到用户强制停止您的应用程序。
Once your application is no longer in "stopped state", Android will deliver broadcast Intent
s to it. That is, until the user "force stops" your application.
如果用户强制停止您的应用程序,它将返回停止状态,并且不会不再获得意图
的广播。直到用户再次手动启动您的应用程序为止。
If the user "force stops" your application, it will go back to "stopped state" and will no longer get the broadcast Intent
s. Until the user manually starts your application again.
这篇关于服务未在BOOT COMPLETE上启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!