我有一个广播接收器,只要设备启动(不是),就应该调用它,它会启动我的服务。不幸的是,该服务尚未启动!
我查看了此页面,阅读了所有答案,并按照每个步骤进行了操作……但是它仍然无法正常工作。每当手机重新启动/开机时,我都想启动我的服务。
package curlybrace.ruchir.myApp; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class BootUp extends BroadcastReceiver { private static final String TAG = "myTag"; @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.v(TAG, "Hooray! Received boot! :) "); //Sadly I am not getting this message Intent startServiceIntent = new Intent(context, MyService.class); context.startService(startServiceIntent); } }
但是我的服务仍然无法启动。这是我的清单:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="curlybrace.ruchir.myApp" android:installLocation="internalOnly"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.VIBRATE"/> <uses-permission android:name="android.permission.SEND_SMS"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/Theme.AppCompat.Light.NoActionBar"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Settings" android:label="@string/title_activity_settings" android:theme="@style/Theme.AppCompat.Light.NoActionBar" /> <service android:name=".MyService" android:enabled="true" android:exported="true" /> <receiver android:name=".BootUp" > <intent-filter> <action android:name="android.intent.action.RECIEVE_BOOT_COMPLETED" /> <action android:name="android.intent.action.QUICKBOOT_POWERON" /> </intent-filter> </receiver> </application> </manifest>
在过去的三天中,我一直对此保持执着, 非常感谢您的帮助。为什么我的服务无法在启动时启动?
这是我定义自动启动接收器的方法:
<receiver android:name=".receivers.AutoStartReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver>
注意 android.intent.action.BOOT_COMPLETED
android.intent.action.BOOT_COMPLETED