I don't understand how it is possible to do this, since there are no WebActivities with PhoneGap, only your Activity classes and your index.html page. I have a MainActivity that looks like this...
public class MastersProjectActivity extends DroidGap
{
@JavascriptInterface
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Set by <content src="index.html" /> in config.xml
//super.loadUrl(Config.getStartUrl());
super.loadUrl("file:///android_asset/www/index.html");
Context myCntxt = getApplicationContext();
TelephonyManager tMgr = (TelephonyManager)myCntxt.getSystemService(Context.TELEPHONY_SERVICE);
String curPhoneNumber = tMgr.getLine1Number();
Log.d("PhoneNumber", curPhoneNumber);
}
}
...and I want to be able to use curPhoneNumber in the index.html page, which in PhoneGap contains the entirety of the UI. Any ideas on how I can do this? I'm completely new to Android development in general, and certainly with PhoneGap. To be honest I still don't really understand how the index.html page is rendered as an Android UI. Do I need to wrap the index.html page in a WebActivity or something? I'm not sure if this would create any issues in the way PhoneGap creates the .apk.
EDIT - I tried to implement but it didn't work for me, all I got was an alert literally saying "interface.getPhoneNumber()"
. I may have implemented the 'MyInterface'
class incorrectly? Here's the class I made....
public class MyInterface extends DroidGap {
private MastersProjectActivity _activity;
private CordovaWebView _view;
public MyInterface(MastersProjectActivity activity, CordovaWebView view) {
this._activity = activity;
this._view = view;
}
@JavascriptInterface
public String getPhoneNumber() {
Context myCtxt = getApplicationContext();
return ((TelephonyManager)myCtxt.getSystemService(Context.TELEPHONY_SERVICE)).getLine1Number();
}
}
...and I exposed the javascript interface in my main activity like so...
@Override
public void onCreate(Bundle savedInstanceState)
{
.
.
.
super.appView.addJavascriptInterface(new MyInterface(this, appView), "interface");
}
...and then I call alert("interface.getPhoneNumber()")
in the index.html
file, but all I get is that string alerted, it doesn't actually call getPhoneNumber()
.