我已经为Android编写了一个应用程序,它使用webview通过复选框向MYSQl后端提交信息。现在要退出webview,用户必须使用设备上的键继续下一个应用程序活动。 有没有办法检测表单提交按钮已被应用程序按下然后使用的东西,说一个Intent去下一个活动(关闭webview然后NextActivity.class),最好没有javascript? 我已经尝试了一个shouldOverrideUrlLoading,当按下提交按钮时调用一个新页面,然后用(Uri.parse(url).getHost()调用if语句.equals(“page url”)){Intent ...没有运气。 提前致谢! 这是编码页面:
public class WebViewActivity extends Activity {
private WebView webView;
int backButtonCount = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
//webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
Bundle extras = getIntent().getExtras();
if(extras!=null){
String rode=extras.getString("rode");
}
String ray = extras.getString("rode");
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://mywebsite.com/SubDir/user_man.php?user=" + ray);
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).equals("http://mywebsite.com/SubDir/process_prof.php")) {
return false;
}
Intent ik = new Intent(WebViewActivity.this, master_menu.class);
startActivity(ik);
finish();
return true;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_bar_two, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()==R.id.action_selected){
Intent iw = new Intent(this,GetUserEmail.class);
startActivity(iw);
finish();
}
else
if (item.getItemId()==R.id.action_cancel){
Intent iw = new Intent(this,RegisterActivity.class);
startActivity(iw);
finish();
}
return true;
}
public void onBackPressed()
{
if(backButtonCount >= 1)
{
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
else
{
Toast.makeText(this, "Press the back button again to close.", Toast.LENGTH_SHORT).show();
backButtonCount++;
}
}
答案 0 :(得分:0)
我认为你的if语句的问题是getHost(),它只返回主机名(www.google.com),而不是整个URL。尝试删除它,我相信如果匹配,你的if语句将返回true。
答案 1 :(得分:0)
让它工作,这是其他人的解决方案:
public class WebViewActivity extends Activity {
public WebView webView;
int backButtonCount = 0;
final Handler myHandler = new Handler();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
Bundle extras = getIntent().getExtras();
if(extras!=null){
String rode=extras.getString("rode");
}
String ray = extras.getString("rode");
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
final JavaScriptInterface myJavaScriptInterface = new JavaScriptInterface(this);
webView.loadUrl("http://mywebsite.com/subDir/user_man.php?user=" + ray);
webView.setWebViewClient(new WebViewClient());
webView.addJavascriptInterface(myJavaScriptInterface, "ForwardFunction");
}
public class JavaScriptInterface {
Context mContext;
JavaScriptInterface(Context c) {
mContext = c; }
public void goHome(){
Intent ik = new Intent(WebViewActivity.this, GetUserEmail.class);
startActivity(ik);
finish();
Toast.makeText(WebViewActivity.this,"Your choices have been updated", Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_bar_two, menu);
return true; }
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.getItemId()==R.id.action_selected)
{ Intent iw = new Intent(this,GetUserEmail.class);
startActivity(iw);
finish(); }
else
if(item.getItemId()==R.id.action_cancel)
{
Intent iw = new Intent(this,RegisterActivity.class);
startActivity(iw);
finish();
}
return true;
}
public void onBackPressed()
{
if(backButtonCount >= 1)
{
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
else
{
Toast.makeText(this, "Press the back button again to close.", Toast.LENGTH_SHORT).show();
backButtonCount++;
}
}
}