为什么 Broadcast Receiver 不适用于服务应用程序 android?
why Broadcast Receiver Don't work for service application android?
我有一个项目,它只是一个服务,没有 activity 和用户界面。我想在 phone 完全启动时启动我的应用程序后台服务。但我从未收到来自 OS 的 "BOOT_COMPLETED" 消息。这些是我的代码:
清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.droid.arghaman.location_tracker">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver android:name=".BootBroadcastReceiver"
android:enabled="true"
android:exported="false"
android:label="StartServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</receiver>
</application>
<service android:name=".mySevice"></service>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
</manifest>
广播接收器:
public class BootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("boot Received", intent.getAction());
Intent serviceLuncher = new Intent(context, myService.class);
context.startService(serviceLuncher);
}
}
我的服务:
public class LocationNotifierService extends Service {
Timer timer ;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate(){
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
Toast.makeText(getBaseContext(),"Location",Toast.LENGTH_SHORT).show();
}
},3000);
}
@Override
public void onDestroy(){
}
@Override
public int onStartCommand(Intent intent, int flagId, int startId){
return START_STICKY;
}
}
但我从未收到 "boot Received" 日志。
有什么错误吗?有什么办法可以调试我的程序吗?
我建议我的项目必须只有这个服务,不能有任何UI。
在您的清单中试试这个
<receiver android:name=".BootBroadcastReceiver"
android:enabled="true"
android:exported="false"
android:label="StartServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I never receive the "BOOT_COMPLETED" Message from OS
部分原因是您没有设置 <receiver>
来接收 android.intent.action.BOOT_COMPLETED
广播。
部分原因是,在设备上的某些内容使用显式 Intent
启动您的组件之一之前,您的应用程序不会接收广播。您的应用程序的设置方式 - 没有用户可以 运行 的 activity - 任何应用程序都不太可能这样做,因此您的代码永远不会 运行.
此外,请记住 Android O 进行了专门设计的更改,以防止后台服务 运行ning 很长时间并限制您获取后台位置更新的能力(您的 location_tracker
名字建议以后要添加)。您可能希望重新考虑以您的方式编写此应用程序是否是明智之举。
我有一个项目,它只是一个服务,没有 activity 和用户界面。我想在 phone 完全启动时启动我的应用程序后台服务。但我从未收到来自 OS 的 "BOOT_COMPLETED" 消息。这些是我的代码:
清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.droid.arghaman.location_tracker">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver android:name=".BootBroadcastReceiver"
android:enabled="true"
android:exported="false"
android:label="StartServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</receiver>
</application>
<service android:name=".mySevice"></service>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
</manifest>
广播接收器:
public class BootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("boot Received", intent.getAction());
Intent serviceLuncher = new Intent(context, myService.class);
context.startService(serviceLuncher);
}
}
我的服务:
public class LocationNotifierService extends Service {
Timer timer ;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate(){
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
Toast.makeText(getBaseContext(),"Location",Toast.LENGTH_SHORT).show();
}
},3000);
}
@Override
public void onDestroy(){
}
@Override
public int onStartCommand(Intent intent, int flagId, int startId){
return START_STICKY;
}
}
但我从未收到 "boot Received" 日志。 有什么错误吗?有什么办法可以调试我的程序吗?
我建议我的项目必须只有这个服务,不能有任何UI。
在您的清单中试试这个
<receiver android:name=".BootBroadcastReceiver"
android:enabled="true"
android:exported="false"
android:label="StartServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I never receive the "BOOT_COMPLETED" Message from OS
部分原因是您没有设置 <receiver>
来接收 android.intent.action.BOOT_COMPLETED
广播。
部分原因是,在设备上的某些内容使用显式 Intent
启动您的组件之一之前,您的应用程序不会接收广播。您的应用程序的设置方式 - 没有用户可以 运行 的 activity - 任何应用程序都不太可能这样做,因此您的代码永远不会 运行.
此外,请记住 Android O 进行了专门设计的更改,以防止后台服务 运行ning 很长时间并限制您获取后台位置更新的能力(您的 location_tracker
名字建议以后要添加)。您可能希望重新考虑以您的方式编写此应用程序是否是明智之举。