我对android开发完全不熟悉我基本上是一名网络开发人员,我做了一个关于将wordpress网站转换为android webview app的Udemy课程。我通过从文件中复制代码来关注并制作应用程序。现在,当我打开应用程序时,我看到一个白色的屏幕,有时会觉得应用程序无法运行,但应用程序正在加载网页。现在我想添加一个加载图像或加载器的任何东西。 这是我的mainActivity.java代码
public class MainActivity extends ActionBarActivity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://bikanershop.com/");
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
// Stop local links and redirects from opening in browser instead of
//WebView
mWebView.setWebViewClient(new MyAppWebViewClient());
Parse.initialize(this, "oI7lWjyQc0EhpDsn1cyfaoCtpUbKQp1rFbX6PPZN", "FIeGPOxqKe3jBuvXKjW4Ml69K12tjDRq6sLruqUQ");
ParseInstallation.getCurrentInstallation().saveInBackground();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
另一个文件是myAppWebViewClient.java
public class MyAppWebViewClient extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(Uri.parse(url).getHost().endsWith("bikanershop.com"))
{
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
和xml文件如下AndroidMenifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bikanershop.bikanershop" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission
android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission
android:name="com.google.android.c2dm.permission.RECEIVE" />
<!--
IMPORTANT: Change
"com.parse.tutorials.pushnotifications.permission.C2D_MESSAGE" in the
lines below
to match your app's package name + ".permission.C2D_MESSAGE".
-->
<permission android:protectionLevel="signature"
android:name="com.bikanershop.bikanershop.permission.C2D_MESSAGE" />
<uses-permission
android:name="com.bikanershop.bikanershop.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action
android:name="com.google.android.c2dm.intent.REGISTRATION" />
<!--
IMPORTANT: Change "com.parse.tutorials.pushnotifications" to
match your app's package name.
-->
<category android:name="com.bikanershop.bikanershop" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.ParsePushBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.INTERNET" />
我使用的是android studio最新版本1.1.0
答案 0 :(得分:0)
按照Here所述使用ProgressDialog
。
对于您的情况,由于您需要关闭对话框,因此您应该在myAppWebViewClient.java
内嵌入您的功能。
所以你的新代码将是这样的:
public class MainActivity extends ActionBarActivity {
private ProgressDialog dialog = new ProgressDialog(WebActivity.this);
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(Uri.parse(url).getHost().endsWith("bikanershop.com"))
{
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
});
dialog.setMessage("Loading..Please wait.");
dialog.setCanceledOnTouchOutside(false);
dialog.show();
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://bikanershop.com/");
// Force links and redirects to open in the WebView instead of in a browser
//mWebView.setWebViewClient(new WebViewClient()); //replaced with code above
// Stop local links and redirects from opening in browser instead of
//WebView
//mWebView.setWebViewClient(new MyAppWebViewClient());
Parse.initialize(this, "oI7lWjyQc0EhpDsn1cyfaoCtpUbKQp1rFbX6PPZN", "FIeGPOxqKe3jBuvXKjW4Ml69K12tjDRq6sLruqUQ");
ParseInstallation.getCurrentInstallation().saveInBackground();
}