如何检查给定应用程序是否安装在内部或外部存储上

时间:2013-06-28 14:46:17

标签: android

我试图通过传递包名来获取应用程序的位置,以检查它是否安装在内部或外部存储上。请帮忙。

3 个答案:

答案 0 :(得分:0)

check this out 


 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.packagename");  
                if(installed)
                {
                //This intent will help you to launch if the package is already installed
                Intent LaunchIntent = getPackageManager()
                    .getLaunchIntentForPackage("com.packagename");
            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 ;
        }
        }

答案 1 :(得分:0)

您可以查看此代码,看看我们是否已在内部或外部存储上安装

(注意:你可以在oncreate或任何地方使用它)

ApplicationInfo io = getApplicationContext().getApplicationInfo();

if(io.sourceDir.startsWith("/data/")) {
Toast.makeText(this,"your app is in internal storage",Toast.LENGTH_LONG).show();

//application is installed in internal memory
} else if(io.sourceDir.startsWith("/mnt/") || io.sourceDir.startsWith("/sdcard/")) {
Toast.makeText(this,"your app is in external storage",Toast.LENGTH_LONG).show();
//application is installed in sdcard(external memory)
}

答案 2 :(得分:0)

PackageManager pm = context.getPackageManager();
ApplicationInfo applicationInfo = pm.getApplicationInfo(packageName, 0);
if ((applicationInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
    // installed on sdcard
}