Call a native android app from ionic app

2020-03-30 03:02发布

问题:

I’m developing an app that will be called by an android native app. I also have to call them. For doing this I found this plugin.

They will call my app (and expect me to call theirs) following this code:

String packageName = “com.android.app”;
Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
if (intent == null) {
   // The app is not installed
   intent = new Intent(Intent.ACTION_VIEW);
   intent.setData(Uri.parse(“market://details?id=” + packageName));
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(“param”, “aaaaa”);
startActivity(intent);

I don’t get if the plugin will work to open my app when they do this (and their app will open also).

I look for a way of using intends in ionic but I found nothing.

Thanks!

回答1:

Maybe it will help someone how I resolved it.

To open another android app I use the com.lampa.startapp. The code for doing it it's:

var sApp = startApp.set({
    "package": "packageName" //The packageName of the app I want to open
}, {
    //extras I want to add
    "myParams" : "aaaaa"
});
sApp.start();

To work with intents (the ones that open my app) I use the cordova-plugin-intent with the following code:

//To get the intent and extras when the app it's open for the first time
window.plugins.intent.getCordovaIntent (function (intent) {
    intenthandler(intent);
});

//To get the intent and extras if the app is running in the background
window.plugins.intent.setNewIntentHandler (function (intent) {
    intenthandler(intent);
});

//To handle the params
var intenthandler = function (intent) {
      if (intent && intent.extras && intent.extras.myParams) {
        //Do something
      }
};

That's all!