2016-09-28 40 views
0

我有一個小應用程序,它使用服務從服務器下載問題論文。我需要直接在通知中執行「全部取消」操作。通常情況下,這很簡單。我只需要停止一項我可以做的服務。但我不知道如何實現代碼來停止通知服務。我想我必須對PendingIntent做些什麼。在Android中的通知中實施操作

I need to implement Cancel All button under first notification

編輯1: @ Shaishav的回答後,我嘗試這樣做:

@Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     // For each start request, send a message to start a job and deliver the 
     // start ID so we know which request we're stopping when we finish the job 
     HandlerThread thread = new HandlerThread("ServiceStartArguments", Process.THREAD_PRIORITY_BACKGROUND); 
     thread.start(); 
     mServiceLooper = thread.getLooper(); 
     mServiceHandler = new ServiceHandler(mServiceLooper); 
     MyApp x = (MyApp)getApplicationContext(); 
     Message msg = mServiceHandler.obtainMessage(); 
     msg.arg1 = startId; 
     mServiceHandler.sendMessage(msg); 
     registerReceiver(stopReceiver, new IntentFilter("Paras")); 


     // If we get killed, after returning from here, restart 
     // I tried changing it to START_NOT_STICKY but still service restarted. 
     return START_STICKY; 
    } 
    private final BroadcastReceiver stopReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      // Methods to stop downloads 
      Intent i = new Intent(getApplicationContext(),DownloadService.class); 
      stopService(i); 
      notificationManager.cancelAll(); 
      stopSelf(); 
     } 
    }; 

private final class ServiceHandler extends Handler { 
     public ServiceHandler(Looper looper) { 
      super(looper); 
     } 
     @Override 
     public void handleMessage(Message msg) { 
      try { 
       MyApp x = (MyApp) getApplicationContext(); 
       notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
       Intent stop_intent = new Intent("Paras"); 
       PendingIntent stop_pi = PendingIntent.getBroadcast(getApplicationContext(), 0, stop_intent, PendingIntent.FLAG_CANCEL_CURRENT); 

       notificationBuilder = new NotificationCompat.Builder(getApplicationContext()) 
         .setSmallIcon(R.drawable.ic_file_download_deep_orange_a400_18dp) 
         .setContentTitle("Downloading") 
         .setContentText("Hold on...") 
         .setAutoCancel(true) 
         .addAction(0, "Cancel All Downloads", stop_pi); 
       notificationManager.notify(x.ID, notificationBuilder.build()); 
       Log.i("Paras", "onHandleIntent: " + x.filename + x.url + " " + x.ID); 
       initDownload(x.filename, x.url, x.ID); 
      }catch (Exception ex){ 

      } 
     } 
    } 

服務的艙單申報:

<service android:name=".DownloadService" android:enabled="true"> 
     </service> 
+0

這裏是你的問題的解決方案[http://stackoverflow.com/questions/12526228/how-to-put-media-controller-button-on-notification-bar](http://stackoverflow.com/questions/ 12526228 /如何把媒體控制器按鈕通知欄) –

回答

1

在您的Service類中,實例化一個BroadcastReceiver並確保您使用自定義Intent-Filter註冊它。現在,在通知中添加通過PendingIntent觸發intent的操作。在您BroadcastReceiveronReceive(),做你的東西與下載的取消等

編輯:添加代碼示例:

定義你Service類爲:

public class YourService extends Service { 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     ... 
     registerReceiver(stopReceiver, new IntentFilter("just.some.random.fixed.string")); 
     ... 
    } 

    // Defining the receiver 
    private final BroadcastReceiver stopReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      // Methods to stop downloads 
      stopSelf(); 
     } 
    }; 

} 

現在,請記住在您的通知中爲Receiver啓動Intent

Intent stop_intent = new Intent("just.some.random.fixed.string"); 
PendingIntent stop_pi = PendingIntent.getBroadcast(context, NUM, stop_intent, PendingIntent.FLAG_CANCEL_CURRENT); 

NotificationCompat.Builder notif = new NotificationCompat.Builder(this) 
    ... 
    .addAction(0, "CANCEL", stop_pi); 
+0

謝謝你的即時響應。由於我對BroadcastReceiver不太瞭解,你能否告訴我第一條評論的方法是否好? –

+0

@ParasSidhu它提到了一個處理東西的活動。你並不是真的需要這個。我已經在上面添加了一個示例。 – Shaishav

+0

非常感謝您的示例。明天晚上會試一試。非常感謝你。下載完成後,「取消所有」按鈕消失或者有一些方法可以刪除操作。 –