如何在类中使用getBaseContext()而不是扩展Activity

Ehs*_*san 6 android module titanium

我正在创建一个从另一个类扩展的模块,但我需要使用getBaseContext().我怎样才能在自己的模块中使用它?如果我必须运行活动,那么如果不是如何解决问题该怎么办谢谢

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 ;
          }
}
Run Code Online (Sandbox Code Playgroud)

pet*_*tey 3

更改GetTelco为包含上下文参数。然后使用您的可用上下文从任何地方调用它

public String GetTelco(final Context context)
{
       TelephonyManager tm =(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
       String operatorName = tm.getNetworkOperatorName();
}
Run Code Online (Sandbox Code Playgroud)

调用示例:

someView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String telcoName = myTelcoInstance.GetTelco(v.getContext())
    }
});
Run Code Online (Sandbox Code Playgroud)