从非活动类发出启动活动 [英] Issue starting activity from non-activity class

查看:83
本文介绍了从非活动类发出启动活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android的菜鸟,并且我有一个Map Activity也使用OverlayItems.在我的覆盖类的onButtonTap方法中,我想执行startActivity,以便随后可以使用intent.ACTION_CALL.

I am a noob to android and I have a Map Activity that also uses OverlayItems. Within the onButtonTap method of my overlay class, I want to execute startActivity so i can then use intent.ACTION_CALL.

Intent callIntent = new Intent(Intent.ACTION_CALL);   
callIntent.setData(Uri.parse("tel:"+MapActivity.phonenumber0));
startActivity(callIntent);

在上面的代码中,我被要求为startActivity(Intent)创建一个我不理解的方法.而当我尝试...

in the code above i am asked to create a method for startActivity(Intent), which I don't understand. and when i try...

Intent callIntent = new Intent(Intent.ACTION_CALL);   
callIntent.setData(Uri.parse("tel:"+MapActivity.phonenumber0));
MapActivity.startActivity(callIntent);

它说我不能对非静态方法的非静态引用进行静态引用.而且,当我尝试使用对象的上下文(即被点击的按钮)时,将不允许我这样做.

It says i cannot make a static reference to a non static reference to a non-static method. And when I try to use the context of the object, which is the button being tapped it won't allow me to do so.

Intent callIntent = new Intent(Intent.ACTION_CALL);   
callIntent.setData(Uri.parse("tel:"+MapActivity.phonenumber0));
ContextObj.startActivity(callIntent);

当然,将此代码块移至主要的Activity需要一个静态方法,该方法会显示自己的一系列问题.

And of course moving this block of code to the main Activity requires a static method which presents its own set of issues.

如何为startActivity设置适当的上下文?任何帮助,我们将不胜感激.

How can set the appropriate context for startActivity? Any help is greatly appreciated.

推荐答案

您可以像这样在MapActivity类中创建方法以获取上下文...

you can create method in your MapActivity class like this to get context...

采取这样的静态变量...

Edit : Take some static variable like this...

public static Context mContext;

在Activity的onCreate()方法中为其分配基本上下文...

In Activity's onCreate() method assign base context to it...

mContext = getBaseContext();

&返回mContext ...

& return mContext...

public static Context getContext() {
    return mContext;
}

&调用它到您的非活动类中以开始活动...

& call it in to your non activity class to start activity...

Intent callIntent = new Intent(Intent.ACTION_CALL);   
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
callIntent.setData(Uri.parse("tel:"+MapActivity.phonenumber0));
MapActivity.getContext().startActivity(callIntent);

这篇关于从非活动类发出启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