How to use getBaseContext () in a class that does not extend the action

I am creating a module that is distributed from another class, but I need to use getBaseContext (). how can i use it in my own module? If I need to start an action, then how to do it, if not, how to solve the problem thanks

public class TelcoModule extends KrollModule { ... // Methods @Kroll.method public String GetTelco() { TelephonyManager tm =(TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE); String operatorName = tm.getNetworkOperatorName(); return operatorName ; } } 
+6
source share
2 answers

Modify GetTelco to include the context parameter. Then call it using your accessible context from anywhere

 public String GetTelco(final Context context) { TelephonyManager tm =(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); String operatorName = tm.getNetworkOperatorName(); } 

An example of its call:

 someView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String telcoName = myTelcoInstance.GetTelco(v.getContext()) } }); 
+3
source

What about...

 Context ctx = getActivity().getApplicationContext(); 
+1
source

All Articles