CSDN简单安卓client实现

一直是在电脑上面看CSDN的博客、资讯,今天做了个小软件在手机上面查看资讯和博客。

                    

直接上代码:

一、首先是欢迎界面 --  LoadingActivity.java

package com.example.webviewtest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;

public class LoadingActivity extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.loading);
		
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				
				Intent mainIntent = new Intent(LoadingActivity.this,
						ShowActivity.class);
				
				startActivity(mainIntent);
				finish();
			}
		}, 2000);
	}
	
}
其布局文件非常easy--loading.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:src="@drawable/splash" />

</LinearLayout>

二、主页面--ShowActivity.java
package com.example.webviewtest;

import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class ShowActivity extends Activity {

	private WebView webView;
	private static Boolean isExit = false;
	Timer tExit = new Timer();// 计时器
	TimerTask task;

	@SuppressLint("SetJavaScriptEnabled")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);

		webView = (WebView) findViewById(R.id.web_view);

		webView.getSettings().setBuiltInZoomControls(true);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setRenderPriority(RenderPriority.HIGH);
		// webView.getSettings().setBlockNetworkImage(true);

		webView.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// 依据传入的參数再去载入新的网页
				view.loadUrl(url);
				// 表示当前WebView能够处理打开新网页的请求,不用借助系统浏览器
				return true;
			}

			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				super.onReceivedError(view, errorCode, description, failingUrl);

				Toast.makeText(ShowActivity.this, "呃!载入失败,或许断网了。或者不能訪问!",
						Toast.LENGTH_LONG).show();
			}
		});

		webView.loadUrl("http://www.csdn.net/");
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
			webView.goBack();// 返回前一个页面
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onBackPressed() {
		if (isExit == false) {
			isExit = true;
			Toast.makeText(this, "再按一次返回键回到桌面", Toast.LENGTH_SHORT).show();
			task = new TimerTask() {
				@Override
				public void run() {
					isExit = false;
				}
			};
			tExit.schedule(task, 2000);
		} else {
			finish();
			System.exit(0);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub

		super.onCreateOptionsMenu(menu);
		// 加入四个菜单项

		menu.add(Menu.NONE, Menu.FIRST + 1, 1, "帮助").setIcon(
				R.drawable.menu_help);

		menu.add(Menu.NONE, Menu.FIRST + 2, 2, "退出").setIcon(
				R.drawable.menu_quit);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {

		case Menu.FIRST + 1:
			Intent intent = new Intent(ShowActivity.this, HelpActivity.class);
			startActivity(intent);
			return true;

		case Menu.FIRST + 2:
			new AlertDialog.Builder(this)
					.setTitle("退出")
					.setMessage("你确定要退出CSDN吗?")
					.setIcon(android.R.drawable.ic_dialog_info)
					.setPositiveButton(R.string.ok,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									finish();// 退出程序
								}
							}).setNegativeButton(R.string.cancel, null).show();
			return true;
		}
		return false;
	}

}
布局文件--activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

三、帮助页面--HelpActivity.java

package com.example.webviewtest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class HelpActivity extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.help);
	}

	
}
布局文件--help.xml

<?xml version="1.0" encoding="utf-8"?

> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:stretchColumns="0,1,2" >     <TableRow>         <TextView             android:layout_width="100dp"             android:layout_height="match_parent"             android:layout_marginRight="10dp"             android:text="第一步:"             android:textSize="23sp" />         <TextView             android:layout_width="100dp"             android:layout_height="match_parent"             android:layout_marginRight="5dp"             android:layout_marginLeft="5dp"             android:text="第二步:"             android:textSize="23sp" />         <TextView             android:layout_width="100dp"             android:layout_height="match_parent"             android:layout_marginRight="10dp"             android:text="第三步:"             android:textSize="23sp" />     </TableRow>     <TableRow         android:layout_marginTop="20dp">        <TextView             android:layout_width="100dp"             android:layout_height="match_parent"             android:layout_marginRight="10dp"             android:text="右上角图标"             android:textSize="15sp" />         <TextView             android:layout_width="100dp"             android:layout_height="match_parent"             android:layout_marginRight="5dp"             android:layout_marginLeft="5dp"             android:text="点击博客"             android:textSize="15sp" />         <TextView             android:layout_width="100dp"             android:layout_height="match_parent"             android:layout_marginRight="10dp"             android:text="点击登录"             android:textSize="15sp" />     </TableRow>     <TableRow>         <ImageView             android:layout_width="100dp"             android:layout_height="match_parent"             android:layout_marginRight="10dp"             android:src="@drawable/first" />         <ImageView             android:layout_width="100dp"             android:layout_height="match_parent"             android:layout_marginLeft="5dp"             android:layout_marginRight="5dp"             android:src="@drawable/second" />         <ImageView             android:layout_width="100dp"             android:layout_height="match_parent"             android:layout_marginLeft="10dp"             android:src="@drawable/third" />     </TableRow> </TableLayout>

四、最后别忘了在AndroidManifest.xml文件里加入訪问网络的权限

 <uses-permission android:name="android.permission.INTERNET" />


五、Apk下载地址:http://download.csdn.net/detail/xdwyyan/8121301



posted @ 2017-04-16 16:18  jzdwajue  阅读(190)  评论(0编辑  收藏  举报