Android中怎么判斷手機(jī)是否聯(lián)網(wǎng),很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
成都創(chuàng)新互聯(lián)是一家專業(yè)提供龍圩企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、H5開發(fā)、小程序制作等業(yè)務(wù)。10年已為龍圩眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。
android歡迎頁自動(dòng)跳轉(zhuǎn)及手機(jī)網(wǎng)絡(luò)檢測,具體實(shí)現(xiàn)代碼如下所示:
在1.2s后從歡迎頁(welcome)跳轉(zhuǎn)到登陸頁(Login_Activity),并進(jìn)行手機(jī)網(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); //手機(jī)網(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, "手機(jī)網(wǎng)絡(luò)已開啟!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(context, "手機(jī)網(wǎng)絡(luò)未開啟!", Toast.LENGTH_SHORT).show(); } }}// An highlighted blockvar foo = 'bar';
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。