首页 > 解决方案 > 如何在 android sdk 29 或最低版本的启动设备上启动服务?

问题描述

嘿,我正在尝试在我的 android 设备 SDK 29、30 中启动服务。但面临启动服务的问题。我的代码在 SDK 29 下运行良好,例如在 Android oreo 或 Android 5 中

我的清单中的代码是:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <receiver android:name=".awc.BootReceiver" android:exported="false" android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

    <service android:name=".awc.AWC_Service"
        android:exported="true"
        android:enabled="true"
        android:process=":ServiceProcess" />

代码是接收者是:

public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        PrefManager prefManager = new PrefManager(context.getApplicationContext());
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            if (!prefManager.getBoolean("AWC_Disabled")) {
                Intent service = new Intent(context.getApplicationContext(), AWC_Service.class);
                intent.putExtra("StopSer", false);
                // context.startService(service);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    context.startForegroundService(service);
                } else {
                    context.startService(service);
                }
                Log.e("Phone Restart", "AWS Service start");
            }
            Log.e("Phone Restart", "AWS Service start1");
        }

    }
}

标签: androidservice

解决方案


推荐阅读