Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9238845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:51:17+00:00 2026-06-18T07:51:17+00:00

I am creating an app that vibrate and beep every 30 sec and when

  • 0

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 must vibrate and beep for every 30 sec until I log out

In my app I am using TimerTask for this implementation

this is the code for vibrate and beep 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’m using in logout section

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

Note: I also removed the Task.cancel(); but still I am getting same error

My vibrate working fine before logout and again login I am geting 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 any one help me with this coding. Where did I go wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:51:18+00:00Added an answer on June 18, 2026 at 7:51 am

    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;
    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 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:

    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 vibration by implementing these lines:
    To start vibration:

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

    To Stop:

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

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating an app that should list the date and time when a button
I'm creating an app that I want to license out to businesses on a
I am creating an app that accesses a database. On every database access, the
I am creating an app that will point a simple arrow in the direction
I am creating an app that is a ticket sales system. On the checkout
I am creating an app that allows users to create and apply for jobs.
I am creating an app that alerts user if he has been stationary for
I'm creating an app that uses ActionBarSherlock. The app consists of three tabs, and
I am creating an app that requires some settings to be set first before
I'm creating and app that will rely on a database, and I have all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.