BroadcastReceiver z warunkiem

0

Zastanawiam się dlaczego pierwszy warunek w BroadcastReceiver nigdy się nie odpala. Mam:

    public class MainActivity extends AppCompatActivity {

    (...)

    public static MainActivity getInstance() {
        return instance;
    }

    private void setAlarm(long timeInMilis) {
        alarmManager = (AlarmManager) getSystemService((Context.ALARM_SERVICE));
        Intent intent = new Intent(this, MyAlarm.class);
        intent.putExtra("KEY", value);
        pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
        alarmManager.setRepeating(AlarmManager.RTC, timeInMilis, 10000, pendingIntent);
        if(dbHelper.getProfile().getHp()>=20) {
            stopAlarmManager();
            Toast.makeText(this, "Your character is fully recovered.", Toast.LENGTH_LONG).show();
        }

        button = findViewById(R.id.button11);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");


                newStoptime = sdf.format(
                        new Date(System.currentTimeMillis() + 60000));

                long alarmDate = 0L;
                try {
                    Date d = sdf.parse(newStoptime);
                    alarmDate = d.getTime();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                setAlarm(alarmDate);

            }
        });

i mój broadcast receiver

    public class MyAlarm extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {
        MainActivity mainActivity = MainActivity.getInstance();
        DBHelper dbHelper = new DBHelper(context);
        dbHelper.getWritableDatabase();

        if(dbHelper.getProfile().getHp()>=40) {
            mainActivity.stopAlarmManager();
            Toast.makeText(context, "Your character is fully recovered", Toast.LENGTH_LONG).show();
        }
        else {
            dbHelper.setHp("1", dbHelper.getProfile().getHp()+20);
            String hp = String.valueOf(dbHelper.getProfile().getHp());
            Toast.makeText(context, "Your character get 20HP and now has: " + hp, Toast.LENGTH_LONG).show();
        }
    }
}

public Profile getProfile() {
        Profile profile = new Profile();
        String er = "";
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res = db.rawQuery("select * from profile", null);
        try {
            if (res.moveToNext()) {
                profile.setId(Integer.parseInt(res.getString(0)));
                profile.setLvl(Integer.parseInt(res.getString(1)));
                profile.setExp(Integer.parseInt(res.getString(2)));
                profile.setHp(Integer.parseInt(res.getString(3)));
                profile.setHpMax(Integer.parseInt(res.getString(4)));
                profile.setMp(Integer.parseInt(res.getString(5)));
                profile.setMpMax(Integer.parseInt(res.getString(6)));
                profile.setStrength(Integer.parseInt(res.getString(7)));
                profile.setSpeed(Integer.parseInt(res.getString(8)));
                profile.setLvlPointsAs(Integer.parseInt(res.getString(9)));
                profile.setLvlPointsUn(Integer.parseInt(res.getString(10)));

            }
        } catch (Exception e) {
            er = e.getMessage();
        }

        return profile;
    }

    public String setHp(String id, int hp) {
        String result = "ok";
        try {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put(PROFILE_COLUMN_HP, hp);
            db.update(PROFILE_TABLE_NAME, contentValues, "ID=?", new String[]{id});

        } catch (Exception ex) {
            result = ex.getMessage();
        }
        return result;
    }

Proszę mi pomóc zastanawiam się nad tym już kilka godzin.

0

jak zadajesz pytanie typu: nie wiem o co chodzi to nie spodziewaj sie że ktoś będzie w wolnym czasie przechodził przez 100 linii czyjgoś kodu

pytaine musisz zadać tak żeby mi się chciało przeczytać - tzn. wytnij wszystko to co nie jest problemem i tylko zaciemnia obraz

czesto samo już to wycięcie powoduje że sam znajdziesz rozwiązanie

0

pewnie dlatego że nigdy nie spełnia się ten warunek >40 .

Btw zastanów się nad architekturą aplikacji. bo posiadanie statycznej referencji do swojego activity to nie jest dobry pomysł

1 użytkowników online, w tym zalogowanych: 0, gości: 1