【发布时间】:2015-03-08 11:08:13
【问题描述】:
我目前有一个片段,它有几个按钮并包含一个 onClickListener。每次单击其中一个按钮时,计数器变量都会增加 1,并使用 SharedPreferences 将其设置为另一个 Fragment 中的 TextView 的文本。
即使在应用完全关闭后,计数器也会保持不变,并会在应用的后续运行中出现。
我的新目标是在每天结束时将计数器重置为 0(确切地说是 23:59:00)。
我决定避免使用 Google 搜索来解决这个问题,并在 Android 开发者文档中找到了 TimerTask、Calendar、Timer 和 Date API;我试图让它与这些 API 一起工作。不幸的是,它没有按照我的计划进行。变量 设置回 0,但它们保持为零,并且只会递增到 1,并且每次我退出应用程序时都会回到 0。
有没有更好的方法来解决这个问题?还是我的方法够用,只需要调整/更改一些代码?
其中一个问题可能是我在哪里更改了计数器变量引用(如果是,我应该在哪里更改它)?
这是我的尝试:
第一个片段
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflating the layout
View v = inflater.inflate(R.layout.starting_fragment, container, false);
//Instantiate new Timer
Timer timer = new Timer();
// Creates a Calendar object that specifies a specific time of day
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY, 20);
cal.set(Calendar.MINUTE, 57);
cal.set(Calendar.SECOND, 00);
cal.set(Calendar.MILLISECOND, 00);
// Instantiate a day object and use the time of day from cal object as its data
Date date = cal.getTime();
TimerTask tt = new TimerTask() {
// Sets the counter variables back to 0
@Override
public void run() {
COUNT_OOL = 0;
COUNT_WTE = 0;
COUNT_BLO = 0;
COUNT_BLK = 0;
COUNT_HBL = 0;
COUNT_GRN = 0;
COUNT_MTE = 0;
}
};
// Resets the counter variables (to 0) at the time specified by the date object
timer.schedule(tt, date);
// Stores count for each button back into their respective count variable
// Initializes the value from previous runs of app to subsequent runs of app
// This way, count variables will never get set back to 0 after onDestroy()
COUNT_OOL = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("oolongCount", 0);
COUNT_WTE = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("whiteCount", 0);
COUNT_BLO = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("bloomingCount", 0);
COUNT_BLK = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("blackCount", 0);
COUNT_HBL = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("herbalCount", 0);
COUNT_GRN = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("greenCount", 0);
COUNT_MTE = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("mateCount", 0);
递增计数器变量的 onClick 方法:
@Override
public void onClick(View view) {
int id = view.getId();
/*
* Use the View interface with OnClickListener to get the Button ID's
* Then you can run a switch on the Buttons (because normally switches
* cannot be run on buttons
*/
if (id == R.id.tea_type1) {
Builder oolongBuilder = new AlertDialog.Builder(StartingFragment.this.getActivity(),
AlertDialog.THEME_HOLO_LIGHT);
oolongBuilder.setPositiveButton("Hot",
//Starts OolongTeaActivity for hot tea when clicked
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Intent i = new Intent(StartingFragment.this.getActivity(),
OolongTeaActivity.class);
StartingFragment.this.getActivity().startActivity(i);
}
});
oolongBuilder.setNeutralButton("Iced",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(StartingFragment.this.getActivity(),
ColdOolongTeaActivity.class);
StartingFragment.this.getActivity().startActivity(i);
}
});
oolongBuilder.setTitle("Oolong Tea");
oolongBuilder.setMessage("How Do You Like Your Tea?");
AlertDialog oolongDialog = oolongBuilder.create();
oolongDialog.show();
COUNT_OOL++;
SharedPreferences pref1 = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE);
SharedPreferences.Editor editor1 = pref1.edit();
editor1.putInt("oolongCount", COUNT_OOL);
editor1.commit();
}
SecondFragment(将计数器设置为 TextViews 的文本):
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tea_counter, container, false);
oolongCounterText = (TextView) rootView.findViewById(R.id.oolong_counter_tv);
SharedPreferences pref1 = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE);
Integer counter1 = pref1.getInt("oolongCount", 0);
String s1 = String.valueOf(counter1);
oolongCounterText.setText(s1);
【问题讨论】:
-
您想使用
AlarmManager安排您的夜间计数器重置。 -
@MikeM。我该怎么做?我现在正在查看文档,所以看来我必须从
AlarmManager am = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE); am.setExact(AlarmManager.RTC, System.currentTimeMillis(), pendingintent);之类的东西开始 -
@MikeM。我查看了文档,但我不明白的是 PendingIntent。我没有尝试使用意图,因为我没有使用任何活动。这种变量的更新仅在两个片段之间进行。你能帮我解释一下吗?
-
最简单的做法可能是实现一个BroadcastReceiver,它更新
onReceive()中的SharedPreferences设置,在manifest中注册它,并通过getBroadcast()获取PendingIntent。 -
@MikeM。我创建了一个 BroadCastReceiver 类并在我的第一个 Fragment 中设置了 AlarmManager。我想我快到了,但我不确定如何处理我的 Broadcast 类中的 SharedPreferences,因为它与 Fragment 中的计数器有关。所以我想要更多的帮助...FirstFragment with Alarmanager && Broadcast Activity
标签: android android-fragments reset