我可以分享到我的NativeScript应用程序吗?

时间:2016-05-29 21:19:36

标签: android nativescript

在NativeScript的当前状态下是否有可能创建一个在Android上侦听共享意图的应用程序?

我想要实现的是例如在Android上的网络浏览器中打开网站,点击共享并在共享目标列表中查看我的NativeScript应用。

我在原生Android应用上完成了此操作但无法在NativeScript应用中使用它。我已经搞乱了AndroidManifest.xml来添加

<action android:name="android.intent.action.SEND"></action>
<category android:name="android.intent.category.DEFAULT"></category>

进入intent-filter但这没有帮助。我的应用未显示在共享目标列表中。

5 个答案:

答案 0 :(得分:2)

除了必须在AppManifest.xml中添加的intent-filter之外 确保重建您的应用程序(livesync选项可能无法反映AppManifest.xml中的更改)

以下是基本共享的NativeScript实现

var app = require("application");

function onShare() {

    var sharingIntent = new android.content.Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    var shareBody = "Here is the share content body";

    sharingIntent.addFlags(android.content.Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    sharingIntent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK | android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);

    app.android.context.startActivity(sharingIntent);
}
exports.onShare = onShare;

答案 1 :(得分:1)

NativeScript应该支持这种情况。以下是我在默认引导应用程序的app/App_resources/Android中的AndroidManifest:

<activity
        android:name="com.tns.NativeScriptActivity"
        android:label="@string/title_activity_kimera"
        android:configChanges="keyboardHidden|orientation|screenSize">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter> 
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
       </intent-filter>
</activity>

编辑: 将意图发送到我的任何其他应用程序的非常简单的实现:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent sendIntent = new Intent(Intent.ACTION_SEND);
                sendIntent.setType("text/plain");
                sendIntent.putExtra("string", "the data Im sending you");

                Intent chooser = Intent.createChooser(sendIntent, "Share with ");

                if (sendIntent.resolveActivity(getPackageManager()) != null) {
                    startActivity(chooser);
                }
            }
        });

答案 2 :(得分:1)

首先在app / App_Resources / AndroidManifest.xml中更新AndroidManifest.xml

添加以下意图过滤器,如下所示

<application        android:name="com.tns.NativeScriptApplication"      android:allowBackup="true"      android:icon="@drawable/icon"       android:label="@string/app_name"
                android:theme="@style/AppTheme">        <activity           android:name="com.tns.NativeScriptActivity"
                        android:label="@string/title_activity_kimera"           android:configChanges="keyboardHidden|orientation|screenSize">

                        <intent-filter>
                <action android:name="android.intent.action.MAIN" />                                
                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>                        
                        <intent-filter>
                                <action android:name="android.intent.action.SEND" />
                                <category android:name="android.intent.category.DEFAULT" />
                                <category android:name="android.intent.category.APP_BROWSER" />
                                <data android:mimeType="text/plain" /> 
                                <data android:mimeType="image/*" />
                        </intent-filter>
                        <intent-filter>
                            <action android:name="android.intent.action.SEND_MULTIPLE" />
                            <category android:name="android.intent.category.DEFAULT" />
                            <category android:name="android.intent.category.APP_BROWSER" />
                            <data android:mimeType="image/*" />
                        </intent-filter>
                                </activity>         <activity android:name="com.tns.ErrorReportActivity"/>  </application>

然后在app.js中添加以下代码行

application.android.on(application.AndroidApplication.activityResumedEvent, function (args) {
        console.log("Event: " + args.eventName + ", Activity: " + args.activity);
        var a = args.activity;
        try {
            var Intent_1 = android.content.Intent;
            var actionSend = Intent_1.ACTION_SEND;
            var actionSendMultiple = Intent_1.ACTION_SEND_MULTIPLE;
            var argIntent = a.getIntent();
            var argIntentAction = argIntent.getAction();
            var argIntentType = argIntent.getType();
            console.log(" ~~~~ Intent is ~~~~ :" + new String(argIntent.getAction()).valueOf());
            String.prototype.startsWith = function (str) {
                return this.substring(0, str.length) === str;
            };
            if (new String(argIntentAction).valueOf() === new String(Intent_1.ACTION_SEND).valueOf()) {
                if (new String(argIntentType).valueOf() === new String("text/plain").valueOf()) {
                    console.dump(cbParseTextAndUrl(argIntent));
                }
                else if (argIntentType.startsWith("image/")) {
                    console.log(cbParseImageUrl(argIntent));
                }
            }
            else if (new String(argIntentAction).valueOf() === new String(Intent_1.ACTION_SEND_MULTIPLE).valueOf()) {
                if (argIntentType.startsWith("image/")) {
                    var Uri = cbParseMultipleImageUrl(argIntent);
                    if (Uri !== null) {
                        var Uris = JSON.parse(Uri);
                        console.log(Uris);
                    }
                }
            }
            function cbParseTextAndUrl(argIntent) {
                var Patterns = android.util.Patterns;
                //let Matcher = java.util.regex.Matcher;
                var ListUrl = [];
                var text = argIntent.getStringExtra(Intent_1.EXTRA_TEXT);
                if (new String().valueOf() !== "null") {
                    var Matcher = Patterns.WEB_URL.matcher(text);
                    while (Matcher.find()) {
                        var url = Matcher.group();
                        ListUrl.push(url);
                    }
                    return { "text": text, "listUrl": ListUrl };
                }
            }
            function cbParseImageUrl(argIntent) {
                var imageUri = argIntent.getParcelableExtra(Intent_1.EXTRA_STREAM);
                if (imageUri != null) {
                    // Update UI to reflect image being shared
                    return imageUri;
                }
            }
            function cbParseMultipleImageUrl(argIntent) {
                var imageUris = argIntent.getParcelableArrayListExtra(Intent_1.EXTRA_STREAM);
                if (imageUris != null) {
                    // Update UI to reflect image being shared
                    return JSON.stringify(imageUris.toString());
                }
            }
        }
        catch (e) {
            console.log(e);
        }
    });

