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



我想写一个应用程序,使用报警管理器每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) {

}
}

增量的实现可以在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);
}*/
}

如果你对此有问题,我认为你应该找到文档并阅读有关活动生命周期、AlarmManager的内容,你可以在网上找到一些关于Android开发的好书。视频教程对一些人来说也很好,但我建议你阅读书籍,并与尽可能多的其他开发人员建立联系。未来就在前方!

最新更新