cant handle onPause after start the app for second time

  • Replies:1
ali zolfaghari
  • Forum posts: 15

Jan 17, 2015, 4:39:22 PM via Website

i have two countdowntimer, one in onCreate and on in onPuase that they do my jobs every seconds. I tested my app in three ways, the first one has a problem.
1. if start app, press the home key, go to menu and start the app again, my object on the main activity that they will be refresh every second, i mean remove all theme and create all theme every second, will blinking(my problem is this one).
2. if start the app, press the back button key, and from the menu run the app again, every thing is OK.
3. if start the app, press the home button key, recall the app from notifications that app create, again every thing is OK.

what shoud id do?

Reply
ali zolfaghari
  • Forum posts: 15

Jan 17, 2015, 5:08:46 PM via Website

here is the code

    @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);
    }

Reply