android - 如何在 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");
}
}
}
解决方案
推荐阅读
- sql - oracle sql中的总和(datediff())
- xamarin.forms - 为 Xamarin Forms 生成 QR 码图像流或字节
- c++ - 针对静态库的 g++ 链接不起作用
- python - 无法在 Google Colab 中运行 Pyspark
- swift - Swift 结构中是否有复制构造函数?
- r - R:如果来自单独向量的术语出现在行中,则保留行
- php - 从转储文件批量导入mysql myisam非常慢
- powershell - FullyQualifiedErrorId : PositionalParameterNotFound,Add-DistributionGroupMember
- python - 如何根据键在两个字典中进行循环以进行多重处理?
- c++ - 如何在openGL中围绕全局轴旋转模型?