简体   繁体   English

有没有办法在应用程序第一次启动时和之后对onresume()进行不同的操作

[英]Is there a way to make different actions onresume() the first time the app start and after

I was making an app that needs a wifi connection to communicate with the app checks if it is connected and continue to mainActivity from the splash screen or asks you to connect and a button appears from back ground by animation and the button takes you to wifi setting. 我正在创建一个需要wifi连接的应用程序,以便与应用程序检查进行通信(如果已连接)并从启动屏幕继续执行mainActivity或要求您连接并通过动画从后台显示一个按钮,该按钮将带您进入wifi设置。 But when I return to my app it is stack in the page it only works when I restart the app it will check and continue 但是,当我返回到我的应用程序时,它是在页面中的堆栈,它只在我重新启动应用程序时才会检查并继续

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

}

}

You could do 02 things:- 你可以做02件事: -

01:-Call the initialApp() method from the onResume() method. 01:从onResume()方法中调用initialApp()方法。

02:-Use a handler to check if the device is connected to wifi repeatedly after a certain duration(for example 100 ms).If it is connected then call the initialApp() method. 02: - 使用处理程序检查设备是否在一定时间后(例如100 ms)重复连接到wifi。如果已连接,则调用initialApp()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM