ロード時のアクティビティから Java スクリプト関数を直接呼び出すことはできません。ハンドラーとスレッド、および非表示のボタンを使用する必要があります
public class AskUsActivity extends Activity{
private Button scriptButton;
private WebView webView;
private Handler handler;;
MyJavaScriptInterface myJavaScriptInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ask_us);
webView = (WebView)findViewById(R.id.askUs_webView);
scriptButton=(Button)findViewById(R.id.scriptButton);
loadData();
myJavaScriptInterface = new MyJavaScriptInterface(this);
webView.loadUrl("file:///android_asset/AskUs.htm");
webView.addJavascriptInterface(myJavaScriptInterface, "AndroidFunction");
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
handler=new Handler(){
@Override
public void handleMessage(Message msg) {
System.out.println("inside handler");
scriptButton.performClick();
}
};
scriptButton.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
webView.loadUrl("javascript:setcontactInfo('1|*|nikhil|||');");
}
});
// String selectedImagePath="1|*|nikhil|||";
// webView.loadUrl("javascript:setInternational()");
// new Thread(){
// @Override
// public void run() {
// System.out.println("inside thread.................Start.......");
// webView.loadUrl("javascript:setcontactInfo('1|*|nikhil|||');");
// System.out.println("inside thread.................end.......");
// }
// }.start();
}
@Override
protected void onResume() {
super.onResume();
try {
Thread.sleep(400);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(){
public void run() {
System.out.println("inside thread................");
handler.sendEmptyMessage(0);
};
}.start();
}
private void loadData(){
try {
String destPath = "/data/data/" + getPackageName()+ "/databases/kellypipe.sqlite";
File f = new File(destPath);
if(!f.exists()){
Log.v("<<< TAG >>>","File Not Exist");
InputStream in = getAssets().open("kellypipe.sqlite");
OutputStream out = new FileOutputStream(destPath);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
Log.v("<<< TAG >>>","ioexeption");
e.printStackTrace();
}
DBManager dbManager = new DBManager(this);
Log.v("<<< TAG >>>","Database is there with version: "+dbManager.getReadableDatabase().getVersion());
// String sql = "select * from CasingMetric";
String sql= "SELECT ID,listname,name,address,contact,email from contactInfo order by ID";
SQLiteDatabase db = dbManager.getReadableDatabase();
Cursor cursor = db.rawQuery(sql, null);
Log.v("<<< TAG >>>","Query Result:"+cursor);
cursor.moveToFirst();
int i=cursor.getColumnIndex("listname");
int j=cursor.getColumnIndex("name");
int x=0;
while(!cursor.isAfterLast()){
x++;
System.out.println("Listname : "+cursor.getString(i)+ " Name: "+cursor.getString(j)+" :: X is : "+x);
cursor.moveToNext();
}
cursor.close();
db.close();
dbManager.close();
}
public class MyJavaScriptInterface {
Context mContext;
MyJavaScriptInterface(Context c) {
mContext = c;
}
public void showToast(String toast){
//Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
webView.loadUrl("javascript:callFromActivity()");
}
public void openAndroidDialog(){
AlertDialog.Builder myDialog = new AlertDialog.Builder(AskUsActivity.this);
myDialog.setTitle("DANGER!");
myDialog.setMessage("You can do what you want!");
myDialog.setPositiveButton("ON", null);
myDialog.show();
}
}
}