Android中怎么判斷手機是否聯(lián)網(wǎng),很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
創(chuàng)新互聯(lián)公司主營港南網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶App定制開發(fā),港南h5微信小程序開發(fā)搭建,港南網(wǎng)站營銷推廣歡迎港南等地區(qū)企業(yè)咨詢android歡迎頁自動跳轉(zhuǎn)及手機網(wǎng)絡(luò)檢測,具體實現(xiàn)代碼如下所示:
在1.2s后從歡迎頁(welcome)跳轉(zhuǎn)到登陸頁(Login_Activity),并進行手機網(wǎng)絡(luò)檢測
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); //手機網(wǎng)絡(luò)檢測 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 blockvar 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, "手機網(wǎng)絡(luò)已開啟!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(context, "手機網(wǎng)絡(luò)未開啟!", Toast.LENGTH_SHORT).show(); } }}// An highlighted blockvar foo = 'bar';
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。