0

I am currently working on a project that i am trying to add an icon making competition and the winner will receive a special app plugin that only the contest winner will get and i would like my app to check if the package(special app plugin) exists on the users device and if it does i would like the app to display an alternate display on run-time.Would i use "if" and "else" statements to achieve this and if so how would i go about this and Thanks in advanced.

Note:I have successfully made the app load a different layout depending on the android version so i have a little bit of an idea but need some help.

Shayden117
  • 252
  • 1
  • 3
  • 17

3 Answers3

2

Check this to know app is installed or not..

public class Example extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //Put the package name here...
        boolean installed  =   appInstalledOrNot("com.Ch.Example.pack");  
        if(installed)
        {
        //This intent will help you to launch if the package is already installed
        Intent LaunchIntent = getPackageManager()
            .getLaunchIntentForPackage("com.Ch.Example.pack");
    startActivity(LaunchIntent);


                  System.out.println("App already installed om your phone");


        }
        else
        {
            System.out.println("App is not installed om your phone");
        }
    }
    private boolean appInstalledOrNot(String uri)
    {
        PackageManager pm = getPackageManager();
        boolean app_installed = false;
        try
        {
               pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
               app_installed = true;
        }
        catch (PackageManager.NameNotFoundException e)
        {
               app_installed = false;
        }
        return app_installed ;
}
}
Preet_Android
  • 2,332
  • 5
  • 25
  • 43
2

Well if your plugin has a unique name(i am sure it should) you can check it.

Please read the post linked below for checking whether the plugin exists or not

Link : https://stackoverflow.com/a/6758962/1542720

Hope this helps !

Community
  • 1
  • 1
Adnan Mulla
  • 2,872
  • 3
  • 25
  • 35
1

you can change your xml file according to your requirement in onCreate()..

as i did in my code, for the different density i used different xml file.

 if (metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM) {
    setContentView(R.layout.activity_main);
 } else if (metrics.densityDpi == DisplayMetrics.DENSITY_LOW) {
    setContentView(R.layout.activity_main_small);
 } else {
    setContentView(R.layout.activity_main_large);
 }
Preet_Android
  • 2,332
  • 5
  • 25
  • 43
  • thank you preet but i already know how to do that but i would like to know how to check if a package exists and if it does change the layout.xml – Shayden117 Apr 15 '13 at 05:38