我有一个简单的AsyncTask,它正在执行后台下载并使用通知向用户显示反馈。此外,此通知还有取消下载的操作。单击动作后,应该发出广播,我的接收器类应该抓住它。但是,我无法点击按钮:它似乎不会以任何方式互动。所有点击都会转到通知本身。如果我在内容意图上设置了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>
异步任务构造函数
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中进一步通知通知。
广播接收器
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);
}
}
答案 0 :(得分:0)
尝试将此添加到清单文件中
<receiver
android:name=".CancelReceiver"
android:enabled="true"
android:exported="false" />
答案 1 :(得分:-1)
<强>的AndroidManifest.xml 强>
<application>
<receiver android:name=".CancelReceiver">
<intent-filter>
<action android:name="org.warpten.dizzy.stopdownload"></action>
</intent-filter>
</receiver>
</application>
异步任务构造函数
class TestAsy extends AsyncTask<Void, Void, Void>{
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@SuppressLint("NewApi")
public TestAsy(Context context){
Log.e("TrackDownloaderDecrypterTask"," ");
android.app.Notification.Builder mBuilder = new android.app.Notification.Builder(
context);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent buttonIntent = new Intent("org.warpten.dizzy.stopdownload");
buttonIntent.putExtra("notificationId", 1);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
buttonIntent, PendingIntent.FLAG_ONE_SHOT);
mBuilder.setPriority(android.app.Notification.PRIORITY_MAX);
//Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.logosmall);
//mBuilder.setLargeIcon(largeIcon);
//Doesn't work, setLargeIcon That's why i am using setSmallIcon
mBuilder.setSmallIcon(R.drawable.logosmall);
mBuilder.addAction(android.R.drawable.ic_menu_close_clear_cancel,
"Cancel", pendingIntent);
mNotificationManager.notify(0, mBuilder.build());
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
return null;
}
};
广播接收器
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) {
// Create Notification Manager
NotificationManager notificationmanager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
// Dismiss Notification
notificationmanager.cancel(0);
Toast.makeText(context, "onStartCommand", Toast.LENGTH_SHORT).show();
int notificationId = intent.getIntExtra("notificationId", -1);
if (Listener != null && notificationId != -1)
Listener.CancelDownload(notificationId);
}
}