android 我可以共享到我的NativeScript应用吗?

3htmauhk  于 2023-02-10  发布在  Android
关注(0)|答案(6)|浏览(117)

在NativeScript的当前状态下,是否可以创建一个在Android上侦听共享Intent的应用?
我想实现的是,例如,在Android上的Web浏览器中打开一个网站,点击共享,然后在共享目标列表中看到我的NativeScript应用程序。
我确实在原生Android应用上完成了这一点,但无法在NativeScript应用中使用。

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

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

q7solyqu

q7solyqu1#

我自己也一直在寻找这个问题的解决方案,发现这里所有其他的答案都很有帮助。
然而,我对 NativeScript 还是个新手(只学了两天),实际上不知道在哪里以及如何实现所有代码位一起工作。
通过使用这里的答案,我可以继续搜索并找到一个完整的GITHUB示例NickIliev/nativescript-receiving-shared-content
对于其他大一新生(或女新生)来说,如果你想找一个完整的例子,可以去repo目录下探索/demo/app/目录下的代码,这对我很有帮助,我希望对你也有帮助。

uubf1zoe

uubf1zoe2#

NativeScript应该支持这种场景。下面是我的AndroidManifest在app/App_resources/Android中的默认引导应用程序的外观:

<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>

edit:非常简单的实现,可以将Intent发送到我的任何其他应用程序:

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

iyfjxgzm3#

除了您必须在AppManifest.xml中添加的Intent过滤器之外,请确保重新构建您的应用(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;
jslywgbw

jslywgbw4#

首先在应用程序/应用程序_资源/安卓清单. xml中更新您的安卓清单. xml
添加如下Intent过滤器

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

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

eni9jsuy

eni9jsuy5#

由于这些答案在2023年都不正确,nativescript 7+的工作方式是
1.添加前面提到的Intent过滤器:

<intent-filter>
       <action android:name="android.intent.action.SEND" />
       <category android:name="android.intent.category.DEFAULT" />
       <data android:mimeType="image/*" />
     </intent-filter>

1.侦听activityNewIntentEvent。这是检索传入Intent并提取额外Intent的唯一方法:

if (isAndroid) {
      Application.android.on(AndroidApplication.activityNewIntentEvent, function (args: AndroidActivityNewIntentEventData) {
        console.log('Event : ' + args.intent);
      });
    }
eqqqjvef

eqqqjvef6#

如果有人正在寻找最新的NS7和NS8兼容版本,这对我和Android都适用。这是在NS8的抽屉模板应用程序上测试的。Javascript部分是主页. js,只需将其内容替换为下面的代码。
NS7和NS8的进口是不同的,这让人们感到困惑。

编辑您的Android清单. xml

    • 应用程序资源/Android/源代码/主文件/Android清单. 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

    • 主页/主页. 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()
    }

相关问题