Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
551 views
in Technique[技术] by (71.8m points)

cordova - Sending url to ionic android app via webintents from another app

Looking for an updated solution, running the latest ionic 1.1.0 release which uses Cordova 5.x. Trying to be able to browse a website in chrome and send that url to my ionic android app using web intent. My app compiles and runs, however when i attempt to use the share to feature from chrome(or any other app) and choose my app to share to, my app crashes.

I first attempted to use the plugin:

ionic plugin add https://github.com/Initsogar/cordova-webintent

and then removed the plugin and i also tried a more recently updated fork:

ionic plugin add https://github.com/fluentstream/cordova-webintent

In my app.js file I putting the following code:

.run(function($ionicPlatform, $rootScope, $ionicHistory, $state) { 
  $ionicPlatform.ready(function() {
    window.plugins.webintent.getExtra(window.plugins.webintent.EXTRA_TEXT,
    function(url) {
      incomingURL = url;
      //alert(incomingURL);
      console.log(incomingURL);
    }, function() {
      incomingURL = false;
      //alert("no url");
      console.log("no url");
    });
  });
})

I also tried:

.run(function($ionicPlatform, $rootScope, $ionicHistory, $state) { 
  $ionicPlatform.ready(function() {
    window.plugins.webintent.getUri(function(url) {
      if(url !== "") {
          alert(url);//url is the url the intent was launched with
      }
    }); 
  });
})

In the file config.xml I would put:

<plugin name="WebIntent" value="com.borismus.webintent.WebIntent"/>

In the AndroidManifest.xml I would manually put in:

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

The app runs, but when I go to chrome and click the share button, and then choose my app, the app shuts down the following android message appears:

Unfortunately, MyAppName has stopped.

Can anybody suggest a solution to getting the share to intent to work with my app...or am i forgetting something and doing something wrong.

Thank you!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This question is a bit aged. But i had a more or less similar challenge. I had no luck with the WebIntent plugin and therefore i developed my own plugin for dealing with Intents on Android.

You may check this: https://github.com/napolitano/cordova-plugin-intent

While i'm still working on this plugin for my own projects, it's yet almost usable and documentation should be good enough to get a foot into the door.

Some background:

To allow third party apps to send content to your app, you must add an intent-filter to your MainActivity in the AndroidManifest.xml.

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

While you can do this manually, this has some disadvantages - so you probably want to have a hook or something similar. If you go to the repository above, you'll find and example for this.

Additionally to the intent-filter, you currently need an plugin that allows you a) to access the cordova intent (that's important to get access to sent content on application startup) and to receive notifications if the onNewIntent event was triggered, which happens, if content is sent while your application is running.

That's exaclty what my plugin does. It gives you access to the cordova intent and allows you to handle the onNewIntent event.

Examples:

window.plugins.intent.getCordovaIntent(function (Intent) {
    console.log(Intent);
}, function () {
    console.log('Error');
});

window.plugins.intent.setNewIntentHandler(function (Intent) {
    console.log(Intent);
});

You will receive a limited intent object as result on success. This can be processed by your app.

Example:

{
    "action": "android.intent.action.SEND_MULTIPLE",
    "clipItems": [
        {
            "uri": "file:///storage/emulated/0/Download/example-document.pdf"
        },
        {
            "uri": "file:///storage/emulated/0/Download/example-archive.zip"
        }
    ],
    "flags": 390070273,
    "type": "*/*",
    "component": "ComponentInfo{com.example.droid/com.example.droid.MainActivity}",
    "extras": "Bundle[mParcelledData.dataSize=596]"
}

While this example actually shows JSON, you will receive a ready-to-use object.

Please note that i develop the plugin currently only for my own needs. However it may work also for you. Currently no angular examples were added to the README, but i think that should not be a big problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...