簡體   English   中英

Android啟動活動

[英]Android Splash Activity

我對Flash有疑問,如果用戶允許,如何請求用戶啟用藍牙,等待它,然后繼續啟動主要活動,如果用戶拒絕,則退出應用程序,我的應用程序中包含以下代碼

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


}

查看代碼的正常狀態,您總是會進入主要活動。 沒有邏輯可以阻止這種情況。 代碼行

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

是總是執行的。 您需要處理意外的行為並做適當的事情(無論這是什么)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM