我有一个库项目,其中包含我的应用程序的代码库。我有一个演示和一个完整版本,其中包括包含所有活动等的这个库。大多数玩家从演示开始,如果他们喜欢它,他们会获取完整版本。但是,他们通常不会立即卸载演示,而是继续将它们同时安装在他们的设备上。这似乎会导致一个弹出窗口,询问哪个应用程序(演示或完整)通过活动打开每个活动作为播放器电影。
有什么方法可以防止这种情况发生,还是设备上同时拥有两个 APK 的不可避免的副作用?
您需要在演示版和完整版之间定义不同的包。即使您的清单定义了不同的包(这是必需的,因为您不能在 Google Play 上有两个应用程序具有相同的包),我假设您调用的活动也存在于您的库项目中,这将使用两个版本的应用程序的库包。
要解决此问题,您还应该在子项目的库中声明清单中的每个活动。
以下是我如何通过检测两个游戏的存在来“解决”问题,然后提示卸载演示版,如果有人对未来感兴趣的话:
public class UpdateManager {
public static void checkForBothVersions(final Activity activity) {
PackageManager packageManager = activity.getPackageManager();
//# We want to intentionally cause an Exception. This will let us know
//# whether there is only one version installed.
try {
packageManager.getPackageInfo("package.full", 0);
packageManager.getPackageInfo("package.demo", 0);
//# If we get here, then both are installed. Let's display our warning.
Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("Warning!");
builder.setMessage("" +
"We've detected that you have both the FULL and DEMO versions of Application installed.\n" +
"This can cause undesired results. We suggest uninstalling the DEMO version of Application." +
"")
.setCancelable(false)
.setPositiveButton("Uninstall DEMO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Uri uri = Uri.parse("package:package.demo");
//# Start the delete intent for the demo
activity.startActivity( new Intent(Intent.ACTION_DELETE, uri) );
//# We don't wanna call finish here. We want the delete Intent to open
//# and once the user closes that Intent, it should go back to the
//# calling RB Activity and call onResume where the check cycle will
//# restart.
}
})
.setNegativeButton("Continue", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
activity.startActivity(new Intent("package.lib.SplashActivity"));
activity.finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}catch( Exception e ) {
Log.i("UpdateManager", "Only one version of Application installed.");
activity.startActivity(new Intent("package.lib.SplashActivity"));
activity.finish();
}
}
}
我只是从演示和完整应用程序的 onResume 方法中调用此方法。