6

I am new to Android and Java. I have constructed an app using HTML/Javascript that is working great.

I now need to create an activity that launches the email client, fills in subject and body, and (the tough part) adds a file attachment. I have not been able to do this from within JavaScript, mailto: will not attach the file.

So I need to accomplish this through Java and execute it from JavaScript. I think this can be done by using addJavaScriptInterface but I cannot find any detailed documentation or examples to go off of.

How could I do this?

Here is what I have so far after reading the documentation:

2nd update to code:

MainActivity.java

public class MainActivity extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setIntegerProperty( "splashscreen", R.drawable.splash );
    super.loadUrl("file:///android_asset/www/index.html", 1000);
    WebView mWebView;
    mWebView = (WebView)findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.addJavascriptInterface(new JavaScriptInterface(), "Android"); 
}
}

JavaScriptInterface.java

public class JavaScriptInterface {

public void doEmail(){
    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.setType("text/html");
    sendIntent.putExtra(android.content.Intent.EXTRA_TEXT,"test text");
    sendIntent.putExtra(Intent.EXTRA_SUBJECT,"test subject");
    sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    sendIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("file://test co.html"));
    startActivity(Intent.createChooser(sendIntent, "Send email..."));
    } 
}

Then I would reference the intent through JavaScript by using Android.doEmail().

With the above code I am getting 2 errors in Eclipse 1. The method startActivity(Intent) is undefined for the type - JavaScriptInterface 2. webview cannot be resolved or is not a field - MainActivity

What am I doing wrong?

user899641
  • 341
  • 1
  • 4
  • 20

4 Answers4

6

This documentation tells you exactly how to do it.

It looks like there are three main steps:

  1. Create your 'interface' class in Android
  2. Add an instance of this 'interface' to the WebView you are using.
  3. Call the interface from your JavaScript.
nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
  • Ok, I edited my question and included what I have so far. Can you tell me what I'm doing wrong? – user899641 Aug 29 '11 at 20:06
  • Your `JavaScriptInterface` class should not extend `Activity`. It will be its own class that has a `send()` method. Your `Activity` class should do the second part of your codes `send()` method (the `WebView` portion) – nicholas.hauschild Aug 29 '11 at 20:32
  • @nicholas.hauschild i'am follow same documentation but toast message no fire why. – NagarjunaReddy Apr 19 '13 at 06:51
4
public class MainActivity extends DroidGap {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setIntegerProperty( "splashscreen", R.drawable.splash );

        JavaScriptInterface jsi = new JavaScriptInterface(this, appView);
        appView.addJavascriptInterface(jsi, "Android");

        super.loadUrl("file:///android_asset/www/index.html", 1000);
    }
}

and

public class JavaScriptInterface {
    private WebView mAppView;
    private DroidGap mGap

    public JavaScriptInterface (DroidGap gap, WebView view)
    {
        mAppView = view;
        mGap = gap;
    }

    public void doEmail(){
        Intent sendIntent = new Intent(Intent.ACTION_SEND);
        sendIntent.setType("text/html");
        sendIntent.putExtra(android.content.Intent.EXTRA_TEXT,"test text");
        sendIntent.putExtra(Intent.EXTRA_SUBJECT,"test subject");
        sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        sendIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("file://test co.html"));
        startActivity(Intent.createChooser(sendIntent, "Send email..."));
    } 
}
adamcodes
  • 1,606
  • 13
  • 21
  • I already have a `MainActivity`. Would I include the other stuff here? `public class MainActivity extends DroidGap { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setIntegerProperty( "splashscreen", R.drawable.splash ); super.loadUrl("file:///android_asset/www/index.html", 1000); } }` – user899641 Aug 29 '11 at 20:34
  • http://stackoverflow.com/questions/2727763/communication-between-android-java-and-phonegap-javascript – adamcodes Aug 29 '11 at 20:42
  • I updated my question with my current code. Please let me know what you think now. – user899641 Aug 29 '11 at 20:53
  • Awesome! This really helps. I am still having one issue though. "The method startActivity(Intent) is undefined for the type - JavaScriptInterface" – user899641 Aug 29 '11 at 21:27
  • That is because startActivity is a method under the Activity class. Try mGap.startActivity. If not, there's probably a workaround. – adamcodes Aug 29 '11 at 21:37
  • ok, `mGap.startActivity` worked. The app now works as expected using Gmail when prompted but when I select Email this is what I get: "The application Email (process com.google.android.email) has stopped unexpectedly. Force close." I need it to use corporate email so this is no good. Any ideas? – user899641 Aug 29 '11 at 22:22
2

Using addJavaScriptInterface will extend the DOM inside the embedded browser, and allow JS to access a Java object, which is exactly what you want.

There are too many steps to outline here, that have already been documented. This link has a good overview.

Steve
  • 53,375
  • 33
  • 96
  • 141
0

I used WebIntents from Boris Smus (http://smus.com/android-phonegap-plugins) and it works like a charm. You can also peruse his code a little to understand better the approach he took with plugins.

NOTE: you do need to update the code provided as is a little (see comments) and the plugin architecture has changed a little.

bryanallott
  • 244
  • 1
  • 7