這是自定義applaunch插件。它的工作原理,當應用程序被安裝它directlly推出如果不是打開Play商店中
你index.js
上按一下按鈕來啓動應用程序代碼
AppChecker是服務。 appcheker是行動。 com.outfit7.mytalkingtomfree
是您想要啓動的應用程序的包名稱。
$(".appopenclass").on("click",function() {
cordova.exec(function(winParam) {},
function(error) {
utils.setAlertMsg("Plugin failed " + error);
},
"AppChecker",
"appchecker",
["com.outfit7.mytalkingtomfree"]);
});
項目 - >水庫 - > XML-> config.xml中
<feature name="AppChecker">
<param name="android-package" value="com.appview.openapp.AppChecker" />
</feature>
創建一個包,如姓名com.appview.openapp
和像AppChecker.java
創建新的類名AppChecker.Java
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.util.Log;
public class AppChecker extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args,
CallbackContext callbackContext) throws JSONException {
if (action.equals("appchecker")) {
String message = args.getString(0);
this.appchecker(message, callbackContext);
return true;
}
return false;
}
private void appchecker(final String message, CallbackContext callbackContext) {
if (message != null && message.length() > 0) {
// callbackContext.success(message);
Log.i("test", "" + message);
// appInstalledOrNot(message);
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
boolean installed = appInstalledOrNot(message);
if (installed) {
// This intent will help you to launch if the package is already
// installed
Intent LaunchIntent = cordova.getActivity().getPackageManager()
.getLaunchIntentForPackage(message);
cordova.getActivity().startActivity(LaunchIntent);
System.out.println("App already installed on your phone");
} else {
System.out.println("App is not installed on your phone");
try {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + message));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
cordova.getActivity().startActivity(i);
} catch (android.content.ActivityNotFoundException anfe) {
}
}
}
});
} else {
callbackContext.error("Expected one non-empty string argument.");
}
}
private boolean appInstalledOrNot(String uri) {
PackageManager pm = cordova.getActivity().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;
}
}
您是使用phonegap build還是CLI? – QuickFix
我正在使用phonegap構建 – Sdog