Android通知回调
|
我正在AsyncTask上使用带有任务和通知的本教程:
https://eliasbland.wordpress.com/2011/03/11/an-example-of-how-to-run-a-background-task-and-report-progress-in-the-status-bar-using- Android上的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);
}
}
但是,似乎PendingIntent是为了打开新的intent而创建的,而不是在打开它的类上调用了一个函数?有什么办法吗?
编辑:好的,我发现了如何从未决的意图中调用呼叫服务:
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都有一个唯一的ID来创建其通知,但是我注意到有时当我在状态下拉列表中选择一个通知时,它会使用错误的URL调用DownloadService和标题,就像使用另一个通知的ID一样?如何解决?
没有找到相关结果
已邀请:
2 个回复
渐首洽陈染
磨标烫徽啪