Şu anda birkaç Düğme içeren ve bir onClickListener içeren bir Parçam var. Bu düğmelerden biri her tıklandığında, bir sayaç değişkeni 1 ile artırılır ve PaylaşılanPreferences'ı kullanarak başka bir Parçada bir TextView için metin olarak ayarlanır.Günün Belirli Bir Saatinde Tamsayı Değerini Sıfırla
Uygulama tamamen kapatıldıktan sonra sayaç aynı kalacaktır ve uygulamanın sonraki işlemlerinde görünecektir.
Yeni hedefim, sayaçların her günün sonunda 0'a geri sıfırlanmasıdır (tam olarak 23:59:00).
ben bu anlamaya bir Google araması önlemek karar verdi ve TimerTask, Takvim, Timer bulundu ve Android Geliştirici dokümanlar üzerinde tarihi API'leri; Bu API'larla çalışmayı denedim. Ne yazık ki planladığım şekilde çalışmıyor. değişkenleri 0'a geri ayarlıdır, ancak sıfırda kalırlar ve yalnızca 1'e kadar artar ve uygulamadan her çıktığımda 0'a geri döner.Buna yaklaşmanın daha iyi bir yolu var mı? Ya da benim yöntemim yeterli, ve sadece kodun bazılarını değiştirmem/değiştirmem gerekiyor mu? sorunların
Bir Ben de sayaç değişkeni referans değiştiriyorum (ve eğer öyleyse, bunu nerede değiştirmelisiniz) nerede olabilir? İşte
Ben girişiminde aşağıdadır: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);
sayaç değişkenlerini artırır onClick yöntemi:
@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 (için metin olarak sayaç kurar 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);
Gecikme sayacınızı programlamak için 'AlarmManager' kullanmak istiyorsunuz. –
@MikeM. Bunu nasıl yapardım? Şu anda belgelere bakıyorum ve öyle görünüyor ki 'AlarmManager am = (AlarmManager) getActivity(). GetSystemService (Context.ALARM_SERVICE); am.setExact (AlarmManager.RTC, System.currentTimeMillis(), pendingintent); ' – freddiev4
@MikeM. Belgelere baktım ama anlamadığım şey PendingIntent. Bir niyet kullanmaya çalışmıyorum çünkü herhangi bir etkinlik kullanmıyorum. Değişkenlerin güncellenmesi sadece iki Fragman arasındadır. Bunu benim için biraz daha açıklayabilir misin? – freddiev4