編輯:關於Android編程
其實手機在網絡方面的的監聽也比較重要,有時候我們必須實時監控這個程序的實時網絡狀態,android在網絡斷開與連接的時候都會發出廣播,我們通過接收系統的廣播就可以實現網絡的監聽。
1、添加訪問網絡和獲取網絡狀態的權限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.INTERNET" />
2、新建一個Application類並繼承android.app.Application 申明變量保存網絡狀態
package com.javen.application; import com.javen.netbroadcast.NetUtil; public class Application extends android.app.Application { private static Application mApplication; public static int mNetWorkState; public static synchronized Application getInstance() { return mApplication; } @Override public void onCreate() { super.onCreate(); mApplication = this; initData(); } public void initData() { mNetWorkState = NetUtil.getNetworkState(this); } }
3、新建NetBroadcastReceiver類繼承BroadcastReceiver 並實現onReceive方法 在onReceive方法通知接口完成加載
package com.javen.receiver; import java.util.ArrayList; import com.javen.application.Application; import com.javen.netbroadcast.NetUtil; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class NetBroadcastReceiver extends BroadcastReceiver { public static ArrayList<netEventHandler> mListeners = new ArrayList<netEventHandler>(); private static String NET_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE"; @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(NET_CHANGE_ACTION)) { Application.mNetWorkState = NetUtil.getNetworkState(context); if (mListeners.size() > 0)// 通知接口完成加載 for (netEventHandler handler : mListeners) { handler.onNetChange(); } } } public static abstract interface netEventHandler { public abstract void onNetChange(); } }
4、在應該的Activity中實現netEventHandler接口
package com.javen.netbroadcast; import com.javen.receiver.NetBroadcastReceiver; import com.javen.receiver.NetBroadcastReceiver.netEventHandler; import com.javen.ui.base.BaseActivity; import android.os.Bundle; import android.view.Menu; public class MainActivity extends BaseActivity implements netEventHandler{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); NetBroadcastReceiver.mListeners.add(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onNetChange() { // TODO Auto-generated method stub if (NetUtil.getNetworkState(this) == NetUtil.NETWORN_NONE) { T.showLong(this, R.string.net_err); }else { T.showLong(this, "網絡可以使用"); } } }
5、判斷網絡的工具類
package com.javen.netbroadcast; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo.State; public class NetUtil { public static final int NETWORN_NONE = 0; public static final int NETWORN_WIFI = 1; public static final int NETWORN_MOBILE = 2; public static int getNetworkState(Context context) { ConnectivityManager connManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); // Wifi State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI) .getState(); if (state == State.CONNECTED || state == State.CONNECTING) { return NETWORN_WIFI; } // 3G state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) .getState(); if (state == State.CONNECTED || state == State.CONNECTING) { return NETWORN_MOBILE; } return NETWORN_NONE; } }
以上所述是小編給大家介紹的Android 網絡狀態實時監聽代碼實例(一),希望對大家有所幫助!
前言:最近因為某項目cpu,內存的使用率實在讓人不敢恭維;手機發燙,電量下降已經讓用戶無法忍受;頻繁快速迭代發版導致各種性能問題突出;由於之前產品不考慮低端手機情況,低端
有關HTC One X9手機的root教程還沒有給大家分享呢,接下來就來給大家說說有關這個手機的root教程了,這個root教程是利用卡刷的方式進行root
今天看了鴻洋_大神在慕課網講的qq5.0側滑菜單。學了不少的知識,同時也佩服鴻洋_大神思路的清晰。看了教程課下也自己實現了一下。代碼幾乎完全相同 別噴我啊。。沒辦法 o(
效果圖源碼KqwOpenCVBlurDemo阈值化是一種將我們想要在圖像中分析的區域分割出來的方法。我們把每個像素值都與一個預設的阈值做比較,再根據比較的結果調整像素值。