Get extras in Cordova app

时间:2016-08-30 04:42:55

标签: android cordova extras

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?

3 个答案:

答案 0 :(得分:4)

我认为在你的情况下,你需要使用其他插件,如cordova-plugin-intent

例如:

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

如需更多帮助,请查看here

希望这会对你有所帮助!!

答案 1 :(得分:0)

要添加,如果是网页意图,您可以使用以下插件来帮助获取额外内容和网址信息。

它还有其他方法,如startActivity和sendBroadcast。

答案 2 :(得分:0)

我还需要做类似的事情。我一开始就挣扎但现在我找到了解决方案,很高兴分享这些信息,这对其他人有用

首先你需要写一个cordova插件,这个插件应该有     BroadcastReceiver实现如下所示

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 将以下节点添加到plugin.xml文件

<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 在平台就绪中添加以下代码

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