このコードを使用して、Broadcast
Wifi または GPRS を利用できるようにします
BroadcastReceiver br;
br = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION))
{
return;
}
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
NetworkInfo aNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if (!noConnectivity)
{
if (aNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
{
Toast.makeText(getApplicationContext(), "Mobile Connected", Toast.LENGTH_LONG).show();
//Handle connected case
}
if (aNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI)
{
Toast.makeText(getApplicationContext(), "WIFI Connected", Toast.LENGTH_LONG).show();
}
}
else
{
if ((aNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) ||
(aNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI))
{
Toast.makeText(getApplicationContext(), "Not Connected", Toast.LENGTH_LONG).show();
}
}
}
};
androidmanifest.xml で次のアクセス許可を提供します。
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
と を忘れないでregister
ください。unregister
BroadcastReceiver