在BroadcastReceiver中使用setPrimaryClip()时,Android通知setAutoCancel()将无法正常工作

时间:2016-08-21 21:37:36

标签: android android-notifications clipboardmanager

我遇到了一个奇怪的行为,试图制作一个非常简单的应用程序。我正在发送通知,当用户点击它时,我将一些随机文本复制到剪贴板。以下是我发布通知的方式:

// Build notification
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(this, NotificationBroadcastReceiver.class);
intent.putExtra("test", "hello");
PendingIntent broadcastIntent = PendingIntent.getBroadcast(this, 0, intent, Intent.FILL_IN_DATA | PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
        .setContentTitle("Hello!")
        .setContentText("Hi!")
        .setSmallIcon(R.drawable.icon)
        .setContentIntent(broadcastIntent)
        .setOngoing(false)
        .setDefaults(Notification.DEFAULT_ALL)
        .setTicker("Click me!")
        .setAutoCancel(true);
notificationManager.notify(0, notification.build());

这是我的BroadcastReceiver

public class NotificationBroadcastReceiver extends BroadcastReceiver {

    public NotificationBroadcastReceiver() {

    }

    @Override
    public void onReceive(Context context, Intent 
        // Copy the text to the clipboard when notification is clicked
        ClipboardManager clipboard = (ClipboardManager) context.getSystemService(context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("", "");
        clipboard.setPrimaryClip(clip);
    }
}

我只是将一个空字符串复制到剪贴板。通过这种方式,当我点击通知时,如果我删除该行,则在设置setAutoCancel(true)时它不应该熄灭:

clipboard.setPrimaryClip(clip);

BroadcastReceiver开始,然后点击通知就会被取消。为什么会这样?

0 个答案:

没有答案