【问题标题】:WebView app in Android StudioAndroid Studio 中的 WebView 应用
【发布时间】:2018-05-28 11:55:45
【问题描述】:

我已使用 Android Studio 将我的完全响应式网站转换为 Android 应用程序。我还添加了一个信号推送通知。我有两个问题。

  1. 通知工作正常,但我希望在单击通知时在单独的窗口中打开它。代码应该是什么,我应该把它们放在哪里?

  2. 我想在我的 webview 应用程序 (android studio) 中有一个页面加载进度条。代码应该是什么,我应该把它们放在哪里?

我的代码如下。

在 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 的新手。任何帮助将不胜感激。

【问题讨论】:

    标签: javascript java android


    【解决方案1】:

    试试这个代码,它将帮助你加载 webview。

    webView = (WebView) findViewById(R.id.webView);
    
            WebSettings webSettings = webView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            webView.setWebViewClient(new WebViewClient());
    
            webView.getSettings().setDomStorageEnabled(true);
            final ProgressDialog pd = ProgressDialog.show(TestView.this, "", "Loading...", true);
            webView = (WebView) findViewById(R.id.webView);
            webView.getSettings().setJavaScriptEnabled(true);
    
            webView.loadUrl("http://www.diamondflorist.com/");
            webView.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon)
                {
                    pd.show();
                }
                @Override
                public void onPageFinished(WebView view, String url) {
                    pd.dismiss();
                }
            });
    

    对于通知,您需要根据需要创建一个新的活动或对话框,然后通过单击这样的通知打开它。

    NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);
    
        Intent notificationIntent = new Intent(context, HomeActivity.class);
    
        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);
    

    希望对你有所帮助;

    【讨论】:

      【解决方案2】:

      在下面找到答案:

      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.在通知中发送您要在通知点击时打开的页面网址。

      1. 在通知意图中传递该网址,例如:

        NotificationManager notificationManager = (NotificationManager) 上下文 .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);
        
      2. 在您的 MainActivity 中从意图中获取页面 url 并将该 url 加载到 webview.ex-

        if (null != getIntent() && null != getIntent().getExtras()) { 捆绑包 = getIntent().getExtras(); 字符串 url = bundle.getString(URL); 加载Webview(url);
        }

      【讨论】:

      • 非常感谢您的回复。我是一个绝对的初学者,所以我可以再问你一些问题,关于第二个问题,你描述了关于加载 URL 的内容。但我不希望打开任何网页,我希望当我的用户单击通知时,该按摩的内容会显示在新屏幕或对话框或窗口中,而不是在我单击它时打开应用程序。
      • 是的.. 如果您想要一个新页面,您可以通过创建一个新活动来做到这一点,或者您也可以在单击通知后在同一屏幕中打开弹出窗口。
      • 请评价它,如果你得到我的答案,否则如果你需要我会发送一些代码。
      • 我正在评价你,无论如何你都值得拥有,但如果你能发送一些代码,将会有很大帮助
      【解决方案3】:

      请尝试以下方式

      public class HtmlPageLoadActivity extends Activity{
      
          private WebView webView = null;
      
          private class WebViewOverrideUrl extends WebViewClient {
              @Override
              public boolean shouldOverrideUrlLoading(WebView view, String url) {
                  proceedUrl(view, Uri.parse(url));
                  return true;
              }
      
              @TargetApi(Build.VERSION_CODES.N)
              @Override
              public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                  proceedUrl(view, request.getUrl());
                  return true;
              }
      
              private void proceedUrl(WebView view, Uri uri) {
                  try {
                      view.loadUrl(uri.toString());
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
          }
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              webView = (WebView) findViewById(R.id.webView);
              webView.setWebChromeClient(new MyWebViewClient());
              webView.setWebViewClient(new WebViewOverrideUrl());
              webView.getSettings().setJavaScriptEnabled(true);
              webView.loadUrl("your url here");
          }
      
          private class MyWebViewClient extends WebChromeClient {
              @Override
              public void onProgressChanged(WebView view, int newProgress) {
                  if (newProgress >= 100) {
      
                  }
                  setValue(newProgress);
                  super.onProgressChanged(view, newProgress);
              }
          }
      
          public void setValue(int progress) {
      
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-02-02
        • 2017-11-11
        • 2023-01-20
        • 2021-06-15
        • 2017-06-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多