ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android屏幕旋转问题

2019-08-27 03:25:56  阅读:155  来源: 互联网

标签:android rotation webview


我的应用程序有问题.它正确地通过时间表系统中的所有形式并显示时间表,然而当我旋转屏幕以查看横向或纵向视图时,它会回到第一个屏幕,我无法弄清楚为什么…提前感谢任何帮助!

    package com.timetable;

import java.io.FileOutputStream;
import java.io.IOException;



  import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;

    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Picture;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.Window;
    import android.webkit.WebChromeClient;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;

    public class Timetable extends Activity {
        WebView mWebView;
        //int count = 0;
        String html = "<html><body>";
        String html2 = "<html><body>";
        Document docs;
        Document docs2;
        public String text(String htmlrec)
        {
            try {
                 docs2 = Jsoup.connect(htmlrec).get();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Elements data = docs2.getElementsByClass("timetables");
            html2 += data.toString()+ "</body></html>";
            return html2;
        }

        public void main(String... args) {

            try {
                 docs = Jsoup.connect("http://www.dcu.ie/timetables/search.shtml").get();
            } catch (IOException e) {
                e.printStackTrace();
            }

            Elements table = docs.getElementsByClass("timetables");
            html += table.toString() + "</body></html>";
        }

        public void onCreate(Bundle savedInstanceState) {
            main(); 

            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
            mWebView = (WebView) findViewById(R.id.webview);
            mWebView.setWebViewClient(new TimeClient());
            mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            mWebView.setClickable(true);
            mWebView.requestFocus();
            mWebView.setInitialScale(1);
            //mWebView.getSettings().setBlockNetworkLoads(true);
            mWebView.getSettings().setSupportZoom(true);
            mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.getSettings().setDomStorageEnabled(true);
            mWebView.loadDataWithBaseURL("http://www.dcu.ie/timetables/",html, "text/html", "utf-8","http://www.dcu.ie/timetables/");

            final Activity MyActivity = this;
            mWebView.setWebChromeClient(new WebChromeClient() {
             public void onProgressChanged(WebView view, int progress)  
             {
              //Make the bar disappear after URL is loaded, and changes string to Loading...
              MyActivity.setTitle("Loading...");
              MyActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded

              // Return the app name after finish loading
                 if(progress == 100)
                    MyActivity.setTitle(R.string.app_name);
               }
             });
        }

        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
                mWebView.goBack();            
                return true;
            }
            return super.onKeyDown(keyCode, event);
        }
        private class TimeClient extends WebViewClient {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (view.canGoBack() == false)
                //if (count == 0)
                {
                    String newUrl = text(url);
                    view.loadDataWithBaseURL("http://www.dcu.ie/timetables/",newUrl, "text/html", "utf-8","http://www.dcu.ie/timetables/");
                   // count+

+;
            }
            else
            {

                view.loadUrl(url);
                Picture picture = view.capturePicture();
                Bitmap  b = Bitmap.createBitmap( picture.getWidth(),picture.getHeight(), Bitmap.Config.ARGB_8888);
                Canvas c = new Canvas( b );
                picture.draw( c );
                FileOutputStream fos = null;
                try 
                {
                    fos = new FileOutputStream( "/sd/dcu" +
                    System.currentTimeMillis() + ".jpg" );
                    if ( fos != null )
                    {
                        b.compress(Bitmap.CompressFormat.JPEG, 90, fos );
                        fos.close();
                    }
                } 
                catch( Exception e )
                {
                } 
            }

            return true;
        }
    }


}

解决方法:

发生的事情是,当您更改方向时,活动将被销毁并重新创建.为避免破坏和重新创建活动,请执行以下操作:

android:configChanges="orientation|keyboardHidden|keyboard"

作为AndroidManifest.xml中活动的属性.

标签:android,rotation,webview
来源: https://codeday.me/bug/20190827/1736711.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有