recieve share send intent on android

291 views
Skip to first unread message

CKB

unread,
May 3, 2015, 9:01:11 AM5/3/15
to codenameone...@googlegroups.com
IDE: Eclipse
Desktop OS Windows 7
Simulator
Device Android

General question on best way to handle a recieve send share intent on android using Codename1.  Normally on the share when I complete the task, I would set the response code to OK and then finish the activity.  If I use the main android activity for Codename1, it will stop the application so here are my questions:

1.  If you use the main android activity for Codename1, how do you tell the calling application that you are done?
2.  If I create another activity using the android.xapplication build property, how can I launch the main Codename1application from my activity once my activity has completed?

Thanks

Shai Almog

unread,
May 4, 2015, 2:48:58 AM5/4/15
to codenameone...@googlegroups.com, clint.b...@gmail.com
Generally you can use AndroidNativeUtil for most things in the native code portion. We covered some of its features here: http://codenameone.com/blog/processing-responsiveness-native-more.html
Display.execute accepts intent:// URL's and provides an action listener that would be invoked on a response from an intent. Chen might be able to elaborate here...

Chen Fishbein

unread,
May 4, 2015, 8:49:59 AM5/4/15
to codenameone...@googlegroups.com, clint.b...@gmail.com
Hi,
From android native you should use 
AndroidNativeUtil.startActivityForResult(...) to invoke another intent.

clint.b...@gmail.com

unread,
May 4, 2015, 10:14:41 AM5/4/15
to codenameone...@googlegroups.com, clint.b...@gmail.com
Okay so lets say I do the following:

1. set the android.xintent_filter to  <intent-filter android:label="Test Send" ><action android:name="android.intent.action.SEND" /><category android:name="android.intent.category.DEFAULT" /><data android:mimeType="image/*" /><data android:mimeType="application/pdf" /></intent-filter>

2.  Go into Adobe PDF and click "Share" on the share menu. I select "Test Send", which sends the PDF to my Codename1 application.

3. Display.getInstance().getProperty("AppArg",null) returns null for this type of share.  I use the code below to get the URI for the share and then upload it to our servers.

4. Once I have completed the upload, how do I signal that I am done with the intent?  Currently Adobe has not received a message from our application that says we are done.  If you go into Adobe, it brings up our application.  If we set the result and call finish on the com.codename1.impl.android.AndroidNativeUtil.getActivity(), we get a message that says the app has crashed.

Native code to get the URI for the Send Share Intent --

    public String share() {
        android.app.Activity ctx = com.codename1.impl.android.AndroidNativeUtil.getActivity();
        Intent intent = ctx.getIntent();
        String action = intent.getAction();
        String type = intent.getType();
        if (Intent.ACTION_SEND.equals(action) && type != null) {
            if (type.startsWith("image/") || type.startsWith("application/pdf")) {
                try{
                Uri uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
                if (uri != null) {
                    String filePath = uri.getPath();
                    File sharedFile = null;
                    String[] projection = { MediaStore.MediaColumns.DATA };
                    Cursor pathCursor =
                            ctx.getContentResolver().query(uri, projection,null, null, null);
                    // Check for a valid cursor
                    if (pathCursor != null &&
                            pathCursor.moveToFirst()) {
                        // Get the column index in the Cursor
                        int filenameIndex = pathCursor.getColumnIndex(
                                MediaStore.MediaColumns.DATA);
                        // Get the full file name including path
                        String  fileName = pathCursor.getString(filenameIndex);
                        pathCursor.close();
                        // Create a File object for the filename
                        sharedFile = new File(fileName);
                    }else{
                        sharedFile = new File(uri.getPath());
                    }
                    return sharedFile.getPath();
                }
               
            }catch (Throwable e){
                return e.getMessage();
            }
            }
        }
        return null;
    } 

Chen Fishbein

unread,
May 4, 2015, 3:34:44 PM5/4/15
to codenameone...@googlegroups.com, clint.b...@gmail.com
Hi,
Can you please share the stack you get when you call finish on the activity?


clint.b...@gmail.com

