2017-07-07 88 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

即使应用程序关闭,您也需要使用Foreground Service才能使其工作。请参阅此链接以获取演示实施。

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

+0

我现在正在尝试使用Foreground服务。但是,我无法按照我想要的方式来阻止它。请参阅上面的更新代码。 – ank

+0

使用startForeground(1,builder.build());在循环下方的循环和stopForeground(true)之上。 –

+0

按照建议更新了我的代码,但单击“停止”仍然不起作用。 – ank

1

你所需要的就是服务,是的,可以停止,当你不再需要它的优势AysncTask是,即使你终止从任务管理器你的应用程序您服务whon't会受到影响。

阻止你将需要调用

stopService(新意图(//服务))的服务;

现在,如果你想显示更新计数器的通知,那么使用前台服务。

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

相关问题