Android Open Source - CityAndroid Main Activity






From Project

Back to project page CityAndroid.

License

The source code is released under:

Apache License

If you think the Android project CityAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.soar.city;
/*from w w w .  ja va  2  s .co  m*/
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity{
      
  private WebView webView;  
  private ValueCallback<Uri> mUploadMessage;
    //private ProgressBar circleProgressBar;  
    private LinearLayout loadeyLayout;
    private LinearLayout error_tip_layout;
    //private TextView netWorkTipTxtView;
    private Button reload_btn;
  private final static int FILECHOOSER_RESULTCODE = 1;
  
  @Override  
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webView = (WebView) findViewById(R.id.merchant_staff);  
    loadeyLayout = (LinearLayout)findViewById(R.id.fullscreen_loading_indicator);  
    error_tip_layout = (LinearLayout)findViewById(R.id.error_tip_layout); 
    reload_btn = (Button) findViewById(R.id.reload_btn);
    
        webView.getSettings().setJavaScriptEnabled(true);//???????JS??  
        webView.getSettings().setBuiltInZoomControls(false);
        webView.setWebChromeClient(new MyWebClient());
        webView.loadUrl("http://www.yuxian.me/");  
        reload_btn.setOnClickListener(new OnClickListener()  
        {         
            public void onClick(View v)  
            {   
              error_tip_layout.setVisibility(View.GONE);
              webView.loadUrl("http://www.yuxian.me/");  
            }  
        });
        
        webView.setWebViewClient(new WebViewClient(){
          
          @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // TODO Auto-generated method stub
            loadeyLayout.setVisibility(View.VISIBLE);  
            webView.setVisibility(View.VISIBLE);
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
              loadeyLayout.setVisibility(View.GONE);  
                super.onPageFinished(view, url);

                
            }
            @Override  
            public boolean shouldOverrideUrlLoading(WebView view, String url) {  
                // TODO Auto-generated method stub  
                view.loadUrl(url);// ?????WebView??????  
                return true;//true???????????????????????  
            }  
            @Override   //????????????  
            public void onReceivedError(WebView view, int errorCode,  
                    String description, String failingUrl) {  
                // TODO Auto-generated method stub
              error_tip_layout.setVisibility(View.VISIBLE);
              webView.setVisibility(View.GONE);
                Toast.makeText(MainActivity.this, R.string.network_error, Toast.LENGTH_SHORT).show();  
            }  
        });  
    
  }
    
  @Override   //??????????Activity????????????????WebView?????  
    public boolean onKeyDown(int keyCode, KeyEvent event) {  
        // TODO Auto-generated method stub  
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {  
            webView.goBack();  
            return true;  
        }  
        return super.onKeyDown(keyCode, event);  
    }  
    
  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == FILECHOOSER_RESULTCODE) {
            if (null == mUploadMessage)
            return;
        Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
        mUploadMessage.onReceiveValue(result);
        mUploadMessage = null;
        }
    }

  
  
  public class MyWebClient extends WebChromeClient {
        // For Android 3.0-
        public void openFileChooser(ValueCallback<Uri> uploadMsg) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
        }

        // For Android 3.0+
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("*/*");
            startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE);
        }

        // For Android 4.1
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
        }
    }

}




Java Source Code List

com.soar.city.MainActivity.java
com.soar.city.utils.MyWebChromeClient.java