What i want to do is create a java file that has various functions and I would like to use it across the whole project. For example check Internet Connection. Then I would like to call that function on each activity. Does anyone know how to do that?
6 Answers
Create Class like this 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;
// constructor
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) {
// There are no active networks.
return false;
} else
return true;
}
}
Then declare instance in your activity :
MyGlobals myGlog;
Then initialize and use methods from that global class :
myGlog = new MyGlobals(getApplicationContext());
String username = myGlog.getUserName();
boolean inConnected = myGlog.isNetworkConnected();
Permission required in your Manifest file :
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Thanks.
4 Comments
mContext.getSystemService for it.Create Utility class like this:
public final class AppUtils {
private AppUtils() {
}
@SuppressLint("NewApi")
public static void setTabColor(TabHost tabHost) {
int max = tabHost.getTabWidget().getChildCount();
for (int i = 0; i < max; i++) {
View view = tabHost.getTabWidget().getChildAt(i);
TextView tv = (TextView) view.findViewById(android.R.id.title);
tv.setTextColor(view.isSelected() ? Color.parseColor("#ED8800") : Color.GRAY);
view.setBackgroundResource(R.color.black);
}
}
}
Now, from any class of Android application, you can function of Apputils like this:
AppUtils.setTabColor(tabHost);
Comments
This may not be the best way to do this and there would be others who might suggest some better alternatives, but this is how I would do.
Create a class with all the functions and save it as maybe Utility.java.
Use an object of the Utility class throughout the code where ever you need to call any of the functions.
Utility myUtilObj = new Utility();
myUtilObj.checkInternet();
Or maybe make the functions static and you can simply use Utility.checkInternet() where ever you need to call it.
6 Comments
Utility myUtil = new Utility(context);. Then in the utility in the structor set context to the top of the class so is accessible through and the context variable can be usedJust create public class with static methods, something like this ...
package com.example.test1;
public class GlobalMethod {
public static String getHelloWorld() {
return "Hello, World!";
}
public static int getAppleCount() {
return 45;
}
}
Now from anywhere you can call the methods ...
GlobalMethod.getHelloWorld();
GlobalMethod.getAppleCount();
There are many ways to do though, check it out other answers. Hope this is helpful.
Comments
I am describing below how I achieved it. First, I created the following class in app/src/main/java/com/[my folder]/MyGlobals.java:
package [My package];
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class MyGlobals {
Context mContext;
// Constructor
public MyGlobals(Context context){
this.mContext = context;
}
public boolean checkInternetConnection() {
ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if ((cm != null) && (netInfo != null)) {
if (netInfo.isConnected()) {
return true;
}
}
return false;
}
}
Then in the classes where I needed to use the global functions, I declared these member variables:
MyGlobals myGlobals;
boolean checkInternetConnection;
In the portions of code where I needed to test internet connectivity, I used this:
myGlobals = new MyGlobals(getApplicationContext());
checkInternetConnection = myGlobals.checkInternetConnection();
if(checkInternetConnection == false){
Util.showToast([Name of my activity].this, getString(R.string.nointernet), getString(R.string.error), true, false);
return;
}