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

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

我在 fragment 中这样调用它:

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

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

关于java - Android Java 共享首选项推送通知一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32159132/

相关文章:

java - 如何在 Ant 中重新定义目标?

java - jparepository 的复杂查询。三张表连接

java - Android - 如何在蓝牙打印机 (X330) 上打印图像(光栅)?

android - BroadcastReceiver 尝试在无序广播期间返回结果

php - Laravel - 如何使用一个信号通知

谷歌浏览器中的 HTML5 通知

java - Eclipse 无法识别 examplefilefinder

android-maven-plugin 工作得很好为什么要迁移到 gradle? (适用于 Android 项目)

java - 当我的闹钟触发 onReceive() 时,通知未显示

java - 如何检查java应用程序发送的邮件是否已读?