首页 文章

Android通知操作无法点击

提问于
浏览
0

我有一个简单的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 回答

  • -1

    AndroidManifest.xml

    <application>
          <receiver android:name=".CancelReceiver">
            <intent-filter>
                <action android:name="org.warpten.dizzy.stopdownload"></action>
            </intent-filter>
         </receiver>
       </application>
    

    Async Task constructor

    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;
        }
    
    };
    

    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) {
        // 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);
    }
    

    }

相关问题