JRE.exe
JRE.exe

Reputation: 799

call another intent after one second in android

i want to enable bluetooth and disable it after one second. but i have no idea how to do it, i have tried this but it is not doing anything, just enabling bluetooth. the reason i am doing this is because i actually wanted to Just disable bluetooth. but it only works if you have also Enabled* it using intent in the app itself, else it force closes (if it is enabled by notification bar)

i was using this code to Enable/disable bluetooth

  public void optimize(View view) {
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
         if (mBluetoothAdapter == null) {
             // Device does not support Bluetooth

             }else{

             if (!mBluetoothAdapter.isEnabled()) {

                mBluetoothAdapter.enable();

             }else{

              mBluetoothAdapter.disable();    

             }

             }

code i am using now

    public void optimize(View view) {
        final BluetoothAdapter mBluetoothAdapter =BluetoothAdapter.getDefaultAdapter();
         if (mBluetoothAdapter == null) {
             // Device does not support Bluetooth

             }else{

             if (!mBluetoothAdapter.isEnabled()) {

                mBluetoothAdapter.enable();

             {Thread mythread = new Thread() {
                    public void run()
                    {
                        try
                        {
                            sleep(1000);
                        }
                        catch (Exception e)
                        {
                        }
                        finally
                        {


              BluetoothAdapter mBluetoothAdapter =BluetoothAdapter.getDefaultAdapter();
              mBluetoothAdapter.disable();    


                        }
                    }
                };

            }

    }           
    }

Upvotes: 0

Views: 142

Answers (5)

Santosh Kathait
Santosh Kathait

Reputation: 1444

Do something like this

private static int SPLASH_TIME_OUT = 1000; //for one sec (You can increase it to see the reflection)

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    
mBluetoothAdapter.enable();

new Handler().postDelayed(new Runnable() {

    @Override
    public void run() {
         BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    
         if (mBluetoothAdapter.isEnabled()) {//disable bluetooth if it is already enabled
                mBluetoothAdapter.disable(); 
            }
    }
}, SPLASH_TIME_OUT);

and in manifest file set permisssion

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

Upvotes: 0

Guillermo Merino
Guillermo Merino

Reputation: 3257

You can just start a new thread, you should not execute this code in the UI thread (since you are sleeping the thread for a second).

The code would be:

 public void optimize(View view) {
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
        final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter
            .getDefaultAdapter();
        if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()) {

            mBluetoothAdapter.enable();
            try {
            Thread.sleep(1000);
            } catch (InterruptedException e) {
            e.printStackTrace();
            }
            mBluetoothAdapter.disable();
        }
        }
    });
    thread.start();
    }

Upvotes: 0

M.Khouli
M.Khouli

Reputation: 4122

Try this

            Timer timer = new Timer();

        timer.schedule(new TimerTask() {

                @Override
                public void run() {

                                      //turn off code should be here 
                                  BluetoothAdapter mBluetoothAdapter =BluetoothAdapter.getDefaultAdapter();
                                  mBluetoothAdapter.disable(); 

            }, 1000);

Upvotes: 1

sai
sai

Reputation: 558

Try this, use Android handler's postdelayed().

It will execute code inside the run() block after the specified time(in milli seconds.)

public void optimize(View view) {
    final BluetoothAdapter mBluetoothAdapter =BluetoothAdapter.getDefaultAdapter();
     if (mBluetoothAdapter == null) {
         // Device does not support Bluetooth

         }else{

         if (!mBluetoothAdapter.isEnabled()) {

            mBluetoothAdapter.enable();

               // Instead of run Thread , use Handler's Postdelayed method as,
               Handler mHandler = new Handler();
               mHandler.postDelayed(new Runnable() {

            @Override
            public void run() {
                      // Runs this after 1 second
                BluetoothAdapter mBluetoothAdapter =BluetoothAdapter.getDefaultAdapter();
          mBluetoothAdapter.disable();    
            }
        }, 1000); // After 1 second it executes code inside run()

        }

}           
}

Upvotes: 0

Shini
Shini

Reputation: 573

Try this

    protected int _splashTime = 1000;

    Thread mythread = new Thread() {
        public void run() {
            try {
                int waited = 0;
                while (waited < _splashTime) {
                    sleep(100);

                        waited += 100;

                }
            } catch (Exception e) {
            } finally {
                //

            }
        }
    };
    mythread.start();

Upvotes: 0

Related Questions