How to cancel the timer and extend the same timer?

I am creating an application that vibrates and beeps every 30 seconds, and when I exit the system, the vibration and the sound should be canceled, and when I register the vibrate, the sound should resume.

NOTE: it should vibrate and beep every 30 seconds until I log out

In my application, I use TimerTask for this implementation

this is the code for vibration and sound using TimerTask

 static TimerTask Task; final static Handler handler = new Handler(); static Timer t = new Timer(); public static void vib() { Task = new TimerTask() { public void run() { handler.post(new Runnable() { public void run() { Vibrator vibrator = (Vibrator) ApplicationUtils.getContext().getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(3000); playSound(); Log.d("TIMER", "Timer set on"); } }); } }; t.schedule(Task, 0, 30000); } 

This is the code I use in the exit section

 public void stopvib() { if (Task != null) { // Log.d("TIMER", "timer canceled"); t.cancel(); Task.cancel(); } } 

Note. I also deleted Task.cancel(); but still getting the same error

My vibration works fine before logging out and logging back in. I get an error

 java.lang.IllegalStateException: Timer was cancelled at java.util.Timer.scheduleImpl(Timer.java:562) at java.util.Timer.schedule(Timer.java:481) at com.vib(AlertListActivity.java:724) 

can someone help me with this coding. Where am I wrong?

+1
source share
3 answers

I recently ran this code and worked great. This can be achieved using a broadcast receiver. You must complete a separate CustomTimer task that extends TimerTask:

 Activity mActivity=null; public MyCustomTimer(Activity mActivity) { this.mActivity=mActivity; } @Override public void run() { this.mActivity.runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(mActivity, "Write you code here",Toast.LENGTH_LONG).show(); Log.d("MyCustomTimer","Call"); } }); } 

After that, you need to implement BroadCast Receive in this class, where you want to implement the "vib ()" method .: Say, in my case (for example, for example) there is MainActivity:

 public class MainActivity extends Activity { private MyCustomTimer myCustomTimer = null; BroadcastReceiver mBr_Start = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("START_VIBRATION")) { System.out.println("onreceive :START_VIBRATION"); vib(); } } }; BroadcastReceiver mBr_Stop = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("STOP_VIBRATION")) { stopVibration(); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); IntentFilter mIntentFilter = new IntentFilter(); mIntentFilter.addAction("START_VIBRATION"); registerReceiver(mBr_Start, mIntentFilter); IntentFilter mIntentFilter2 = new IntentFilter(); mIntentFilter2.addAction("STOP_VIBRATION"); registerReceiver(mBr_Stop, mIntentFilter2); Button b1 = (Button) findViewById(R.id.button1); b1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this, MySecondActivity.class) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(i); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } private void vib() { myCustomTimer = new MyCustomTimer(MainActivity.this); Timer timer = new Timer(); timer.scheduleAtFixedRate(myCustomTimer, 0, 30000); } private void stopVibration() { Log.d("MainActivity", "Before Cancel"); if (null != myCustomTimer) myCustomTimer.cancel(); Log.d("MainActivity", "After Cancel"); } } 

Now you can start or stop the vibration by following these lines: To start the vibration:

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

Stop:

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

Note: onDestroy () MainActivity (in your case, when you use a broadcast receiver, unregister BroadcastReceiver.)

+3
source

Set the timer instance to zero when logging out and then initialize it every time a user logs in to the application. This will fix the related timer problems.

0
source

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

 timer.schedule(new TimerTask() { @Override public void run() { //your code } }, 0, 30000); 

During logout timer.cancel(). Here you can simply cancel the timer. No need to cancel the TimerTask.

0
source

Source: https://habr.com/ru/post/1484887/


All Articles