javascript - Android Studio 中的 WebView 应用
问题描述
我已经使用 Android Studio 将我的完全响应式网站转换为 Android 应用程序。我还添加了一个信号推送通知。我有两个问题。
通知工作正常,但我希望在单击通知时在单独的窗口中打开它。代码应该是什么,我应该把它们放在哪里?
我想在我的 webview 应用程序(android 工作室)中有一个页面加载进度条。代码应该是什么,我应该把它们放在哪里?
我的代码如下。
在 mainactivity.java 页面
package com.example.rijvan.awebaddress;
import...
public class MainActivity extends AppCompatActivity {
public WebView mywebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OneSignal.startInit(this)
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.unsubscribeWhenNotificationsAreDisabled(true)
.init();
setContentView(R.layout.activity_main);
mywebView=(WebView)findViewById(R.id.webview);
WebSettings webSettings=mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebView.loadUrl("https://my web site/");
mywebView.setWebViewClient(new WebViewClient());
}
@Override
public void onBackPressed() {
if (mywebView.canGoBack()){
mywebView.goBack();
}else{
super.onBackPressed();
}
}
}
在activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:layout_editor_absoluteY="81dp">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
</android.support.constraint.ConstraintLayout>
在 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rijvan.awebaddress">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我真的是 Android 的新手。任何帮助将不胜感激。
解决方案
在下面找到答案:
1. 我想在我的 webview 应用程序(android studio)中有一个页面加载进度条。代码应该是什么,我应该把它们放在哪里? 试试这些代码会起作用 -
package com.example.rijvan.awebaddress;
import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
private WebView webView;
private ProgressBar progressBar;
@SuppressLint("HandlerLeak")
private Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
switch (message.what) {
case 1: {
webViewGoBack();
}
break;
}
}
};
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
progressBar = findViewById(R.id.progress_bar);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// TODO: 5/28/2018 set your loader progress here
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
progressBar.setVisibility(View.VISIBLE);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
});
webView.loadUrl("https://my web site/");
progressBar.setVisibility(View.VISIBLE);
webView.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
handler.sendEmptyMessage(1);
return true;
}
return false;
}
});
}
private void webViewGoBack() {
webView.goBack();
}
}
2. 通知工作正常,但我希望在单击通知时在单独的窗口中打开它。代码应该是什么,我应该把它们放在哪里?
您可以通过在一个信号通知中传递 url 来做到这一点 - 1. 在您想要在通知单击时打开的通知中发送页面 url。
在通知意图中传递该 url,例如:
NotificationManager notificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); 通知通知 = new Notification(icon, message, when);
Intent notificationIntent = new Intent(context, MainActivity.class); notifcationIntent.putString(URL, pageUrl); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0); notification.setLatestEventInfo(context, title, message, intent); notification.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(0, notification);
在您的 MainActivity 中从意图中获取页面 url 并将该 url 加载到 webview.ex-
if (null != getIntent() && null != getIntent().getExtras()) { Bundle bundle = getIntent().getExtras(); 字符串 url = bundle.getString(URL); 加载Webview(网址);
}
推荐阅读
- powershell - Powershell:逻辑运算符 - 或无法按预期工作
- python - Python复杂选择查询TypeError:字符串格式化期间并非所有参数都转换
- android - 如何将字符串从共享首选项转换为 MutableList
对在kotlin中绘制折线? - regex - 匹配范围(1 到 99)整数的正则表达式和小数的 xx.xx
- python - Python:如何在其他类别之间分配“杂项”总数
- angular - 如何将 DropList 数据绑定到 Observable
- .net - 使用 tf.exe 通过命令行签入解决方案
- terraform - 使用工作区时的全局资源
- shell - Golang exec命令chmod返回错误
- data-science - 使用存在-不存在机会相机陷阱数据的栖息地适宜性