在Android

时间:2016-09-28 13:43:39

标签: android

我有一个小应用程序,它使用服务从服务器下载问题文件。我需要实现一个"全部取消"直接在那里通知行动。通常,它非常简单。我只需要停止我能做的服务。但我不知道如何实现代码来停止通知服务。我想我必须对PendingIntent做点什么。

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>

1 个答案:

答案 0 :(得分:1)

Service课程中,实例化BroadcastReceiver,并确保使用自定义Intent-Filter进行注册。现在,通过通知中的intent添加PendingIntent触发操作。在onReceive()的{​​{1}}范围内,通过取消下载等方式来处理您的内容。

编辑:添加代码示例:

将您的BroadcastReceiver课程定义为:

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(); } }; } 启用Intent

Receiver