Android通知回调

Android通知回调,android,notifications,android-intent,android-pendingintent,Android,Notifications,Android Intent,Android Pendingintent,我将本教程用于AsyncTask,其中包含一个任务和一个通知: 我所困惑的是如何让回调在调用它的原始类中执行某些操作。最理想的情况是,最好有如下内容: private class DownloaderTask extends AsyncTask { doInBackground() { ... download the file ... } onProgressUpdate, onPreExecute, etc. from the example, managing a no

我将本教程用于AsyncTask,其中包含一个任务和一个通知:

我所困惑的是如何让回调在调用它的原始类中执行某些操作。最理想的情况是,最好有如下内容:

private class DownloaderTask extends AsyncTask {
    doInBackground() { ... download the file ... }

    onProgressUpdate, onPreExecute, etc. from the example, managing a notification.

    notificationClicked() {
        if (success) {
          //show file
        } else {
          cancel(true);
        }
}
然而,PendingEvent似乎是为了打开一个新的意图,而不是调用打开它的类上的函数?有没有办法做到这一点


编辑:好的,我找到了如何从PendingEvent调用呼叫服务:

Intent returnIntent = new Intent(_context,DownloadService.class);
returnIntent.putExtra("url", _url);
returnIntent.putExtra("name",_title);
notificationIntent = PendingIntent.getService(_context, 0, returnIntent, 0);
notification.setLatestEventInfo(_context, _title, _url, notificationIntent);
由于总是只有一个服务在运行,DownloadService有一个其所有AsyncTasks的ArrayList,onStart会检查其中一个是否具有相同的url和标题,如果是,它会调用AsyncTask的方法来取消运行项或对已完成项执行操作

ArrayList的计数作为新DownloaderTasks的id发送,因此每个任务都有一个唯一的id来创建其通知,但我注意到,有时当我在status下拉列表中选择通知时,它会使用错误的url和标题调用DownloaderService,就像它使用另一个通知的id一样?如何解决此问题?

请参阅尝试取消异步任务执行的
取消(布尔)
方法

取消任务:

请参阅尝试取消异步任务执行的
取消(布尔)
方法

取消任务:


我终于找到了通知不起作用的原因。在我创建的通知类中,“newpendingent”不足以创建新的pendingent。如文件所述: “如果创建应用程序稍后重新检索相同类型的PendingEvent(相同的操作、相同的意图操作、数据、类别和组件以及相同的标志),它将收到表示相同标记的PendingEvent(如果该标记仍然有效),因此可以调用cancel()将其删除。”它还需要标记\u CANCEL\u CURRENT,因为它可能缓存了上一次运行时的数据

此代码适用于:

Intent returnIntent = new Intent(_context,DownloadService.class);
returnIntent.putExtra("url", _url);
returnIntent.putExtra("name",_title);
returnIntent.putExtra("notifClick",true);
returnIntent.setAction("test.test.myAction"+_NOTIFICATION_ID);
// Important to make a unique action name, and FLAG_CANCEL_CURRENT, to make separate notifications.

notificationIntent = PendingIntent.getService(_context, 0, returnIntent, PendingIntent.FLAG_CANCEL_CURRENT);

我终于找到了通知不起作用的原因。在我创建的通知类中,“newpendingent”不足以创建新的pendingent。如文件所述: “如果创建应用程序稍后重新检索相同类型的PendingEvent(相同的操作、相同的意图操作、数据、类别和组件以及相同的标志),它将收到表示相同标记的PendingEvent(如果该标记仍然有效),因此可以调用cancel()将其删除。”它还需要标记\u CANCEL\u CURRENT,因为它可能缓存了上一次运行时的数据

此代码适用于:

Intent returnIntent = new Intent(_context,DownloadService.class);
returnIntent.putExtra("url", _url);
returnIntent.putExtra("name",_title);
returnIntent.putExtra("notifClick",true);
returnIntent.setAction("test.test.myAction"+_NOTIFICATION_ID);
// Important to make a unique action name, and FLAG_CANCEL_CURRENT, to make separate notifications.

notificationIntent = PendingIntent.getService(_context, 0, returnIntent, PendingIntent.FLAG_CANCEL_CURRENT);

你的问题似乎令人困惑。是否要取消异步任务?你能提供一个你想做什么的例子吗?是的,我希望下载是可取消的,类似于默认浏览器的下载程序,你可以点击它来取消或打开下载的文件。你的问题似乎让人困惑。是否要取消异步任务?你能提供一个你想做什么的例子吗?是的,我希望下载是可取消的,类似于默认浏览器的下载程序,你可以点击它来取消或打开下载的文件。同样的请求代码,文档中声明它是供将来使用的,但实际上它用于平等性检查。请参阅和相同的requestCode,文档中说明它是供将来使用的,但实际上它用于平等性检查。看见