我有两个倒计时器,一个在onCreate,在onPause,他们每隔几秒就完成我的工作。 我用三种方式测试了我的应用程序,第一种有问题 1.如果启动应用程序,按主页键,进入菜单并再次启动应用程序,我的对象将在主活动中每秒刷新一次,我的意思是删除所有主题并每秒创建所有主题,将闪烁(我的问题是这个) 2.如果启动应用程序,请按后退按钮键,然后从菜单中再次运行应用程序,每件事情都可以 3.如果启动应用程序,请按主页按钮键,从应用程序创建的通知中调用应用程序,再次确定每件事情都可以。
我该怎么办?
这是代码(抱歉,我是java和android的新手,我的代码很乱):
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
new CountDownTimer(360000, 360000) {
@Override
public void onTick(long arg0) {
db = openOrCreateDatabase("likeDislike", MODE_PRIVATE, null);
c = db.rawQuery("SELECT count(*) from tbl_like", null);
c.moveToFirst();
if(c.getInt(0) > 0){
c = db.rawQuery("SELECT * FROM tbl_like;", null);
c.moveToFirst();
LinearLayout lay_like_dynamic = (LinearLayout) findViewById(R.id.main_lay_dynamic_like);
((LinearLayout) lay_like_dynamic).removeAllViews();
int id = 0;
DateDiff dateDiff;
do{
id++;
String result_id = c.getString(c.getColumnIndex("id"));
String result_title = c.getString(c.getColumnIndex("title"));
String result_day = c.getString(c.getColumnIndex("day"));
String result_month = c.getString(c.getColumnIndex("month"));
String result_year = c.getString(c.getColumnIndex("year"));
String result_hour = c.getString(c.getColumnIndex("hour"));
String result_minute = c.getString(c.getColumnIndex("minute"));
String result_second = c.getString(c.getColumnIndex("second"));
dateDiff = new DateDiff(result_year, result_month, result_day, result_hour, result_minute, result_second);
if(dateDiff.IsDateValidYet() == false){
if(doneJobIDs.contains(Integer.valueOf(result_id)) == false ){
doneJobIDs.add(Integer.valueOf(result_id));
NotificationManager notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.icon_alert, "زمان سنج", System.currentTimeMillis());
PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), Main.class), 1);
note.setLatestEventInfo(getApplicationContext(), "زمانسنج", "فرصت " + result_title + " تمام شد!", intent);
notifManager.notify(Integer.valueOf(result_id), note);
}
}//check task is valid or not
}while(c.moveToNext());
}
db.close();
c.close();
}
@Override
public void onFinish() {
start();
}
}.start();
}//end onPause
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//////////////////////////////////////////////////////////////////////////////////////////////
NotificationManager notifManagerForCancel = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notifManagerForCancel.cancel(NOTIF_ID);
//////////////////////////////////////////////////////////////////////////////////////////////
TextView txt_title_red = (TextView) findViewById(R.id.main_txt_title_red);
TextView txt_title_blue = (TextView) findViewById(R.id.main_txt_title_blue);
Typeface fnt_titr = Typeface.createFromAsset(getAssets(), "font/BTitrTGEBold.ttf");
txt_title_red.setTypeface(fnt_titr);
txt_title_blue.setTypeface(fnt_titr);
////////////////////////////////////////////////////////////////////////////////////////////
new CountDownTimer(1000,1000) {
@Override
public void onTick(long arg0) {
db = openOrCreateDatabase("likeDislike", MODE_PRIVATE, null);
// db.execSQL("DROP TABLE IF EXISTS tbl_like");
// db.execSQL("DROP TABLE IF EXISTS tbl_dislike");
db.execSQL(
"CREATE TABLE IF NOT EXISTS tbl_like("
+ "id INT (15), "
+ "title VARCHAR(50), "
+ "day VARCHAR(2), "
+ "month VARCHAR(2), "
+ "year VARCHAR(4), "
+ "hour VARCHAR(2), "
+ "minute VARCHAR(2), "
+ "second VARCHAR(2)); "
);
db.execSQL("CREATE TABLE IF NOT EXISTS tbl_dislike("
+ "id INT(15), "
+ "title VARCHAR(50), "
+ "day VARCHAR(2),"
+ "month VARCHAR(2),"
+ "year VARCHAR(4),"
+ "hour VARCHAR(2),"
+ "minute VARCHAR(2),"
+ "second VARCHAR(2));"
);
c = db.rawQuery("SELECT count(*) from tbl_like", null);
c.moveToFirst();
if(c.getInt(0) > 0){
c = db.rawQuery("SELECT * FROM tbl_like;", null);
c.moveToFirst();
LinearLayout lay_like_dynamic = (LinearLayout) findViewById(R.id.main_lay_dynamic_like);
((LinearLayout) lay_like_dynamic).removeAllViews();
int id = 0;
DateDiff dateDiff;
do{
id++;
String result_id = c.getString(c.getColumnIndex("id"));
String result_title = c.getString(c.getColumnIndex("title"));
String result_day = c.getString(c.getColumnIndex("day"));
String result_month = c.getString(c.getColumnIndex("month"));
String result_year = c.getString(c.getColumnIndex("year"));
String result_hour = c.getString(c.getColumnIndex("hour"));
String result_minute = c.getString(c.getColumnIndex("minute"));
String result_second = c.getString(c.getColumnIndex("second"));
dateDiff = new DateDiff(result_year, result_month, result_day, result_hour, result_minute, result_second);
if(dateDiff.IsDateValidYet() == true){
TextView txt_dynamic_title = new TextView(getApplicationContext());
TextView txt_dynamic_time = new TextView(getApplicationContext());
ImageView img_bollet = new ImageView(getApplicationContext());
img_bollet.setImageResource(R.drawable.icon_timer);
txt_dynamic_title.setGravity(Gravity.RIGHT);
txt_dynamic_title.setTextSize(20);
txt_dynamic_title.setTextColor(Color.BLUE);
txt_dynamic_time.setGravity(Gravity.RIGHT);
txt_dynamic_time.setTextSize(16);
txt_dynamic_time.setTextColor(Color.BLACK);
Typeface fnt_koodak = Typeface.createFromAsset(getAssets(), "font/BTitrTGEBold.ttf");
Typeface fnt_narm = Typeface.createFromAsset(getAssets(), "font/BNarm.ttf");
txt_dynamic_title.setTypeface(fnt_koodak);
txt_dynamic_time.setTypeface(fnt_narm);
txt_dynamic_time.setText(" " + dateDiff.getDiff());
txt_dynamic_title.setText(result_title);
LinearLayout li = new LinearLayout(getApplicationContext());
li.setOrientation(LinearLayout.HORIZONTAL);
//
li.setGravity(Gravity.RIGHT);
lay_like_dynamic.addView(li);
li.addView(txt_dynamic_time);
li.addView(txt_dynamic_title);
li.addView(img_bollet);
}else{
doneJobIDs.add(Integer.valueOf(result_id));
TextView txt_dynamic_title = new TextView(getApplicationContext());
TextView txt_dynamic_time = new TextView(getApplicationContext());
ImageView img_bollet = new ImageView(getApplicationContext());
img_bollet.setImageResource(R.drawable.icon_alert);
txt_dynamic_title.setGravity(Gravity.RIGHT);
txt_dynamic_title.setTextSize(20);
txt_dynamic_title.setTextColor(Color.RED);
txt_dynamic_time.setGravity(Gravity.RIGHT);
txt_dynamic_time.setTextSize(16);
txt_dynamic_time.setTextColor(Color.RED);
Typeface fnt_koodak = Typeface.createFromAsset(getAssets(), "font/BTitrTGEBold.ttf");
Typeface fnt_narm = Typeface.createFromAsset(getAssets(), "font/BNarm.ttf");
txt_dynamic_title.setTypeface(fnt_koodak);
txt_dynamic_time.setTypeface(fnt_narm);
txt_dynamic_time.setText(" مهلت تمام شد!");
txt_dynamic_title.setText(result_title);
// if(doesNotified == false){
// NotificationManager notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Notification note = new Notification(R.drawable.icon_alert, "New E-mail", System.currentTimeMillis());
//
// PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), Main.class), 0);
//
// note.setLatestEventInfo(getApplicationContext(), "زمانسنج", "لطفا لیست کارهای خود را بررسی کنید،!", intent);
//
// notifManager.notify(NOTIF_ID, note);
// doesNotified = true;
// }
//
LinearLayout li = new LinearLayout(getApplicationContext());
li.setOrientation(LinearLayout.HORIZONTAL);
//
li.setGravity(Gravity.RIGHT);
lay_like_dynamic.addView(li);
li.addView(txt_dynamic_time);
li.addView(txt_dynamic_title);
li.addView(img_bollet);
}//check task is valid or not
}while(c.moveToNext());
}
db.close();
c.close();
}
@Override
public void onFinish() {
start();
}
}.start();
////////////////////////////////////////////////////////////////////////////////////////////
// Toast.makeText(getApplicationContext(), (System.currentTimeMillis() + "").length() + "", 500).show();//13
// Log.i("timestamp", System.currentTimeMillis() + "");
main_img_like_add = (ImageView) findViewById(R.id.main_img_add_like);
main_img_dislike_add = (ImageView)findViewById(R.id.main_img_add_dislike);
main_img_like_add.setOnTouchListener(OnTouchListener);
main_img_dislike_add.setOnTouchListener(OnTouchListener);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// doneJobIDs.add(Integer.valueOf(result_id));
NotificationManager notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Notification note = new Notification(R.drawable.icon_alert, "زمانسنج", System.currentTimeMillis());
//
// PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), Main.class), 0);
//
// note.setLatestEventInfo(getApplicationContext(), "زمانسنج", "لطفا لیست کارهای خود را بررسی کنید،!", intent);
//
// notifManager.notify(NOTIF_ID, note);
for(int counter=0; counter < doneJobIDs.size(); counter++){
// Toast.makeText(getApplicationContext(), doneJobIDs.size() + "", 500).show();
notifManager.cancel(doneJobIDs.get(counter));
}
// notifManager.cancelAll();
}