[英]Trouble starting my Android service on boot
我有一個廣播接收器,只要設備啟動(不是),就應該調用它,它會啟動我的服務。 不幸的是,該服務尚未啟動!
我查看了此頁面,閱讀了所有答案,並按照每個步驟進行了操作……但是它仍然無法正常工作。 每當手機重啟/開機時,我都想啟動我的服務。
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
而不是RECIEVE_BOOT_COMPLETED
,因此不會調用您的BroadcastReceiver
。
如果您修復了Receiver應該可以工作的問題。
您啟動服務的代碼看起來不錯。
android:installLocation="internalOnly"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.