现在,您可以将第三方应用中的内容分享到您的应用。

答案 3 :(得分:0)

我自己一直在寻找解决方案,发现这里所有其他答案非常有帮助。

但是,我对 NativeScript 还是一个菜鸟(仅两天),实际上无法获得在哪里以及如何实现所有代码位一起工作。

通过使用此处的答案,我可以继续搜索并找到完成的GITHUB示例NickIliev/nativescript-receiving-shared-content

对于其他希望完成示例的新生(或新生),请访问存储库并浏览/demo/app/目录中的代码。这对我很有帮助,希望对您有帮助。

答案 4 :(得分:0)

如果有人正在寻找最新的 NS7 和 NS8 兼容版本,这对我和 Android 都有效。这是在 NS8 的抽屉模板应用程序上测试的。 Javascript 部分用于 home-page.js ,只需将其内容替换为下面的代码即可。

NS7 和 NS8 的进口不同,这让人们感到困惑。

编辑您的 AndroidManifest.xml

App_Resources/Android/src/main/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="__PACKAGE__"
    android:versionCode="10000"
    android:versionName="1.0">

    <supports-screens
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="true"/>

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:name="com.tns.NativeScriptApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity
            android:name="com.tns.NativeScriptActivity"
            android:label="@string/title_activity_kimera"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize|smallestScreenSize|screenLayout|locale|uiMode"
            android:theme="@style/LaunchScreenTheme">

            <meta-data android:name="SET_THEME_ON_LAUNCH" android:resource="@style/AppTheme" />

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="image/*" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND_MULTIPLE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="image/*" />
            </intent-filter>
        </activity>
        <activity android:name="com.tns.ErrorReportActivity"/>
    </application>
</manifest>

Javascript

home/home-page.js

    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const Observable = require("@nativescript/core").Observable;
    const application = require("@nativescript/core/application");

    import { Application } from '@nativescript/core'
    import { HomeViewModel } from './home-view-model'

    var vm = new Observable();

    export function onNavigatingTo(args) {
    const page = args.object
    page.bindingContext = new HomeViewModel();
    page.bindingContext = vm;
    vm.set("sharedText", "Waiting for intent...");
    if (application.android) {
        application.android.on(application.AndroidApplication.activityCreatedEvent, function (args) {
            var activity = args.activity;
            console.log(activity);
            vm.set("sharedText", "Intend data received");
            // Get intent, action and MIME type
            var intent = activity.getIntent();
            var action = intent.getAction();
            var type = intent.getType();
            if (android.content.Intent.ACTION_SEND === action && type != null) {
                if (type.startsWith("text/")) {
                    handleSendText(intent); // Handle text being sent
                }
                else if (type.startsWith("image/")) {
                    handleSendImage(intent); // Handle single image being sent
                }
            }
            else if (android.content.Intent.ACTION_SEND_MULTIPLE === action && type != null) {
                if (type.startsWith("image/")) {
                    handleSendMultipleImages(intent); // Handle multiple images being sent
                }
            }
            else {
                // Handle other intents, such as being started from the home screen
            }
        });
    }
    }

    function handleSendText(intent) {
    if (application.android) {
        var sharedText = intent.getStringExtra(android.content.Intent.EXTRA_TEXT);
        if (sharedText != null) {
            // Update UI to reflect text being shared
            console.log("sharedText: ", sharedText);
            console.log("Text received!");
            // set timeout - enough to update UI after app loading
            setTimeout(func, 1000);
            function func() {
                vm.set("sharedText", sharedText);
            }
        }
    }
    }
    function handleSendImage(intent) {
    if (application.android) {
        var imageUri = intent.getParcelableExtra(android.content.Intent.EXTRA_STREAM);
        if (imageUri != null) {
            // Update UI to reflect image being shared
            console.log("Image received!");
            var appContext = application.android.context;
            var bitmap = android.provider.MediaStore.Images.Media.getBitmap(appContext.getContentResolver(), imageUri);
            console.log("bitmap: ", bitmap);
            vm.set("bitmap", bitmap);
        }
    }
    }
    function handleSendMultipleImages(intent) {
    if (application.android) {
        var imageUris = intent.getParcelableArrayListExtra(android.content.Intent.EXTRA_STREAM);
        if (imageUris != null) {
            // Update UI to reflect multiple images being shared
            console.log("imageUris: ", imageUris);
            console.log("Multiple images received!");
        }
    }
    }

    export function onDrawerButtonTap(args) {
    const sideDrawer = Application.getRootView()
    sideDrawer.showDrawer()
    }