0

我想在它显示为自动收报机并播放声音(以及振动)后从抽屉中删除通知。

我的应用程序中有以下代码

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_action_calendar_themedark)
            .setLights(0xffffffff, 500, 1000)
            .setAutoCancel(true)
            .setTicker(context.getString(R.string.tewstgfd));

    if (vibratePattern != null) {
        mBuilder.setVibrate(vibratePattern);
    }

    if (soundUri != null) {
        mBuilder.setSound(soundUri, AudioManager.STREAM_NOTIFICATION);
    }

    addPriorityHigh(mBuilder);

    NotificationManager mNotificationManager =
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(R.string.e_notif_rest_id, mBuilder.build());
    mNotificationManager.cancel(R.string.e_notif_rest_id);

注意最后两行:我创建通知并立即将其删除。但是,这也会停止通知声音并停止振动。所以只显示一个代码,但这根本不是我想要的。如果我删除最后一行 (mNotificationManager.cancel(R.string.e_notif_rest_id),则通知将保留在抽屉中,但这是我想要避免的。

我错过了什么?

4

1 回答 1

1

尝试延迟.cancel

final  NotificationManager mNotificationManager =
.....rest of your code...


/// might need to do this part in a background thread -- start
MediaPlayer mp = new MediaPlayer();

AssetFileDescriptor d = context.getAssets().openFd(fileName);
mp.reset();
mp.setDataSource(d.getFileDescriptor(), d.getStartOffset(), d.getLength());
mp.prepare();  
int duration = mp.getDuration();
mp.reset();
/// might need to do this part in a background thread -- end


mNotificationManager.notify(R.string.e_notif_rest_id, mBuilder.build());
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
         mNotificationManager.cancel(R.string.e_notif_rest_id);
    }
}, duration+200 /*+ 200ms*/);
于 2013-06-21T21:13:08.193 回答