Android Java共享首选项推送通知一次

时间:2015-08-22 17:55:58

标签: java android notifications save

我在myMainActivity内有这个:

public void sendNotificationIfTimeEnd01() {
    Intent intent01 = new Intent(Intent.ACTION_VIEW,
            Uri.parse("https://www.google.de/?gws_rd=ssl"));
    PendingIntent pendingIntent01 = PendingIntent.getActivity(this, 1, intent01, 0);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.drawable.ic_stat_notification);
    builder.setContentIntent(pendingIntent01);
    builder.setAutoCancel(true);
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
    builder.setContentTitle(gamesJuliToStringArray[0]);
    builder.setContentText("Ready");
    builder.setSubText("click for google");
    NotificationManager notificationManager = (NotificationManager) getSystemService(
            NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID_01, builder.build());
    try {
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
        r.play();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

我在我的片段中称它为:

if (diffDays <= 0) {
    String diffDaysAvailable = "ready";
    ((TextView) android.findViewById(R.id.readyLeft)).setText(diffDaysAvailable);
    ((TextView) 
    activity.sendNotificationIfTimeEnd01();
    Log.d("MyApp", "notification 1");
}

我基本上会收到diffDays <= 0的示例通知。 到目前为止这是有效的。

问题是当我重新启动应用程序时,通知会一直弹出。

我用Google搜索并读取应该使用共享首选项来解决此问题。 (没有经验)。 到目前为止我有这个:

        final SharedPreferences sharedPreferences = getActivity().getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
        final SharedPreferences.Editor editor = sharedPreferences.edit();

        // push notification once time is up
        final String notification01 = sharedPreferences.getString("notification01", "not01");

但不知道如何继续解决这个问题。

2 个答案:

答案 0 :(得分:2)

做这样的事情:

if (diffDays <= 0) {
    final SharedPreferences sharedPreferences = getActivity().getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
    final boolean notification01 = sharedPreferences.getBoolean("notification01", false);

    if (!notification01) {
        String diffDaysAvailable = "ready";
        ((TextView) android.findViewById(R.id.readyLeft)).setText(diffDaysAvailable);
        activity.sendNotificationIfTimeEnd01();
        Log.d("MyApp", "notification 1");
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("notification01", true);
        editor.apply();
    }
}

在发送任何通知之前,这将检查布尔值是否为假。如果为false,则发送通知并将该布尔值设置为true,否则为空。

答案 1 :(得分:0)

此行会使用您提供的名称初始化您的SharedPreference,并将Private保留在您的应用中

SharedPreferences sharedPreferences = getActivity().getSharedPreferences("YOUR_PREFERENCE_NAME", Context.MODE_PRIVATE);

如果希望优先存储与密钥相关联的boolean数据(字符串文字),请调用此方法。

public void setValue() {
 SharedPreferences.Editor editor = sharedPreferences.edit();
 editor.putBoolean("your_key", true);
 editor.commit();
}

使用此方法,您可以检查是否已使用密钥在您的偏好中添加了true值。

public boolean isValueExists() {
 boolean value = sharedPreferences.getBoolean("your_key", false);
 return value;
}