我目前有一個片段有幾個按鈕幷包含一個onClickListener。每次單擊其中一個按鈕時,計數器變量將增加1,並使用SharedPreferences在另一個Fragment中設置爲TextView的文本。在特定時間復位整數值
即使應用程序完全關閉,計數器也會保持不變,並且會在應用程序的後續運行中出現。
我的新目標是在每一天結束時計數器復位回到0(23:59:00的時間,是精確的)。
我決定避免了谷歌搜索摸不着頭腦,發現TimerTask的,日曆,定時器,並在Android開發者文檔日期的API;我試圖讓這個與這些API一起工作。不幸的是,它沒有按照我計劃的方式工作。變量爲設置爲0,但它們保持爲零,並且只會增加到1,並且每次退出應用程序時都會回到0。
有沒有更好的方法來解決這個問題?或者我的方法足夠了,我只需要調整/更改一些代碼?
的問題之一可能是在那裏我改變計數器變量引用,以及(如果是的話,我應該在哪裏改變它)?
以下是我嘗試:
FirstFragment
@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);' – freddiev4
@MikeM。我查看了文檔,但我不明白的是PendingIntent。我沒有嘗試使用意圖,因爲我沒有使用任何活動。這種變量更新僅在兩個片段之間。你能爲我解釋一下嗎? – freddiev4