unread,
May 4, 2015, 4:04:40 PM5/4/15
to codenameone...@googlegroups.com, clint.b...@gmail.com
<11> EDT@830021635432, prio=5, in group 'main', status: 'RUNNING'
      at android.os.Bundle.setAllowFds(Bundle.java:193)
      at android.content.Intent.setAllowFds(Intent.java:3832)
      at android.app.Activity.finish(Activity.java:3702)
      at com.mobile.app.share.ShareNativeImpl.complete(ShareNativeImpl.java:38)
      at com.mobile.app.share.ShareNativeStub.complete(ShareNativeStub.java:17)
      at com.mobile.app.share.MobileApplication.checkForShare(Unknown Source:-1)
      at com.mobile.app.share.MobileApplication.start(Unknown Source:-1)
      at com.mobile.app.share.MobileApplicationStub.run(MobileApplicationStub.java:124)
      at com.codename1.ui.Display.processSerialCalls(Display.java:1139)
      at com.codename1.ui.Display.edtLoopImpl(Display.java:1083)
      at com.codename1.ui.Display.mainEDTLoop(Display.java:989)
      at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
      at com.codename1.impl.CodenameOneThread$1.run(CodenameOneThread.java:60)
      at java.lang.Thread.run(Thread.java:856)

Chen Fishbein

unread,
May 5, 2015, 2:51:10 AM5/5/15
to codenameone...@googlegroups.com, clint.b...@gmail.com
Hi,
What's on the top of the trace? what is the exception?


clint.b...@gmail.com

unread,
May 5, 2015, 8:15:53 AM5/5/15
to codenameone...@googlegroups.com, clint.b...@gmail.com
There was not an exception, that was the full stack trace that I was able to get

Chen Fishbein

unread,
May 5, 2015, 1:16:43 PM5/5/15
to codenameone...@googlegroups.com, clint.b...@gmail.com
Hi,
The trace probably got swallowed somewhere in the android noisy log.
Can you try again and extract the full log? or maybe file an issue with a test case, so I could reproduce this?


clint.b...@gmail.com

unread,
May 5, 2015, 4:26:02 PM5/5/15
to codenameone...@googlegroups.com, clint.b...@gmail.com
Chen,

Thanks for the help on this, I was able to get my Native Activity to work.  If you would like additional information on this, let me know and I can add it to the thread.

Thanks


Chen Fishbein

unread,
May 6, 2015, 2:04:01 AM5/6/15
to codenameone...@googlegroups.com, clint.b...@gmail.com
Yes, please share your findings.
Thanks

clint.b...@gmail.com

unread,
May 6, 2015, 9:36:53 AM5/6/15
to codenameone...@googlegroups.com, clint.b...@gmail.com

1. Created a build hint for android.xapplication for my native activity
<activity  android:name="com.codename.spike.project.ShareActivity" android:label="ShareActivity"><intent-filter android:label="Test Send" ><action android:name="android.intent.action.SEND" /><category android:name="android.intent.category.DEFAULT" /><data android:mimeType="image/*" /><data android:mimeType="application/pdf" /></intent-filter></activity>
2. Added the Activity below to the native folder for android
3. Created an intent for the CodeName1 Stub class in my custom Activity
4. Set the data attribute on the Intent to the location of the file.
5. Once the main CodeName1 Activity has launched, a call to Display.getInstance().getProperty("AppArg", null); will return the path from my native source
6. Set the result and call finish on my custom Activity to let the calling application know that we are done.


package com.codename.spike.project;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.provider.MediaStore;
import android.database.Cursor;

import java.io.File;

import userclasses.StateMachine;

import com.codename.spike.project.CodeNameOneSpikeProjectStub;


import android.content.Context;


public class ShareActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();
        String path = null;


        if (Intent.ACTION_SEND.equals(action) && type != null) {
        
            if (type.startsWith("image/") || type.startsWith("application/pdf")) {
                try{
                    Uri uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
                    if (uri != null) {
                        String filePath = uri.getPath();
                        File sharedFile = null;
                        String[] projection = { MediaStore.MediaColumns.DATA };
                        Cursor pathCursor = getContentResolver().query(uri, projection,null, null, null);
                        // Check for a valid cursor
                        if (pathCursor != null &&
                                pathCursor.moveToFirst()) {
                            // Get the column index in the Cursor
                            int filenameIndex = pathCursor.getColumnIndex(
                                    MediaStore.MediaColumns.DATA);
                            // Get the full file name including path
                            String  fileName = pathCursor.getString(filenameIndex);
                            pathCursor.close();
                            // Create a File object for the filename
                            sharedFile = new File(fileName);
                        }else{
                            sharedFile = new File(uri.getPath());
                        }
                        path = sharedFile.getPath();
                    }
                    Intent startMain = new Intent(this, CodeNameOneSpikeProjectStub.class);
                    startMain.setData(Uri.parse(path));
                    startMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP  | Intent.FLAG_ACTIVITY_NEW_TASK);
                    this.startActivity(startMain);
                this.setResult(Activity.RESULT_OK);
                intent.setData(null);
                this.finish();


                }catch (Throwable e){
                    Log.e("ShareActivity", e.getMessage());
                }
            }
        
           
        }

    }

}


Reply all
Reply to author
Forward
0 new messages