我正在尝试通过传递包名称来检查其是否安装在内部或外部存储上来获取应用程序的位置。请帮忙。
问问题
1446 次
3 回答
1
你可以检查这个代码,看看我们是否安装在内部或外部存储上
(注意:你可以在 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)
}
于 2016-09-30T12:58:42.187 回答
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 ;
}
}
于 2013-06-28T14:53:50.130 回答
0
PackageManager pm = context.getPackageManager();
ApplicationInfo applicationInfo = pm.getApplicationInfo(packageName, 0);
if ((applicationInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
// installed on sdcard
}
于 2018-09-12T08:18:15.083 回答