我正在开发一个消息传递应用程序,无论如何还是有任何API来识别android中的服务提供商名称,也可以有人帮助我如何以编程方式检查应用程序是否使用WIFI连接或3G服务?请帮帮我。
答案 0 :(得分:1)
通过此代码段代码,您可以检查设备中的所有类型网络:
public boolean isNetworkAvailable() {
ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
return false;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
然后通过以下方式测试您的网络:
if (isNetworkAvailable()) {
//Network availabe...
} else {
Toast alrtMsg = Toast.makeText(this, "No network connection available !!!", Toast.LENGTH_LONG);
alrtMsg.setGravity(Gravity.CENTER, 0, 0);
alrtMsg.show();
}
答案 1 :(得分:1)
这是一个非常好的类来获取当前的连接类型并通过connexion update的监听器提醒:
package com.android.aft.AFCoreTools;
import java.util.ArrayList;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class AFNetworkMonitoring {
public enum NetworkMode {
NotConnected,
ConnectedToWifi,
ConnectedTo3G,
};
public interface NetworkMonitoringInterface {
public void onNetworkUpdate(NetworkMode mode);
}
// Network connection
public NetworkMode mode = NetworkMode.NotConnected;
// Connection listener
private BroadcastReceiver mConnReceiver;
// Listeners
private ArrayList<NetworkMonitoringInterface> mListeners;
// Context
private Context mContext;
public AFNetworkMonitoring(Context ctx) {
this(ctx, null);
}
public AFNetworkMonitoring(Context ctx, NetworkMonitoringInterface listener) {
mContext = ctx;
addListener(listener);
// Create network state update
mConnReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
updateNetworkConnectionStatus();
}
};
mContext.registerReceiver(mConnReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
protected void updateNetworkConnectionStatus() {
ConnectivityManager conMgr = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = conMgr.getActiveNetworkInfo();
if (info == null || !info.isConnected() || !info.isAvailable()) {
DebugTools.d("Lost connection detected");
mode = NetworkMode.NotConnected;
notifyListener();
return;
}
NetworkInfo infoWifi = conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (infoWifi.isConnected() || infoWifi.isAvailable()) {
DebugTools.d("On connection wifi detected");
mode = NetworkMode.ConnectedToWifi;
notifyListener();
return;
}
DebugTools.d("On connection 3G detected");
mode = NetworkMode.ConnectedTo3G;
notifyListener();
}
protected void notifyListener() {
if (mListeners == null)
return ;
new AsyncTaskWrapper<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
// Nothing to do
return null;
}
@Override
protected void onPostExecute(Void result) {
for (NetworkMonitoringInterface l: mListeners)
l.onNetworkUpdate(mode);
};
}.executeParallel();
}
public void addListener(NetworkMonitoringInterface listener) {
if (listener == null)
return ;
if (mListeners == null)
mListeners = new ArrayList<AFNetworkMonitoring.NetworkMonitoringInterface>();
mListeners.add(listener);
}
public void removeListener(NetworkMonitoringInterface listener) {
if (mListeners == null)
return ;
mListeners.remove(listener);
}
}
答案 2 :(得分:0)
LocationManager服务=(LocationManager)getSystemService(getActivity()。LOCATION_SERVICE);
boolean GPS_PROVIDER = service.isProviderEnabled(LocationManager.GPS_PROVIDER); boolean NETWORK_PROVIDER = service.isProviderEnabled(LocationManager.NETWORK_PROVIDER);