SHARE
TWEET

Untitled

a guest Apr 21st, 2017 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class MainActivity extends Activity {
  2.  
  3. private WebView mywebview;
  4. private SwipeRefreshLayout swipeLayout;
  5. private ValueCallback<Uri> mUploadMessage;
  6. private final static int FILECHOOSER_RESULTCODE=1;
  7.  
  8. @Override
  9. protected void onActivityResult(int requestCode, int resultCode,
  10.                                 Intent intent) {
  11.     if(requestCode==FILECHOOSER_RESULTCODE)
  12.     {
  13.         if (null == mUploadMessage) return;
  14.         Uri result = intent == null || resultCode != RESULT_OK ? null
  15.                 : intent.getData();
  16.         mUploadMessage.onReceiveValue(result);
  17.         mUploadMessage = null;
  18.     }
  19. }
  20.  
  21.  
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24.     super.onCreate(savedInstanceState);
  25.     setContentView(R.layout.activity_main);
  26.  
  27.     //webview
  28.     mywebview = (WebView) findViewById(R.id.webView);
  29.     WebSettings webSettings = mywebview.getSettings();
  30.     webSettings.setJavaScriptEnabled(true);
  31.     mywebview.loadUrl("https://m.samods.ru/");
  32.     mywebview.setWebChromeClient(new WebChromeClient()
  33.     {
  34.         //The undocumented magic method override
  35.         //Eclipse will swear at you if you try to put @Override here
  36.         // For Android 3.0+
  37.         public void openFileChooser(ValueCallback<Uri> uploadMsg) {
  38.  
  39.             mUploadMessage = uploadMsg;
  40.             Intent i = new Intent(Intent.ACTION_GET_CONTENT);
  41.             i.addCategory(Intent.CATEGORY_OPENABLE);
  42.             i.setType("image/*");
  43.             MainActivity.this.startActivityForResult(Intent.createChooser(i,"File Chooser"), FILECHOOSER_RESULTCODE);
  44.  
  45.         }
  46.  
  47.         // For Android 3.0+
  48.         public void openFileChooser( ValueCallback uploadMsg, String acceptType ) {
  49.             mUploadMessage = uploadMsg;
  50.             Intent i = new Intent(Intent.ACTION_GET_CONTENT);
  51.             i.addCategory(Intent.CATEGORY_OPENABLE);
  52.             i.setType("*/*");
  53.             MainActivity.this.startActivityForResult(
  54.                     Intent.createChooser(i, "File Browser"),
  55.                     FILECHOOSER_RESULTCODE);
  56.         }
  57.  
  58.         //For Android 4.1
  59.         public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
  60.             mUploadMessage = uploadMsg;
  61.             Intent i = new Intent(Intent.ACTION_GET_CONTENT);
  62.             i.addCategory(Intent.CATEGORY_OPENABLE);
  63.             i.setType("image/*");
  64.             MainActivity.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), MainActivity.FILECHOOSER_RESULTCODE );
  65.  
  66.         }
  67.  
  68.     });
  69.     //setContentView(mywebview);
  70.  
  71.  
  72.  
  73.     //swipe refresh
  74.     swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
  75.     swipeLayout.setColorSchemeResources(R.color.colorPrimary);
  76.     swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
  77.         @Override
  78.         public void onRefresh() {
  79.             mywebview.loadUrl("javascript: loc = location.href; location.href=loc");
  80.             //mywebview.loadUrl("javascript: window.location.reload();");
  81.         }
  82.     });
  83.  
  84.     mywebview.setWebViewClient(new WebViewClient() {
  85.         @Override
  86.         public void onPageFinished(WebView web, String url) {
  87.             if (swipeLayout.isRefreshing()) {
  88.                 swipeLayout.setRefreshing(false);
  89.             }
  90.         }
  91.     });
  92.  
  93. }
  94.  
  95.  
  96.  
  97. //back button
  98. @Override
  99. public void onBackPressed() {
  100.     if(mywebview.canGoBack()) {
  101.         //mywebview.goBack();
  102.         mywebview.loadUrl("javascript: popstate();");
  103.  
  104.     } else {
  105.         super.onBackPressed();
  106.     }
  107. }
RAW Paste Data
Challenge yourself this year...
Learn something new in 2017
Top