簡體   English   中英

Android Java Shared Preferences推送通知一次

[英]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我基本上會收到一個示例通知。 到目前為止有效。

問題是,當我重新啟動應用程序時,通知總是彈出。

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

但是不知道如何繼續解決這個問題。

做這樣的事情:

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,否則為空。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM