Get extras in Cordova app

老子叫甜甜 提交于 2020-01-01 16:52:12

问题


We have two Android applications: one implemented using native Java and another written using Ionic. The Ionic app launches my app which is the Android app using the lampaa plugin. I could receive the extras that the Ionic app provides using the following code:

String keyid = getIntent().getStringExtra("keyid");

Before I exit my app, I would like to send extras to the Ionic app. This is easily done from the Android side. How does the Ionic app know that my application has transferred control to it and how can it retrieve the extras that I have sent?


回答1:


I think in your case to get extras from your native app you need to use other plugin like cordova-plugin-intent.

For example :

    //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
          }
    };

For more help check to here.

Hopes this will help you!!




回答2:


To add on, In case of web intents, you can use the following plugins thats helps in getting extras and URL info.

It also has other methods like startActivity and sendBroadcast too.

  • cordova-web intent plugin
  • webintent plugin



回答3:


I also needed to do similar stuff. I struggled in the beginning but now I have found the solution, happy to share this information, this could be useful for others

First you need to write a cordova plugin, this plugin should have BroadcastReceiver implementation as shown below

public class IntentReceiver extends BroadcastReceiver {
public static final String EXTRA_NAME = "message";
@Override
    public void onReceive(Context ctx, Intent intent) {
try{
Intent mainIntent = new Intent(ctx, Class.forName(ctx.getPackageName() + ".MainActivity"));
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String extra = intent.getStringExtra(EXTRA_NAME);
mainIntent.putExtra("message", extra);
ctx.startActivity(mainIntent);
}catch(Exception ex){ }
}

Plugin.xml Add below nodes to plugin.xml file

<config-file target="AndroidManifest.xml" parent="/manifest/application">
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>
        </config-file>
        <config-file parent="/manifest/application" target="AndroidManifest.xml">
            <receiver android:name="hcw.fi.phoniro.receiver.IntentReceiver" android:exported="true">
                <intent-filter android:priority="999">
                    <action android:name="android.intent.action.SEND" />
                </intent-filter>
            </receiver>
        </config-file>

htmlpage.ts Add below code in platform ready

platform.ready().then(() => {

   window.plugins.intent.setNewIntentHandler(this.HandleNewIntent);
   window.plugins.intent.getCordovaIntent(this.HandleNewIntent, function ()    {
    //alert("Error: Cannot handle open with file intent");
  });

});
HandleNewIntent(intent){

      if(intent && intent.extras){  
intent.extras.myParams) {
        // Do something with the File
        document.getElementById("intentdata").innerHTML = "Data from Android app : " +intent.extras.message;
      }else{
        // this will happen in getCordovaIntent when the app starts and there's no
        // active intent
        console.log("The app was opened manually and there's not file to open");
        //alert('The app was opened manually and there is not file to open' + intent);
      }
  }


来源:https://stackoverflow.com/questions/39218893/get-extras-in-cordova-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!