Create the class as follows and add your functions here:
package com.mytest;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class MyGlobals{
Context mContext;
public MyGlobals(Context context){
this.mContext = context;
}
public String getUserName(){
return "test";
}
public boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null) {
return false;
} else
return true;
}
}
Then declare an instance in your activity:
MyGlobals myGlog;
Then initialize and use the methods from this global class:
myGlog = new MyGlobals(getApplicationContext());
String username = myGlog.getUserName();
boolean inConnected = myGlog.isNetworkConnected();
Permission required in manifest file:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Thank.
source
share