Android远程视图待处理的Intent无法正常运行

时间:2019-05-09 12:28:12

标签: android notifications android-pendingintent remoteview

我有一项服务,需要停止使用显示自定义通知的远程视图中的挂起意图。我已经设置了待处理的意图,并在清单中注册了广播接收器。但是,当我单击按钮时,没有任何反应。广播接收器是服务类的内部类。

内部类广播接收器:

public class CancelDownload extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i(TAG, "onReceive: cancel intent");
            if(Objects.equals(intent.getAction(), VIDEODOWNLOADACTIONS.STOP)){
                stopSelf();
                CustomModel model = CustomModel.getInstance();
                if (model != null) {

                    model.changeState();
                }
                notificationManager.cancel(notificationId);
                database.setDownloadFileAsFailed(fileID);
                File emptyfile = new File(destinationPath);
                if (emptyfile.exists()) {
                    emptyfile.delete();
                }
            }
        }
    }

清单:

 <receiver android:name=".data.VideoDownloadService$CancelDownload"
            android:exported="true"
            android:enabled="true">
            <intent-filter>
                <action android:name="my.action.stopdownload"/>
            </intent-filter>
        </receiver>

通知代码:

   RemoteViews downloadViews = new RemoteViews(context.getPackageName(), R.layout.notificaition_layout);
            Intent cancelIntent = new Intent(context, VideoDownloadService.CancelDownload.class);
            cancelIntent.setAction(VIDEODOWNLOADACTIONS.STOP);
            PendingIntent cancelPendingIntent = PendingIntent.getBroadcast(context, 0, cancelIntent, PendingIntent.FLAG_ONE_SHOT);

//在这里都尝试

downloadViews.setPendingIntentTemplate(R.id.cancel,cancelPendingIntent);
            downloadViews.setOnClickPendingIntent(R.id.cancel,cancelPendingIntent);

///将视图设置为通知

 builder.setContent(downloadViews)
         .setCustomBigContentView(downloadViews);

当我单击按钮时,按钮ID正确无动。广播接收器必须是内部类,因为如果用户单击“取消”,我将要执行很多操作。 我如何使其工作。请帮忙。

0 个答案:

没有答案