Есть ли способ совершать различные действия с onresume () при первом запуске приложения и после - PullRequest
0 голосов
/ 31 мая 2019

Я создавал приложение, которому требуется соединение Wi-Fi для связи с приложением, которое проверяет, подключено ли оно и продолжает ли mainActivity на заставке, или запрашивает подключение, и с помощью анимации появляется кнопка на заднем плане. к настройке Wi-Fi. Но когда я возвращаюсь к своему приложению, это стек на странице, он работает только при перезапуске приложения, он проверит и продолжит

public class MainActivity extends AppCompatActivity {
NetworkInfo wifiCheck;                               // declaration for the wifi checker
Timer timer;                                         // we declared a timer

ImageView bgapp, call, chat, file, vid;
TextView wifitext;
LinearLayout imagesplash, menu;
Animation frombottom;
Button btnCreat;
Button btnConnect;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    frombottom = AnimationUtils.loadAnimation(this, R.anim.frombottom);

    bgapp = (ImageView) findViewById(R.id.bgapp);
    call = (ImageView) findViewById(R.id.call);
    chat = (ImageView) findViewById(R.id.chat);
    file = (ImageView) findViewById(R.id.file);
    vid = (ImageView) findViewById(R.id.vid);
    wifitext = (TextView) findViewById(R.id.wifitext);
    imagesplash = (LinearLayout) findViewById(R.id.imgsplash);
    menu = (LinearLayout) findViewById(R.id.menu);



    initialApp();




    btnCreat = findViewById(R.id.btnCreat);
    btnCreat.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Intent intent = new Intent(Intent.ACTION_MAIN, null);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            final ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.TetherSettings");
            intent.setComponent(cn);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);


        }

    });

    btnConnect = findViewById(R.id.btnConnect);
    btnConnect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
        }
    });

}


public void openConnectedActivity() {
    Intent intent = new Intent(this, TheActivity.class);
    startActivity(intent);
    finish();
}


@Override
protected void onResume() {
    super.onResume();
    Toast.makeText(this, "I just came back", Toast.LENGTH_SHORT).show();


}

private void initialApp(){

    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    wifiCheck = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);


    if (wifiCheck.isConnected()) {
        runIfConnected();
    } else {
        runIfnotConnected();
    }
}


private void runIfConnected(){
    // if it is connected it will do this
    imagesplash.animate().translationY(-350).setDuration(800).setStartDelay(8000);
    bgapp.animate().translationY(-490).setDuration(800).setStartDelay(8000);
    call.animate().alpha(0).setDuration(800).setStartDelay(8000);
    chat.animate().alpha(0).setDuration(800).setStartDelay(8000);
    file.animate().alpha(0).setDuration(800).setStartDelay(8000);
    vid.animate().alpha(0).setDuration(800).setStartDelay(8000);
    wifitext.animate().alpha(0).setDuration(800).setStartDelay(8000);

    menu.startAnimation(frombottom);

    // putting timer to start the Activity and kill the splash Screen

    timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            openConnectedActivity();
        }
    }, 2000);
}

private void runIfnotConnected(){
    // if id is not connected is will do this

    imagesplash.animate().translationY(-350).setDuration(800).setStartDelay(8000);
    bgapp.animate().translationY(-490).setDuration(800).setStartDelay(8000);
    call.animate().alpha(0).setDuration(800).setStartDelay(8000);
    chat.animate().alpha(0).setDuration(800).setStartDelay(8000);
    file.animate().alpha(0).setDuration(800).setStartDelay(8000);
    vid.animate().alpha(0).setDuration(800).setStartDelay(8000);
    wifitext.animate().alpha(0).setDuration(800).setStartDelay(8000);

    menu.startAnimation(frombottom);

}

}

1 Ответ

0 голосов
/ 31 мая 2019

Вы можете сделать 02 вещи: -

01: - вызвать метод initialApp () из метода onResume ().

02: - использовать обработчик, чтобы проверить, является ли устройствочерез определенное время (например, через 100 мс) подключается к wifi. Если он подключен, вызовите метод initialApp ().

...