Skip to content Skip to sidebar Skip to footer

How To Cancel The Timer And Renew The Same Timer?

I am creating an app that vibrate and beep every 30 sec and when I log out the vibrate and beep must be cancelled and when I log in the vibrate and beep should resume. NOTE: it mu

Solution 1:

i have recently run this code and is working fine. This can be achieved using broadcast Receiver.You have to implement separate CustomTimer task that extend TimerTask:

Activity mActivity=null;
publicMyCustomTimer(Activity mActivity) {
    this.mActivity=mActivity;
}
    @Overridepublicvoidrun() {
        this.mActivity.runOnUiThread(newRunnable() {

            @Overridepublicvoidrun() {
                Toast.makeText(mActivity, "Write you code here",Toast.LENGTH_LONG).show();
                Log.d("MyCustomTimer","Call");
            }
        });

    }

After this you have to implement BroadCast Receive in that class where you want to implement " vib() " method.: Let say, in my case (just for example ) is MainActivity:

publicclassMainActivityextendsActivity {
    privateMyCustomTimermyCustomTimer=null;
    BroadcastReceivermBr_Start=newBroadcastReceiver() {

        @OverridepublicvoidonReceive(Context context, Intent intent) {
            if (intent.getAction().equals("START_VIBRATION")) {
                System.out.println("onreceive :START_VIBRATION");
                vib();
            }
        }
    };
    BroadcastReceivermBr_Stop=newBroadcastReceiver() {

        @OverridepublicvoidonReceive(Context context, Intent intent) {
            if (intent.getAction().equals("STOP_VIBRATION")) {
                stopVibration();
            }
        }
    };

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        IntentFiltermIntentFilter=newIntentFilter();
        mIntentFilter.addAction("START_VIBRATION");
        registerReceiver(mBr_Start, mIntentFilter);
        IntentFiltermIntentFilter2=newIntentFilter();
        mIntentFilter2.addAction("STOP_VIBRATION");
        registerReceiver(mBr_Stop, mIntentFilter2);

        Buttonb1= (Button) findViewById(R.id.button1);
        b1.setOnClickListener(newOnClickListener() {

            @OverridepublicvoidonClick(View v) {
                Intenti=newIntent(MainActivity.this, MySecondActivity.class)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);

            }
        });
    }

    @OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        returntrue;
    }

    privatevoidvib() {
        myCustomTimer = newMyCustomTimer(MainActivity.this);
        Timertimer=newTimer();
        timer.scheduleAtFixedRate(myCustomTimer, 0, 30000);
    }

    privatevoidstopVibration() {
        Log.d("MainActivity", "Before Cancel");
        if (null != myCustomTimer)
            myCustomTimer.cancel();
        Log.d("MainActivity", "After Cancel");
    }

}

Now,you can start Or stop vibration by implementing these lines: To start vibration:

Intent i=newIntent("START_VIBRATION");
                mActivity.sendBroadcast(i);

To Stop:

Intent i=newIntent("STOP_VIBRATION");
                mActivity.sendBroadcast(i);

Note: onDestroy() of MainActivity (in your case,Where you implement Broadcast Receiver,unregister BroadcastReceiver.)

Solution 2:

Set timer instance to null when you logout and then initialize it everytime user logged in the app. This will fix the "Timer was cancelled" related issues.

Solution 3:

Why do you need a static TimerTask.You can give like this which works fine for me.

timer.schedule(newTimerTask() {
        @Overridepublicvoidrun() {
        //your code
        }
        }, 0, 30000);

While logout use, timer.cancel(). Here you can simply cancel the timer.No need to cancel the TimerTask.

Post a Comment for "How To Cancel The Timer And Renew The Same Timer?"