【问题标题】:Android Splash ActivityAndroid Splash 活动
【发布时间】:2015-04-03 05:06:16
【问题描述】:

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


}

【问题讨论】:

  • “在 Flash 上”是什么意思?
  • 喜欢加载屏幕或启动屏幕,
  • 您的问题能具体一点吗?你试过什么,什么不起作用?
  • 在大多数现代应用程序中,当他们启动时,他们有加载屏幕(启动画面),我在这个项目上工作了一段时间,但是当用户启动我们的应用程序时,他登陆启动画面(加载屏幕),然后我们在后台做了一些事情然后进入主活动,我不知道如何控制这个动作,我可以在这个应用程序进入主活动后启动一个活动启动(加载活动),即使有一些问题正在加载。
  • 在上面的活动中,例如我有我们的应用程序需要的检查网络、蓝牙等的背景信息,如果用户没有打开它,应用程序应该在启动/加载活动时停止,但它不会登陆主要活动。

标签: java android-activity splash-screen android-bluetooth


【解决方案1】:

查看代码很正常,您总是会进入主要活动。没有逻辑可以阻止这种情况。 代码行

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

总是执行。您需要处理意外行为并做适当的事情(无论是什么)。

【讨论】:

    猜你喜欢
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多