在android中一秒后调用另一个意图

时间:2014-05-07 12:48:32

标签: android android-intent

我想启用蓝牙并在一秒钟后禁用它。但我不知道怎么做,我试过这个,但它没有做任何事情,只是启用蓝牙。我这样做的原因是因为我其实想要禁用蓝牙。但它只有在你使用app在应用程序本身中启用它时才有效,否则强制关闭(如果通过通知栏启用)

我正在使用此代码启用/禁用蓝牙

  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();    

             }

             }

我正在使用的代码

    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();    


                        }
                    }
                };

            }

    }           
    }

5 个答案:

答案 0 :(得分:1)

试试这个

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

答案 1 :(得分:0)

做这样的事情

private static int SPLASH_TIME_OUT = 1000; //一秒钟(您可以增加它以查看反射)

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

并在清单文件集中使用

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

答案 2 :(得分:0)

试试这个

    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();

答案 3 :(得分:0)

试试这个,使用Android处理程序的postdelayed()。

它将在指定时间(以毫秒为单位)后在run()块内执行代码。

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()

        }

}           
}

答案 4 :(得分:0)

你可以启动一个新线程,你不应该在UI线程中执行这个代码(因为你正在睡眠线程一秒钟)。

代码如下:

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