2017-05-05 32 views
0

我在這裏做什麼,我已經從Activity.Once服務啓動一個服務啓動它的開始另一個後臺線程從使用通知管理器使用延遲的線程在通知面板上啓動Command()從它顯示進度欄更新,但是什麼問題是當我停止從活動服務比在服務的消滅()服務被調用,我停止更新進度條,即我已經調用這個通知管理器。取消All()或線程。當前Thread().interrupt()來自Service的Destroy(),但進度條不會停止更新通知面板仍在更新。我已經嘗試了所有可能的解決方案,但線程不停止或進度欄不停止更新通知面板上它仍然顯示,以便如何停止在通知面板上更新進度欄我已經做了如下,請幫助我解決問題。謝謝。如何停止更新進度條,顯示在通知面板從後臺服務時,在服務的Destroy()被稱爲

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    Button start, stop; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     start = (Button) findViewById(R.id.start); 
     stop = (Button) findViewById(R.id.stop); 


     start.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 


       startService(new Intent(MainActivity.this, NotificationService.class)); 
      } 
     }); 

     stop.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 


       stopService(new Intent(MainActivity.this, NotificationService.class)); 


      } 
     }); 


    } 

} 

NotificationService.java

public class NotificationService extends Service { 

    private Thread thread; 
    private NotificationManager mNotifyManager; 
    private Builder mBuilder; 


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

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     mNotifyManager = (NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE); 
     mBuilder = new NotificationCompat.Builder(NotificationService.this); 


     Toast.makeText(getApplicationContext(), "oncreate of service called", Toast.LENGTH_SHORT).show(); 
    } 

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

     super.onStartCommand(intent, flags, startId); 

     Toast.makeText(getApplicationContext(), "onStart of service called", Toast.LENGTH_SHORT).show(); 


     mBuilder.setContentTitle("Download") 
       .setContentText("Download in progress") 
       .setSmallIcon(R.mipmap.ic_launcher); 


     mBuilder.setProgress(100, 0, false); 
     mNotifyManager.notify(0, mBuilder.build()); 


     updateService(); 

     return START_NOT_STICKY; 


    } 

    private void updateService() { 

     thread = new Thread() { 
      @Override 
      public void run() { 


       for (int i = 0; i <= 100; i += 5) { 
        // Sets the progress indicator completion percentage 

        try { 


         mBuilder.setProgress(100, i, false); 
         mNotifyManager.notify(0, mBuilder.build()); 

         // Sleep for 5 seconds 
         Thread.sleep(2 * 1000); 

        } catch (InterruptedException e) { 
         Log.d("TAG", "sleep failure"); 
        } 


       } 


       mBuilder.setContentText("Download complete"); 
       // Removes the progress bar 
       mBuilder.setProgress(0, 0, false); 
       mNotifyManager.notify(0, mBuilder.build()); 


      } 
     }; 

     thread.start(); 


    } 


    @Override 
    public void onDestroy() { 
     super.onDestroy(); 


     Toast.makeText(getApplicationContext(), "service Destroyed", Toast.LENGTH_SHORT).show(); 


     // mNotifyManager.cancelAll(); 
     //mNotifyManager.cancel(0); 
     thread.currentThread().interrupt(); 

     // mNotifyManager.cancel(0); 

     // mNotifyManager.notify(0, mBuilder.build()); 


    } 
} 

回答

0

了Thread.interrupt()不會立即中斷線程。它只更新線程的中斷狀態。如果你的線程包含輪詢中斷狀態(即睡眠)的方法,那麼線程將被中斷,否則線程完成執行並且中斷狀態將被忽略。

所以,在你的情況做如下方式:

private class MyWorkerThread extends Thread { 

    public MyWorkerThread(String name) { 
     super(name); 
    } 

    private boolean isStopThread = false; 

    public void setStopThread(boolean isStopThread) { 
     this.isStopThread = isStopThread; 
    } 


    @Override 
    public void run() { 
     super.run(); 

     for (int i = 0; i <= 100; i += 5) { 
      // Sets the progress indicator completion percentage 

      try { 

       if (mWorkerThread.isStopThread) { 
        Log.d("Thread", "thread is interrupted , so breaking loop !"); 
        mWorkerThread.interrupt(); 
        mWorkerThread = null; 
        break; 
       } 

       mBuilder.setProgress(100, i, false); 
       mNotifyManager.notify(0, mBuilder.build()); 

       // Sleep for 5 seconds 
       Thread.sleep(2 * 1000); 

      } catch (InterruptedException e) { 
       Log.d("TAG", "sleep failure"); 
      } 


     } 


     mBuilder.setContentText("Download complete"); 
     // Removes the progress bar 
     mBuilder.setProgress(0, 0, false); 
     mNotifyManager.notify(0, mBuilder.build()); 

    } 
} 

UpdateService方法:

private void updateService() { 

     mWorkerThread = new MyWorkerThread("MyWorkerThread"); 
     mWorkerThread.start(); 

    } 

的OnDestroy()方法

@Override 
    public void onDestroy() { 
     super.onDestroy(); 


     Toast.makeText(getApplicationContext(), "service Destroyed", Toast.LENGTH_SHORT).show(); 


     // mNotifyManager.cancelAll(); 
     //mNotifyManager.cancel(0); 
     //Setting flag for stop thread 
     mWorkerThread.setStopThread(true); 


     // mNotifyManager.cancel(0); 

     // mNotifyManager.notify(0, mBuilder.build()); 


    } 

希望它可以幫助你!

+0

感謝bro.for你的答案。我知道這個標誌是正確還是錯誤,並在循環中做一個trick.but如何停止upadating通知面板上的進度條使用Notificationmanager上可用的mNotifyManager.cancelAll()方法使用這些方法它不會停止更新進度條或只使用欺騙你告訴我是可能的 – Viral

+0

@病毒mNotifyManager.cancellAll()正常工作,停止更新once.but但你的線程正在運行,所以它再次生成通知。 –

+0

@病毒我檢查了你的代碼,你可以在循環內添加一個bool變量,如果通知是取消,然後打破循環 –