Reputation: 1
As a beginner, I am trying to develop an Android
application, which is fetching data from server database.
Here, if Wi-Fi
is not available at place, I need to turn on a 3G
- or a 2G
-data, which is provided on the Android device automatically or by a dialog box to turn on the mobile network.
Upvotes: 0
Views: 174
Reputation: 21551
Try this:
findViewById(R.id.button).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
// Check for n/w connection
if (!NetworkService
.isNetWorkAvailable(CurrentActivity.this)) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
CurrentActivity.this);
// set title
alertDialogBuilder.setTitle("Warning");
alertDialogBuilder
.setMessage("Check your network connection and try again.");
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setNegativeButton(
"Ok",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
} else {
// Do your action if network exists
Intent i = new Intent(getApplicationContext(),NextActivity.class);
startActivity(i);
}
}
});
NetworkService.java:
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public final class NetworkService {
private NetworkService() {}
public static boolean isNetWorkAvailable(final Context contextActivity) {
ConnectivityManager conMgr = (ConnectivityManager) contextActivity.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnected();
}
}
Add these permissions in the manifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
Upvotes: 0
Reputation: 5379
// Checking for all possible internet providers
public boolean isConnectingToInternet(){
ConnectivityManager connectivity =
(ConnectivityManager) getSystemService(
Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
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;
}
Also specify these permission:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
Upvotes: 0
Reputation: 510
How to check for WIFI internet connection:
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()) {
// Do whatever
}
Add Permission in manifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
How to Check for for mobile internet connections (3G/LTE):
boolean is3g = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
.isConnectedOrConnecting();
// Checks if Mobile network is available ...
if(!is3g){
// IF MOBILE CONNECTION NOT AVAILBLE, OPEN SYSTEM SETTINGS FOR MOBILE NETWORKS
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS);
startActivity(intent);
}
Hope this helps, please mark it as accepted answer if it worked for you.
Upvotes: 1
Reputation: 1362
to check network is available or not use following block of code
private boolean isNetworkAvailable(Context mContext) {
// TODO Check network connection
ConnectivityManager connMgr = (ConnectivityManager) mContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
return true;
else
return false;
}
and next do following check condition for if WIFI not available :-
private void setMobileDataEnabled(Context context, boolean enabled) {
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(conman);
final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
}
you also required this
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
Upvotes: 0
Reputation: 2326
use this code to check the availability of network:-
public class CheckConnection {
private Context _context;
public CheckConnection(Context context){
this._context = context;
}
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
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;
}
}
Upvotes: 0
Reputation: 844
you can check if the network is available or not, by this class
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context){
this._context = context;
}
/**
* Checking for all possible Internet providers
* **/
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
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;
}
}
check for internet
ConnectionDetector conn = new ConnectionDetector(youractivity.this);
if(conn.isConnectingToInternet()){
// call network operations
}else{
// Toast
}
hope this helps you
Upvotes: 0