2017-07-07 3 views
0

私はasynctaskのライフサイクルをテストするためにこのコードを使用しています。私がしたいのは、アプリケーションがユーザーによって強制的に閉じられていてもasynctaskを終了させることです。毎秒進捗をインクリメントする通知を表示します。 asynctaskはフラグメントから呼び出されます。しかし、すべてのアプリを押して強制終了してアプリを強制終了すると、通知は停止し、それ以降は増分されません。以下の私のコードを見てください。Android:アプリケーションが終了してもasynctaskを維持する

これが該当するのですか、間違っていますか?

また、私がasynctaskを使用している理由は、ユーザーができる限りタスクをキャンセルできるようにするためです。 IntentServiceでこれを行うことはできないと思いますか? (私がこれに間違っているなら私を訂正してください)。

ありがとうございました。

更新:私は今、@John Wickと@Abishekの提案したフォアグラウンドサービスを試みています。しかし、私はそれが望むようにサービスを停止することはできません。停止をクリックすると、ループが始まり、停止しません。

パブリッククラスForegroundServiceは、サービスを拡張{

boolean isCancelled = false; 

    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     if (intent.getAction().equals("Start")) { 
      Log.d("Action", "Start"); 
      Intent notificationIntent = new Intent(this, MainActivity.class); 
      notificationIntent.setAction("Start"); 
      notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

      Intent closeIntent = new Intent(this, ForegroundService.class); 
      closeIntent.setAction("Stop"); 
      PendingIntent pCloseIntent = PendingIntent.getService(this, 0, closeIntent, 0); 

      Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); 

      NotificationManagerCompat manager = (NotificationManagerCompat.from(this)); 
      NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 

      builder.setContentTitle("Truiton Music Player"); 
      builder.setTicker("Truiton Music Player"); 
      builder.setContentText("My Music"); 
      builder.setSmallIcon(R.mipmap.ic_launcher); 
      builder.setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false)); 
      builder.setContentIntent(pendingIntent); 
      builder.addAction(android.R.drawable.ic_media_pause, "Stop", pCloseIntent); 

      startForeground(1, builder.build()); 
      for (int i = 0; i < 10; i++) { 
       Log.d("Service", String.valueOf(i)); 
       builder.setProgress(9, i, false); 

       if (isCancelled) { 
        builder.setContentTitle("Cancelled"); 
        builder.setContentText(""); 
        builder.setProgress(0, 0, false); 
        builder.mActions.clear(); 
        manager.notify(1, builder.build()); 
       } else { 
        if (i == 9) { 
         builder.setContentTitle("Done"); 
         builder.setContentText(""); 
         builder.setProgress(0, 0, false); 
         manager.notify(1, builder.build()); 
        } else { 
         manager.notify(1, builder.build()); 
        } 
        try { 
         Thread.sleep(1000); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 

      stopForeground(true); 
     } else { 
      Log.d("Action", "Start"); 
      isCancelled = true; 
      stopForeground(true); 
      stopSelf(); 
     } 

     return START_STICKY; 
    } 


} 
+1

あなたのアプリが –

+0

近くにあり、あなたが独立してアプリの完成に努めたい場合は、サービス(ない意向サービス)が必要ですライフサイクル。 – PPartisan

答えて

0

あなたはアプリが殺された後もasynctaskを維持することができますが、あなたのdoInBackground作業が終了し、その時にonPostExecuteメソッドの仕事はあなたがcontextそうでない場合はアプリの使用可能なオブジェクトを要求する必要がある場合になります墜落した

1

アプリケーションが終了しても機能するようにするには、フォアグラウンドサービスを使用する必要があります。デモの実装については、このリンクを参照してください。あなたがここに必要なもの

http://www.truiton.com/2014/10/android-foreground-service-example/

+0

私は現在、フォアグラウンドサービスを使用しようとしています。しかし、私はそれが望むようにそれを停止することはできません。上記の更新されたコードをご覧ください。 – ank

+0

startForeground(1、builder.build())を使用します。ループのすぐ上にあり、stopForeground(真)はループの下にあります。 –

+0

提案通りにコードを更新しましたが、[停止]をクリックしても何も起きません。 – ank

1

はサービスです、はい、あなたが不要になったあなたは、あなたが影響を受けますwhon'tサービスタスクマネージャからアプリケーションを終了しても、AysncTask以上の利点がある停止することができます。

あなたは

stopService(新しいテント(//サービス))呼び出す必要がありますサービスを停止するには、

ここで、カウンタを更新する通知を表示するには、このためにフォアグラウンドサービスを使用します。でもあなたは、このためのバックグラウンドサービスを実装し、そのサービスを利用できるようにする必要があり

https://developer.android.com/guide/components/services.html#Foreground

関連する問題