SURESH KUMAR
SURESH KUMAR

Reputation: 403

call another application from non activity class

I want to call another application from one.My call to another application is from non activity class.but the getPackageManager() shows the error like this The method getPackageManager() is undefined for the type AppLauncher. I imported the package manager also,still it shows the same error.

Is it possible to call another app from non activity class?thanks in advance.

Upvotes: 1

Views: 1993

Answers (3)

yrajabi
yrajabi

Reputation: 647

You need a context to call getPackageManager() on it. Get the context from an activity in your app:

in onCreate block of your main activity:

AppLauncher.setContext(this.getBaseContext));

in your Non-Activity Class (AppLauncher):

Context context;
public static void setContext(Context context) {
    this.context = context;
}

in your Non-Activity Class where you want to call getPackageManager():

context.getPackageManager();

Tip: if you are developing in eclipse on windows, always use Ctrl+Shift+O to automatically import missing and required packages.

Upvotes: 1

Remi Peuvergne
Remi Peuvergne

Reputation: 279

Those methods (such as getPackageManager() or startActivity()) are members of a context. You need to pass either your main activity or its Context to the other class.

BTW I don't get why you need package manager to launch another application. What about calling startActivity() with the corresponding intent ? (but I guess there are good reasons for doing it your way)

Upvotes: 0

Shrikant Ballal
Shrikant Ballal

Reputation: 7087

Pass your activity context to non-activity class. And from that context call the different application.

Upvotes: 2

Related Questions