2016-09-28 42 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

非常感谢您的示例。明天晚上会试一试。非常感谢你。下载完成后,“取消所有”按钮消失或者有一些方法可以删除操作。 –