在 Android 啟動時啟動服務
實現正確的配置以在 Android 作業系統啟動時自動啟動服務至關重要。儘管看似一切設定正確,但可能存在阻止所需行為的潛在問題。
Android 清單
查看提供的 Android 清單:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.phx.batterylogger" android:versionCode="1" android:versionName="1.0" android:installLocation="internalOnly"> <uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.BATTERY_STATS" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <service android:name=".BatteryLogger"/> <receiver android:name=".StartupIntentReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> </application> </manifest>
確保 BOOT_COMPLETED 操作已在IntentTEDReceiver。
用於啟動的廣播接收器
接下來,檢查StartupIntentReceiver:
package com.phx.batterylogger; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class StartupIntentReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent serviceIntent = new Intent(context, BatteryLogger.class); context.startService(serviceIntent); } }
此接收器應該偵聽BOOT_COMPLETED 操作並啟動想要的服務。
故障排除
如果服務未在啟動時啟動,請考慮以下步驟:
完整範例
相關 AutoStart應用程式的完整範例,請參考以下程式碼程式碼片段:
AndroidManifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="pack.saltriver" android:versionCode="1" android:versionName="1.0"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <receiver android:name=".autostart"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> <activity android:name=".hello"></activity> <service android:enabled="true" android:name=".service" /> </application> </manifest>
autostart.java
public class autostart extends BroadcastReceiver { public void onReceive(Context context, Intent arg1) { Intent intent = new Intent(context,service.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(intent); } else { context.startService(intent); } Log.i("Autostart", "started"); } }
service.java
public class service extends Service { private static final String TAG = "MyService"; @Override public IBinder onBind(Intent intent) { return null; } public void onDestroy() { Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); Log.d(TAG, "onDestroy"); } @Override public void onStart(Intent intent, int startid) { Intent intents = new Intent(getBaseContext(),hello.class); intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intents); Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); Log.d(TAG, "onStart"); } }
hello.java
public class hello extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Toast.makeText(getBaseContext(), "Hello........", Toast.LENGTH_LONG).show(); } }
實現這些步驟🎜>實現這些步驟應確保這些步驟服務在作業系統啟動時成功啟動。
以上是為什麼我的 Android 服務無法在啟動時啟動?的詳細內容。更多資訊請關注PHP中文網其他相關文章!