我有一个简单的AsyncTask正在执行后台下载并使用通知向用户显示反馈 . 此外,此通知还有取消下载的操作 . 单击动作后,应该发出广播,我的接收器类应该 grab 它 . 但是,我无法点击按钮:它似乎不会以任何方式互动 . 所有点击都会转到通知本身 . 如果我在内容意图上设置PendingIntent,它可以正常工作 .
我正在运行CyanogenMod Lollipop 5.0.2
这是代码 .
AndroidManifest.xml
<receiver android:name=".CancelReceiver">
<intent-filter>
<action android:name="org.warpten.dizzy.stopdownload"></action>
</intent-filter>
</receiver>
Async Task constructor
public DownloaderTask(Context ctx, SearchMatch info) {
_context = ctx;
_info = info;
_notification = new Notification.Builder(_context);
_notificationManager = (NotificationManager)_context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent actionIntent = new Intent("org.warpten.dizzy.stopdownload");
actionIntent.putExtra("notificationId", _info.id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(_context, 0, actionIntent, PendingIntent.FLAG_ONE_SHOT);
_notification.setPriority(Notification.PRIORITY_MAX);
if (ImageCache.HasImage(_info.id))
_notification.setLargeIcon(ImageCache.GetImageBitmap(_info.id));
// _notification.setContentIntent(pendingIntent); // This works
_notification.addAction(android.R.drawable.ic_menu_close_clear_cancel,
"Cancel", pendingIntent); // Doesn't work, cannot interact with the action
}
经理可以在doInBackground中进一步通知通知 .
Broadcast Receiver
public class CancelReceiver extends BroadcastReceiver {
interface ICancelReceiver
{
void CancelDownload(int trackId);
}
public static ICancelReceiver Listener; // Lazyness to write a setter
@Override
public void onReceive(Context context, Intent intent) {
int notificationId = intent.getIntExtra("notificationId", -1);
if (Listener != null && notificationId != -1)
Listener.CancelDownload(notificationId);
}
}
1 回答
AndroidManifest.xml
Async Task constructor
Broadcast Receiver
}