Android通知管理器重复响铃

时间:2012-05-16 21:11:23

标签: android android-notifications

public class test extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

        String extra = "test";

        NotificationManager myNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        Intent intent = new Intent(this, another.class);

        Notification notification = new Notification(R.drawable.ic_launcher, extra, 
                System.currentTimeMillis());
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 
                0,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        notification.setLatestEventInfo(getApplicationContext(), "title", "text", pendingIntent);


        notification.defaults|= Notification.DEFAULT_SOUND;
        notification.defaults|= Notification.DEFAULT_LIGHTS;
        notification.defaults|= Notification.DEFAULT_VIBRATE;
        notification.flags |= Notification.FLAG_INSISTENT;
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        myNotificationManager.notify(1, notification);
    }
}

这段代码工作正常,问题是,当我运行它时,通知声音不断响起,直到我在状态栏上检查它。 有没有办法让它只响一次,而不是不断响?

1 个答案:

答案 0 :(得分:7)

删除标记Notification.FLAG_INSISTENT

来自docs:“要按位进入标记字段,如果设置,音频将重复,直到通知被取消或通知窗口被打开。”