Android判断手机是否联网及自动跳转功能(收藏版)

android欢迎页自动跳转及手机网络检测,具体实现代码如下所示:

在1.2s后从欢迎页(welcome)跳转到登陆页(Login_Activity),并进行手机网络检测

package com.example.amusic.welcome;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.example.amusic.monitor.NetworkChangeReceiver;
import com.example.amusic.R;
import java.util.Timer;
import java.util.TimerTask;
public class welcome extends AppCompatActivity {
  private final int SKIP_DELAY_TIME = 1200;
  private NetworkChangeReceiver networkChangeReceiver;
  private IntentFilter intentFilter;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);
    //手机网络检测
    intentFilter = new IntentFilter();
    intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
    networkChangeReceiver = new NetworkChangeReceiver();
    registerReceiver ( networkChangeReceiver, intentFilter);
    Timer time = new Timer();
    TimerTask task = new TimerTask(){
      @Override
      public void run() {
        startActivity(new Intent(welcome.this,Login_Activity.class));
        finish();
        //onDestroy();
      }
    };
    time.schedule(task, SKIP_DELAY_TIME);
  }
  protected void onDestroy(){
    super.onDestroy();
    unregisterReceiver( networkChangeReceiver);
  }
}
// An highlighted block
var foo = 'bar';

public class NetworkChangeReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
    ConnectivityManager connectivityManager = (ConnectivityManager)
        context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo!= null&&networkInfo.isAvailable()) {
      Toast.makeText(context, "手机网络已开启!", Toast.LENGTH_SHORT).show();
    } else {
      Toast.makeText(context, "手机网络未开启!", Toast.LENGTH_SHORT).show();
    }

  }

}
// An highlighted block
var foo = 'bar';

运行结果

总结

以上所述是小编给大家介绍的Android判断手机是否联网及自动跳转功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对呐喊教程网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。