![](/img/trans.png)
[英]How do I require that the user installs another Android app (on which my app depends), during installation of my app?
[英]How to make an Android app that depends on another app?
如果我创建一个依赖于其他应用程序或应用程序的应用程序(例如:Facebook和Twitter应用程序),但尚未安装它们,是否有一种方法可以检查这些依赖项并在我自己的应用程序的同时安装它们?
我在我的应用程序中这样做,需要安装zxing扫描仪应用程序。 您将在onclick或ontouch中想要这个:
try{
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.setPackage("com.google.zxing.client.android");
startActivityForResult(intent, 0);
} catch (Exception e) {
createAlert("Barcode Scanner not installed!", "This application uses " +
"the open source barcode scanner by ZXing Team, you need to install " +
"this before you can use this software!", true);
}
哪个叫
public void createAlert(String title, String message, Boolean button) {
// http://androidideasblog.blogspot.com/2010/02/how-to-add-messagebox-in-android.html
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle(title);
alertDialog.setMessage(message);
if ((button == true)) {
alertDialog.setButton("Download Now",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Intent browserIntent = new Intent(
Intent.ACTION_VIEW,
Uri.parse("market://search?q=pname:com.google.zxing.client.android"));
startActivity(browserIntent);
}
});
}
alertDialog.show();
}
然后在整理完所有代码后,我意识到你要求它与你的应用同时安装。 不确定我是否应该发布此代码,但它可能会有所帮助
简短回答:不,你不能自动安装其他应用程序作为依赖项。
更长的回答:
Android Market不允许您声明其他应用程序作为依赖项安装。 作为一个系统,Market似乎是为单个应用程序安装而设计的 - 而不是Linux发行版风格的大型依赖图。
在运行时 ,您可以测试已安装的应用程序,并将用户提交给市场,如果是这样的话。 如果你想要的话,请参阅@QuickNick建议的技术(测试是否安装了应用程序)和@TerryProbert(推向市场)。
你最好的选择可能是设计你的应用程序,如果依赖项不可用,优雅地降级,并建议(或坚持)他们前往市场安装它们。
从这开始:
Intent mediaIntent = new Intent("com.example.intent.action.NAME");
// add needed categories
List<ResolveInfo> listResolveInfo = getPackageManager().queryIntentServices(mediaIntent, 0);
if (listResolveInfo.size() != 0) {
//normal behavior
} else {
//install what you need
}
我举几个查询服务的例子。 如果要检查活动,则调用queryIntentActivities()。
我认为遵循Android开发者博客上这篇文章中概述的模式将对您有所帮助。 http://android-developers.blogspot.com/2009/01/can-i-use-this-intent.html正如TerryProbert所指出的,如果您知道Intent不可用,则会提示用户安装缺少的应用程序。
这是我用来返回存在的第一个任务活动:
try {
Class<?> missionClass = Class.forName(mPackageName+".Mission"+mission);
Method missionDescription;
missionDescription = missionClass.getMethod("missionDescription");
mMissionDescription = (String) missionDescription.invoke(null);
if (mMissionDescription.length() > 0) {
nextMission = mission;
break;
}
} catch (Exception e) {
//DEBUG*/Log.v(this.getClass().getName(), "onResume: Mission no "+mission+" not found: "+e.getMessage());
}
每个任务都在一个单独的班级中进行,该班级来自任务基础班。 派生类称为Mission1,Mission24等。
并非所有任务都已定义。
基类有一个抽象类missionDescription,它返回一个描述任务的字符串。
此代码在循环内部,因此测试任务= 1到99,尝试调用missionDescription。 返回找到第一个任务的描述时返回。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.