[英]BroadcastReceiver in Service is not working after boot completed
[英]Unable to start Service after Device Boot Completed. Boot Completed BroadcastReceiver onReceive() not Calling
我正在开发一个应用程序,它将在设备启动时(启动完成后)自动启动服务。 我做了代码,但是我认为我的代码出了点问题。 我也尝试过Google以及研究时的所有技巧。 你能帮我么? 提前感谢。
这是我的代码:
我的BroadcastReceiver是
public class AutostartReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Toast.makeText(this, "Booting Completed", Toast.LENGTH_LONG).show();
//context.startService(new Intent(context, MyService.class));
}
}
}
我的AndroidMenifest文件是
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myhiddenapp"
android:installLocation="internalOnly"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="@drawable/ic_launcher"
android:label="MyHiddenApp" >
<receiver
android:name=".AutostartReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".MyService" />
</application>
</manifest>
注意: 我没有任何活动,我只想在启动完成时启动服务。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="internalOnly"
... >
您必须设置android:installLocation =“ internalOnly”
https://developer.android.com/guide/topics/data/install-location.html#应该
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.