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

Android Java Shared Preferences push notification once

我里面有这个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,我基本上会收到示例通知。 到目前为止有效。

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

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

        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");

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

做这样的事情:

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();
    }
}

在发送任何通知之前,这将检查布尔值是否为假。如果为假,则发送通知并将该布尔值设置为真,否则什么都不做。

此行使用您提供的名称初始化您的 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;
}