如何使用sharedprefs删除通知

时间:2015-08-05 17:01:46

标签: android notifications

我建立一个布局通知它工作正常,但当我想添加一个swhiche并且用户可以打开或关闭它。 我是通过SharedPreferences制作的,但我不知道我的问题在哪里。 这是我使用的代码

这是主要活动中的oncreat

SharedPreferences notification = getSharedPreferences("notiffsett",
            MODE_PRIVATE);
    boolean norifbollen = notification.getBoolean("notiffk", true);

    if (norifbollen) {

        notification1 = new Notification(R.drawable.ic_launcher,
                "my app", System.currentTimeMillis());

        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        RemoteViews contentView = new RemoteViews(getPackageName(),
                R.layout.custom);
        contentView
                .setImageViewResource(R.id.image, R.drawable.ic_launcher);
        contentView.setTextViewText(R.id.title, "my title");
        contentView.setTextViewText(R.id.text,
                "my text");
        notification1.contentView = contentView;

        Intent notificationIntent = new Intent(this, Main_page.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);
        notification1.contentIntent = contentIntent;

        notification1.flags |= Notification.FLAG_ONGOING_EVENT;
        notification1.flags |= Notification.FLAG_HIGH_PRIORITY;
        mNotificationManager.notify(1, notification1);
    } else {
        //i think my problem is here
        // notification1.flags |= Notification.FLAG_AUTO_CANCEL;

}

这个是我的设置活动:

这3行是我的oncreat

notification = getSharedPreferences("notiffsett", MODE_PRIVATE);
norifbollen = notification.getBoolean("notiffk", true);
swnoti.setChecked(norifbollen);

并且这3行位于点击列表器

上的保存按钮中
SharedPreferences.Editor notiedit = notification.edit();
notiedit.putBoolean("notiffk", swnoti.isChecked());
notiedit.commit();

1 个答案:

答案 0 :(得分:0)

Settings guide中解释了您想要做什么。

还有关于如何使用PreferenceActivityPreferenceFragment的教程,如thisthis