我正在尝试将设备ID显示在javascript中,但是我收到了上述错误。我正在使用android studio。我能够获得getState值没有问题。我是机器人的菜鸟。任何帮助,将不胜感激。这是我的代码
MainActivity.java
import android.os.Bundle;
import org.apache.cordova.*;
import org.apache.cordova.device.Device;
import android.provider.Settings.Secure;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.content.Context;
import android.webkit.JavascriptInterface;
public class MainActivity extends CordovaActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
WebView webview = new WebView(this);
setContentView(webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebChromeClient(new WebChromeClient());
webview.addJavascriptInterface(new HelperClass(),"device");
webview.loadUrl("file:///android_asset/www/index.html");
webview.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String loc) {
String value = "some val";
String template = "javascript:runme('%s')";
String thisout = String.format(template, value);
view.loadUrl(thisout);
}
});
}
}
HelperClass.java
import android.content.Context;
import android.provider.Settings;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.webkit.JavascriptInterface;
import java.util.UUID;
import org.apache.cordova.CordovaActivity;
public class HelperClass extends CordovaActivity{
public int state = 4;
// All the methods that you want to expose to Javascript will go here.
@JavascriptInterface
public int getState(){
return this.state;
}
@JavascriptInterface
public String getDuid() {
TelephonyManager TM = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// IMEI No.
String imeiNo = TM.getDeviceId();
// IMSI No.
String imsiNo = TM.getSubscriberId();
// SIM Serial No.
String simSerialNo = TM.getSimSerialNumber();
Log.d("Device ID",imeiNo);
return imeiNo;
}
}//end of class
的index.html
<div class="app">
<input type="text" id="myText">
<input type="text" id="myVal">
<button onclick="runme('test');">Run me</button>
</div>
<script type="text/javascript">
function runme(txt){
document.getElementById("myText").value = "output is "+txt;
alert('i ran');
document.getElementById("myVal").value = window.device.getDuid();
}
document.addEventListener("deviceready", function(){
});
</script>