欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-05-21 21:20:41
android欢迎页自动跳转及手机网络检测,具体实现代码如下所示: 在1.2s后从欢迎页(welcome)跳转到登陆页(login_activity),并进行手机网络检测...

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

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

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

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

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判断手机是否联网及自动跳转功能(收藏版)

总结

以上所述是小编给大家介绍的android判断手机是否联网及自动跳转功能,希望对大家有所帮助