Изменение переменной из метода уведомления - PullRequest
0 голосов
/ 01 мая 2020

Я добавил функцию уведомления в свое приложение, когда пользователь нажимает на уведомление, которое он отправляет ему на определенную операцию в моем приложении, но я также хочу, чтобы оно изменило целочисленный номер переменной, я попытался определить переменную stati c и сделайте:

ActivityName.Variable = ActivityName.Variable + 1 или ActivityName.Variable++;

И это действительно сработало! но только когда я вручную изменяю дату на своем телефоне, поэтому, когда пользователь видит уведомление после ожидания 1 дня (уведомление является ежедневным) и нажимает на него ... он попадает в действие, но без увеличения переменной.

вот коды уведомлений:

 public void setNotificationOn() { //This method sets the notification...so after 24h it will be triggered

        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.HOUR_OF_DAY, 24);

        Intent intent = new Intent(getApplicationContext(), NotificationReceiverActivity.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
                100, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
    }
package com.example.wordspuzzlejsontest;

public class NotificationReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        LevelActivity.stars++;    //This is the variable which I'm trying to increase
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent repeating_intent = new Intent(context, LevelsListActivity.class);
        repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity
                (context, 100, repeating_intent, PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setContentIntent(pendingIntent)
                .setSmallIcon(android.R.drawable.star_big_off)
                .setContentTitle("Collect your daily star")
                .setContentText("Click to collect your daily star")
                .setAutoCancel(true)
                .setColor(Color.BLUE);

        notificationManager.notify(100, builder.build());
    }
}
...