Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.xaluan2;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.SharedPreferences;
- import android.content.DialogInterface.OnClickListener;
- import android.content.Intent.ShortcutIconResource;
- import android.content.Intent;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.net.Uri;
- import android.os.Bundle;
- import android.preference.PreferenceManager;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.Window;
- import android.webkit.WebChromeClient;
- import android.webkit.WebSettings;
- import android.webkit.WebView;
- import android.webkit.WebViewClient;
- import android.widget.ProgressBar;
- public class XaLuanActivity extends Activity{
- private static final String tag=XaLuanActivity.class.getCanonicalName();
- private static final int CHIASE_MENU=1;
- private static final int THOAT_MENU=2;
- private static final int TAILAI_MENU=3;
- private static final int TROGIUP_MENU=4;
- private static final int DANHGIA_MENU=5;
- private String mainUrl="http://www.xaluan.com/mobi.php";
- ProgressBar mProgressBar;
- WebView wbvContent;
- @SuppressWarnings("deprecation")
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.xaluan);
- wbvContent=(WebView) findViewById(R.id.wbvContent);
- mProgressBar=(ProgressBar)findViewById(R.id.progressBar);
- wbvContent.getSettings().setJavaScriptEnabled(true);
- //wbvContent.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
- //wbvContent.getSettings().setSupportMultipleWindows(true);
- wbvContent.getSettings().setPluginsEnabled(true);
- wbvContent.getSettings().setPluginState(WebSettings.PluginState.ON);
- wbvContent.setInitialScale(1);
- wbvContent.getSettings().setLoadWithOverviewMode(true);
- wbvContent.getSettings().setUseWideViewPort(true);
- wbvContent.getSettings().setSupportZoom(true);
- wbvContent.getSettings().setBuiltInZoomControls(true);
- //wbvContent.getSettings().setDefaultZoom(ZoomDensity.FAR);
- HelloWebViewClient wbvClient=new HelloWebViewClient();
- wbvContent.setWebViewClient(wbvClient);
- if(!isConnectedNetwork(this)) {
- new AlertDialog.Builder(this)
- .setIcon(android.R.drawable.ic_dialog_alert)
- .setTitle("Thông báo")
- .setMessage("Không có kết nối mạng, vui lòng kết nối mạng để sử dụng")
- .setPositiveButton("Thử lại", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- XaLuanActivity.this.finish();
- startActivity(new Intent(XaLuanActivity.this, XaLuanActivity.class));
- }
- })
- .setNegativeButton("Thoát", new OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- XaLuanActivity.this.finish();
- }
- })
- .show();
- }else {
- wbvContent.loadUrl(mainUrl);
- wbvContent.setWebChromeClient(new WebChromeClient() {
- public void onProgressChanged(WebView view, int progress) {
- if(progress < 100 && mProgressBar.getVisibility() == ProgressBar.GONE){
- mProgressBar.setVisibility(ProgressBar.VISIBLE);
- }
- mProgressBar.setProgress(progress);
- if(progress == 100) {
- mProgressBar.setVisibility(ProgressBar.GONE);
- }
- }
- });
- }
- }
- private static class HelloWebViewClient extends WebViewClient {
- @Override
- public boolean shouldOverrideUrlLoading(WebView view, String url) {
- if (url != null && url.startsWith("market://")) {
- view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
- //return true;
- }
- else if(url.endsWith(".apk"))
- {
- view.getContext().startActivity(new Intent(Intent.ACTION_VIEW ,Uri.parse(url)));
- //need help here to download the apk and form install affter downloaded
- }
- else {
- view.loadUrl(url);
- //return false;
- }
- return true;
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- menu.add(0, CHIASE_MENU, 0, "Chia Sẻ").setIcon(R.drawable.share);
- menu.add(0, TAILAI_MENU, 0, "Tải Lại").setIcon(R.drawable.refresh);
- menu.add(0, DANHGIA_MENU, 0, "Đánh Giá").setIcon(R.drawable.rate);
- menu.add(0, TROGIUP_MENU, 0, "Trợ Giúp").setIcon(R.drawable.about);
- menu.add(0, THOAT_MENU, 0, "Thoát").setIcon(R.drawable.exit);
- return super.onCreateOptionsMenu(menu);
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case CHIASE_MENU: // chia se
- Intent sharingIntent = new Intent(Intent.ACTION_SEND);
- sharingIntent.setType("text/plain");
- sharingIntent.putExtra(Intent.EXTRA_SUBJECT, wbvContent.getTitle());
- sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, wbvContent.getUrl());
- startActivity(Intent.createChooser(sharingIntent,"Chia sẻ"));
- break;
- case THOAT_MENU: // chia se
- System.exit(0);
- break;
- case TAILAI_MENU: // chia se
- wbvContent.reload();
- break;
- case TROGIUP_MENU: // chia se
- Intent intent1=new Intent(this, About.class);
- startActivity(intent1);
- break;
- case DANHGIA_MENU:
- String url="market://details?id="+getApplicationContext().getPackageName();
- Intent intent2 = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
- startActivity(intent2);
- break;
- default:
- break;
- }
- return super.onOptionsItemSelected(item);
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_BACK) {
- if(wbvContent.canGoBack()) {
- wbvContent.goBack();
- return false;
- }else {
- new AlertDialog.Builder(this)
- .setIcon(android.R.drawable.ic_dialog_alert)
- .setTitle("Thoát")
- .setMessage("Bạn có muốn thoát khỏi ứng dụng không")
- .setPositiveButton("Có", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- XaLuanActivity.this.finish();
- }
- })
- .setNegativeButton("Không", null)
- .show();
- }
- }
- return super.onKeyDown(keyCode, event);
- }
- public static boolean isConnectedNetwork(Context context) {
- ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
- return activeNetworkInfo != null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement