有没有一种方法可以在应用程序首次启动时以及之后执行不同的操作onresume()

时间:2019-05-31 10:57:10

标签: android android-lifecycle onresume

我正在制作一个需要wifi连接才能与该应用进行通信的应用,以检查它是否已连接,并从初始屏幕继续进入mainActivity或要求您连接,然后一个按钮通过动画从背景出现,并且该按钮将您带走到wifi设置。但是,当我返回到我的应用程序时,它会堆叠在页面中,仅当我重新启动应用程序时它才起作用,它将检查并继续

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 :(得分:0)

您可以做02件事:-

01:-从onResume()方法中调用initialApp()方法。

02:-使用处理程序检查设备是否在一定时间(例如100 ms)后重复连接到wifi。如果已连接,则调用initialApp()方法。