如何使用警报管理器增加整数值

时间:2018-11-07 12:25:57

标签: android

我想编写一个使用警报管理器每15分钟增加一个int数的应用程序,并在共享的首选项中保存新值,然后在文本视图中显示值。我是一个全新的人,尝试了很多事情,但是徒然。如果您发现一些不好的事情并向我解释,请保持友善。谢谢。 这是代码。

public class MainActivity extends AppCompatActivity {
AlarmManager am;

    int d=3;
    TextView tv;
    SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.ed);

        }

    public void time(Context context,Intent intent){
        am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        PendingIntent alarmIntent;
        Intent intnt = new Intent(context, MyAlarm.class);
        // Set the alarm to start at approximately 2:00 p.m.
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 14);
        alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
d++;
// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
        am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_FIFTEEN_MINUTES, alarmIntent);
    }
    public void shared(){
        sp = (SharedPreferences) getSharedPreferences("sha", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt("as", d);
        editor.commit();
    }
    public void show(int i){
        tv=(TextView)findViewById(R.id.ed);
        int q=sp.getInt("as",d);
        tv.setText(q);

    }
}

服务是

public class MyAlarm extends BroadcastReceiver {

    //the method will be fired when the alarm is triggerred
    @Override
    public void onReceive(Context context, Intent intent) {


    }

}

1 个答案:

答案 0 :(得分:0)

您可以在MyAlarm类的onReceive方法中实现增量的实现,当您收到警报时,这就是您应该响应的地方。

public class MyAlarm extends BroadcastReceiver {

    //the method will be fired when the alarm is triggerred
    @Override
    public void onReceive(Context context, Intent intent) {
        SharedPreferences sp = (SharedPreferences) getSharedPreferences("sha", Context.MODE_PRIVATE);
        int d = sp.getInt("as", 0);// get the previous value
        d = d + 1;
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt("as", d);
        editor.commit();// finally, save it
    }

}

您的活动然后可以清理为类似于以下内容:

public class MainActivity extends AppCompatActivity {
    AlarmManager am;

    int d=3;// do you really need this?
    TextView tv;
    SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.ed);
        int q=sp.getInt("as",0);// this should return an integer if available, otherwise 0
        tv.setText(String.format("%d", q));// format the integer as a String
        am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        PendingIntent alarmIntent;
        Intent intnt = new Intent(this, MyAlarm.class);// I replaced context with `this` because AppCompatActivity and any Activity is a Context in Android applications
        // Set the alarm to start at approximately 2:00 p.m.
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 14);
        alarmIntent = PendingIntent.getBroadcast(this, 0, intnt, 0);
        d++;// you never used this value here, is it really needed?
// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
        am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_FIFTEEN_MINUTES, alarmIntent);
    }


/* Function is not used in the flow
        public void shared(){
            sp = (SharedPreferences) getSharedPreferences("sha", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putInt("as", d);
            editor.commit();
        }*/
/* Function could be inlined in the onCreate method
        public void show(int i){
            tv=(TextView)findViewById(R.id.ed);
            int q=sp.getInt("as",d);
            tv.setText(q);

        }*/
    }

如果您对此有疑问,我认为您应该找到文档并阅读有关Activity生命周期的内容,AlarmManager,并且可以在线找到有关Android开发的好书。视频教程对某些人也有好处,但是我建议您阅读书籍并与见面的其他开发人员交流。有未来!