1. ホーム
  2. アンドロイド

Context.startForegroundService()がService.startForeground()を呼び出してAnrを生成したわけではない

2022-02-27 09:27:35

本題の解決策1へまっしぐら

//Open the service for compatibility
Intent intentOne = new Intent(this, BackService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    startForegroundService(intentOne);
}else {
    startService(intentOne);
}

サービス受信時

public void onCreate() {
    super.onCreate();
    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
       // the number is randomly written as "40".
        nm.createNotificationChannel(new NotificationChannel("40", "App Service", NotificationManager.IMPORTANCE_DEFAULT)) ;
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "40");

               // where the 2, is also randomly written, the official project is also randomly written
        startForeground(2 ,builder.build());
    }
  
}