我正在开发一个应用程序,它将在设备启动时(启动完成后)自动启动服务。我做了代码,但我认为我的代码出了点问题。我也尝试过谷歌以及研究时的所有技巧。你能帮我么?提前谢谢。
这是我的代码:
我的广播接收器是
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>
注意: 我没有任何活动,我只想在启动完成时启动服务。