2015-04-03 73 views
0

我对Flash有疑问,如何要求用户启用蓝牙,等待它,然后继续启动主要活动(如果用户允许),或者如果用户拒绝,则退出应用程序,我有以下代码我的应用程序Android Splash Activity

// Library from Android 
import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.ProgressBar; 
import android.widget.Toast; 

// User defined library 
import java.io.IOException; 

import eu.atriaparis.support.ATRIAExceptions.BluetoothDeviceException; 
import eu.atriaparis.support.R; 
import eu.atriaparis.ledplayfinal.MainControllerActivity; 


// Starting Splash Activity for loading other background process. before main. 
// Edited Harsha S, for ATRIA Paris - 2015 
public class SplashActivity extends Activity { 

    private static String TAG = SplashActivity.class.getName(); 
    private static long SLEEP_TIME = 3;       //Max Loading Time 
    private final static int REQUEST_ENABLE_BT = 22; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); // Removes title bar 
     this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // Removes notification bar 

     setContentView(R.layout.activity_splash); 

     // Start timer and launch main activity 
     IntentLauncher launcher = new IntentLauncher(); 
     launcher.start(); 

     // Wait for the launcher thread to complete. 

    } 

// @Override 
// public void onActivityResult(int requestCode, int resultCode, Intent data) { 
//  Log.d(TAG,"onActivityResult"); 
//  switch (requestCode) { 
//   case REQUEST_ENABLE_BT: 
//    if (resultCode == Activity.RESULT_CANCELED) { 
//     // User did not enable Bluetooth or an error occurred 
//     finish(); 
//    } 
//    break; 
//   default: 
//    super.onActivityResult(requestCode, resultCode, data); 
//    break; 
//  } 
// } 

    // Internal Class which loads all the other background activity and then starts main thread. 
    // ATRIA Paris - 2015 
    private class IntentLauncher extends Thread { 

     private ProgressBar progressBar; 
     private int progressStatus = 0; 

     @Override 
     /** 
     * Sleep for some time and than start new activity. 
     */ 
     public void run() { 

      // Progress 
      progressBar = (ProgressBar) findViewById(R.id.progressBar1); 

      // Network 
      BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

      // For Error Handling 
      BluetoothDeviceException exception; 


      try { 

       // Status 1, Check device and bluetooth status 
       Thread.sleep(SLEEP_TIME*1000/3); 
       progressStatus = 20; 
       progressBar.setProgress(progressStatus); 

       // Status 2, Check Network Status 
       if (mBluetoothAdapter == null) { 
        Toast.makeText(getBaseContext(), getResources().getString(R.string.BluetoothAdapterNotFound), Toast.LENGTH_SHORT).show(); 
        Thread.sleep(SLEEP_TIME*1000/3); 
        throw new BluetoothDeviceException("Bluetooth Module Not Found"); 
       } 
       if (!mBluetoothAdapter.isEnabled()) { 
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
        Thread.sleep(SLEEP_TIME*1000/3); 
        Log.d("Splash", "Bluetooth Status + "+ mBluetoothAdapter.isEnabled()); 
       } 
       progressStatus = 50; 
       progressBar.setProgress(progressStatus); 

       // Status 3 
       Thread.sleep(SLEEP_TIME*1000/3); 
       progressStatus = 75; 
       progressBar.setProgress(progressStatus); 

       // Final Activity 
       // Load background activities 
       Thread.sleep(SLEEP_TIME*1000/3); 
       progressStatus = 100; 
       progressBar.setProgress(progressStatus); 

       // Start main activity 
       Intent intent = new Intent(SplashActivity.this, MainControllerActivity.class); 
       SplashActivity.this.startActivity(intent); 
       SplashActivity.this.finish(); 

      } catch (Exception e) { 
       Log.e(TAG, e.getMessage()); 
      } 
     } 
    } 


} 
+0

你是什么意思的“在Flash”? – RaphMclee 2015-04-10 10:47:26

+0

喜欢加载屏幕或启动屏幕, – harsha 2015-08-31 17:08:25

+0

你能更具体地处理你的问题吗?你有什么尝试,什么不行? – RaphMclee 2015-09-09 10:47:48

回答

0

查看代码正常情况下,您始终登录主要活动。没有任何逻辑来阻止这一点。 代码行

// Start main activity Intent intent = new Intent(SplashActivity.this, MainControllerActivity.class); SplashActivity.this.startActivity(intent); SplashActivity.this.finish();

是一直在执行。你需要对待意外的行为并做适当的事情(不管这是什么)。