【问题标题】:Get extras in Cordova app在 Cordova 应用程序中获取额外内容
【发布时间】:2016-08-30 04:42:55
【问题描述】:

我们有两个 Android 应用程序:一个使用原生 Java 实现,另一个使用 Ionic 编写。 Ionic 应用程序启动我的应用程序,它是使用lampaa plugin 的 Android 应用程序。我可以使用以下代码接收 Ionic 应用程序提供的附加功能:

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

在我退出我的应用之前,我想向 Ionic 应用发送额外内容。这很容易从 Android 端完成。 Ionic 应用程序如何知道我的应用程序已将控制权转移给它,以及它如何检索我发送的附加信息?

【问题讨论】:

    标签: android cordova extras


    【解决方案1】:

    我认为在你的情况下,你需要使用其他插件,如 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

    希望对你有帮助!!

    【讨论】:

      【解决方案2】:

      添加,如果是网络意图,您可以使用以下插件,这有助于获取额外信息和 URL 信息。

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

      【讨论】:

        【解决方案3】:

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

        首先你需要写一个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){ }
        }
        

        插件.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);
              }
          }
        

        【讨论】:

        • 你能解释更多吗?
        猜你喜欢
        • 1970-01-01
        • 2011-01-02
        • 1970-01-01
        • 2016-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-15
        • 1970-01-01
        相关资源
        最近更新 更多