Issue starting activity from non-activity class

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);

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);

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

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

Answers


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

Edit : Take some static variable like this...

public static Context mContext;

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

mContext = getBaseContext();

& 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.setData(Uri.parse("tel:"+MapActivity.phonenumber0));
MapActivity.getContext().startActivity(callIntent);

Need Your Help

Using AVPlayer to play DRM protected songs

iphone ios xcode cocoa-touch

For various reasons I need to use AVPlayer to play music from the IOS iTunes library. When I try to retrieve the URL for a DRM protected song the result is null. Has anyone found a way to achieve...

Implement a inline scrollviewer in Windows Store app with HTML5/JS

html css html5 windows-8 windows-store-apps

I am developing a Windows 8 Store app with html5/JS, a reading app. The content page looks like :

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.