liangoogle

liangoogle
随笔 - 9, 文章 - 0, 评论 - 3, 引用 - 0
数据加载中……

2011年4月21日

android音乐播放器常见操作

/*变量声明*/  
private ImageButton playBtn = null;//播放、暂停  
private ImageButton latestBtn = null;//上一首  
private ImageButton nextButton = null;//下一首  
private ImageButton forwardBtn = null;//快进  
private ImageButton rewindBtn = null;//快退  
private TextView playtime = null;//已播放时间  
private TextView durationTime = null;//歌曲时间  
private SeekBar seekbar = null;//歌曲进度  
private Handler handler = null;//用于进度条  
private Handler fHandler = null;//用于快进  
private int currentPosition;//当前播放位置  
/*获得列表传过来的数据*/  
@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.play);  
    Intent intent = this.getIntent();  
    Bundle bundle = intent.getExtras();  
    _ids = bundle.getIntArray("_ids");    //获得保存音乐文件_ID的数组  
    position = bundle.getInt("position"); //获得应该播放的音乐的号数,既播放第几首  
        //代码未完,见下面的代码  
}  
/*初始化控件*/  
playtime = (TextView)findViewById(R.id.playtime);         //显示已经播放的时间  
durationTime = (TextView)findViewById(R.id.duration);     //显示歌曲总时间  
playBtn = (ImageButton)findViewById(R.id.playBtn);       //开始播放、暂停播放按钮  
latestBtn = (ImageButton)findViewById(R.id.latestBtn);   //播放上一首按钮  
nextButton = (ImageButton)findViewById(R.id.nextBtn);    //播放下一首按钮  
forwardBtn = (ImageButton)findViewById(R.id.forwardBtn); //快进按钮  
rewindBtn = (ImageButton)findViewById(R.id.rewindBtn);   //快退按钮  
seekbar = (SeekBar)findViewById(R.id.seekbar);           //播放进度条  
/*定义各控件的回调函数*/ 
playBtn.setOnClickListener(new View.OnClickListener() { //点击“播放、暂停”按钮时回调  
    @Override 
    public void onClick(View v) {                 
        if (mp.isPlaying()){                     //如果正在播放则暂停  
            pause();  
            playBtn.setBackgroundResource(  
                 R.drawable.play_selecor);   //更改按键状态图标  
        } else{                                  //如果没有播放则恢复播放  
            play();  
            playBtn.setBackgroundResource(  
                R.drawable.pause_selecor);   //更改按键状态图标  
        }  
    }  
});  
latestBtn.setOnClickListener(new View.OnClickListener() {//点击“播放上一首”按钮时回调            
    @Override 
    public void onClick(View v) {  
        int num = _ids.length;                  //获得音乐的数目  
        if(position==0){                        //如果已经时第一首则播放最后一首  
            position=num-1;                                       
        }else{                                  //否则播放上一首  
            position-=1;  
        }  
        int pos = _ids[position];              //得到将要播放的音乐的_ID  
        setup();                               //做播放前的准备工作  
        play();                    //开始播放  
    }  
});  
nextButton.setOnClickListener(new View.OnClickListener(){//点击“播放下一首”按钮时回调            
    @Override 
    public void onClick(View v) {                  
        int num = _ids.length;                 //获得音乐的数目  
        if (position==num-1){                  //如果已经是最后一首,则播放第一首  
            position=0;   
        }else{  
            position+=1;                  //否则播放下一首  
        }  
        int pos = _ids[position];             //得到将要播放的音乐的_ID  
        setup();                              //做播放前的准备工作  
        play();                               //开始播放  
    }  
});  
forwardBtn.setOnTouchListener(new OnTouchListener() {    //点击“快进”按钮时回调  
    @Override 
    public boolean onTouch(View v, MotionEvent event) {  
        switch (event.getAction()) {  
            case MotionEvent.ACTION_DOWN:  
                fHandler.post(forward); //此处使用handler对象更新进度条  
                mp.pause();     //点击快进按钮时,音乐暂停播放                              
                break;  
            case MotionEvent.ACTION_UP:  
                fHandler.removeCallbacks(forward);            
                mp.start();     //松开快进按钮时,音乐暂恢复播放                             
                playBtn.setBackgroundResource(  
                    R.drawable.pause_selecor);  
                break;  
        }  
        return false;  
    }  
});  
rewindBtn.setOnTouchListener(new OnTouchListener() {    //点击“快退”按钮时回调         
    @Override 
    public boolean onTouch(View v, MotionEvent event) {  
        switch (event.getAction()) {  
            case MotionEvent.ACTION_DOWN:     
                fHandler.post(rewind);            
                mp.pause(); //点击快退按钮时,音乐暂暂停播放  
                break;  
            case MotionEvent.ACTION_UP:  
                fHandler.removeCallbacks(rewind);  
                mp.start(); //松开快退按钮时,音乐暂恢复播放  
                playBtn.setBackgroundResource(  
                    R.drawable.pause_selecor);  
                break;  
        }  
        return false;  
    }  
});  
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {            
    @Override 
    public void onStopTrackingTouch(SeekBar seekBar) {  
        mp.start();     //停止拖动进度条时,音乐开始播放  
    }  
    @Override 
    public void onStartTrackingTouch(SeekBar seekBar) {  
        mp.pause();     //开始拖动进度条时,音乐暂停播放  
    }  
    @Override 
    public void onProgressChanged(SeekBar seekBar, int progress,  
        boolean fromUser) {  
        if(fromUser){  
            mp.seekTo(progress);    //当进度条的值改变时,音乐播放器从新的位置开始播放  
        }  
    }

posted @ 2011-04-28 20:06 haojinlian 阅读(1300) | 评论 (0)编辑 收藏

jsp-servlet简单登陆界面 有数据库连接

使用mysql数据库
使用tomcat服务器
数据库连接类:
package com.servlet;
import java.sql.*;
public class conn {
public static String name;
public static String mima;
public conn(String name,String mima){
conn.name =name;
conn.mima=mima;
    // 1. 注册驱动
    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch(ClassNotFoundException ex) {
        ex.printStackTrace();
    }
}
    public ResultSet date() {
     
        // 声明变量,使用,而后关闭
        Connection conn = null;        //数据库连接
        Statement stmt = null;         //数据库表达式
        ResultSet rs = null;             //结果集
        
        try {
            //2. 获取数据库的连接
            conn = DriverManager.getConnection
                ("jdbc:mysql://localhost:3306/dl","root","");
            
            //3. 获取表达式
            stmt = conn.createStatement();
            
            //4. 执行SQL
            String sql =  "select * from login where user='" + name
            + "' and pass = '" + mima + "'";
            rs = stmt.executeQuery(sql);
            
            //5. 现实结果集里面的数据
            //5. 现实结果集里面的数据
         //   while(rs.next()) {
             //   System.out.println("id为123的time值=" + rs.getString(1));
        //    }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
     
        finally {
           
        }
return rs;
    }
}
HttpServlet类:
package com.servlet;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;
import com.sun.xml.internal.bind.v2.runtime.Name;


/**
 * @author Administrator
 *
 */
public class login extends HttpServlet {

/**
*/
private static final long serialVersionUID = 1L;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
String name=req.getParameter("name");
String mima=req.getParameter("mima");
conn conn=new conn(name ,mima);
ResultSet rs=conn.date();
try {
if (rs.next())
  {
resp.sendRedirect("sucessed.jsp?name="+name);
  } else
  //否则登录失败
  {
    resp.sendRedirect("index.jsp");
  }
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.servlet.login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/jump.jsp</url-pattern>
</servlet-mapping>
</web-app>
index.php:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>

系 统 登 录

<form action="../dl/jump.jsp" method="post">


用户名
<input type="text" name="name">
<br>

密&nbsp;&nbsp;&nbsp;&nbsp;码
<input type="password" name="mima" />

<br>
<input type="submit" value="登录">

<input type="reset" value="取消">
</form>

</body>
</html>
sucessed.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>

您好:
<%String name = request.getParameter("name");
out.print(name);
%>
</body>
</html>

posted @ 2011-04-28 19:58 haojinlian 阅读(5579) | 评论 (2)编辑 收藏

Socket 服务器端和客户端简单对话

客户端:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class TalkClient {
public static void main(String args[]) {
try {
Socket socket = new Socket("127.0.0.1", 4700);
// 向本机的4700端口发出客户请求
BufferedReader sin = new BufferedReader(new InputStreamReader(
System.in));
// 由系统标准输入设备构造BufferedReader对象
PrintWriter os = new PrintWriter(socket.getOutputStream());
// 由Socket对象得到输出流,并构造PrintWriter对象
BufferedReader is = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
// 由Socket对象得到输入流,并构造相应的BufferedReader对象
String readline;
readline = sin.readLine(); // 从系统标准输入读入一字符串
while (!readline.equals("bye")) { // 若从标准输入读入的字符串为 "bye"则停止循环
os.println(readline); // 将从系统标准输入读入的字符串输出到Server
os.flush(); // 刷新输出流,使Server马上收到该字符串
System.out.println("Client:" + readline); // 在系统标准输出上打印读入的字符串
System.out.println("Server:" + is.readLine()); // 从Server读入一字符串,并打印
readline = sin.readLine(); // 从系统标准输入读入一字符串
} // 继续循环
os.close(); // 关闭Socket输出流
is.close(); // 关闭Socket输入流
socket.close(); // 关闭Socket
} catch (Exception e) {
System.out.println("Error" + e.getMessage()); // 出错,则打印出错信息}
}
}
}
服务器端:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class TalkServer {
public static void main(String args[]) {
try {
ServerSocket server = null;
try {
server = new ServerSocket(4700); // 创建一个ServerSocket在端口4700监听客户请求
} catch (Exception e) {
System.out.println("can not listen to:" + e); // 出错,打印出错信息
}
Socket socket = null;
try {
socket = server.accept();
// 使用accept()阻塞等待客户请求,有客户
// 请求到来则产生一个Socket对象,并继续执行
} catch (Exception e) {
System.out.println("Error." + e); // 出错,打印出错信息
}
String line;
BufferedReader is = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
// 由Socket对象得到输入流,并构造相应的BufferedReader对象
PrintWriter os = new PrintWriter(socket.getOutputStream());
// 由Socket对象得到输出流,并构造PrintWriter对象
BufferedReader sin = new BufferedReader(new InputStreamReader(
System.in));
// 由系统标准输入设备构造BufferedReader对象
System.out.println("Client:" + is.readLine()); // 在标准输出上打印从客户端读入的字符串
line = sin.readLine(); // 从标准输入读入一字符串
while (!line.equals("bye")) { // 如果该字符串为 "bye",则停止循环
os.println(line); // 向客户端输出该字符串
os.flush(); // 刷新输出流,使Client马上收到该字符串
System.out.println("Server:" + line); // 在系统标准输出上打印读入的字符串
System.out.println("Client:" + is.readLine());// 从Client读入一字符串,并打印输出
line = sin.readLine(); // 从系统标准输入读入一字符串继续循环
}
os.close(); // 关闭Socket输出流
is.close(); // 关闭Socket输入流
socket.close(); // 关闭Socket
server.close(); // 关闭ServerSocket
} catch (Exception e) {
System.out.println("Error:" + e); // 出错,打印出错信息
}
}
}

posted @ 2011-04-28 19:39 haojinlian 阅读(1685) | 评论 (0)编辑 收藏

java 连接数据库

public class JDBCTest {
public static void main(String[] arg) {
// 1. 注册驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}

// 声明变量,使用,而后关闭
Connection conn = null; // 数据库连接
Statement stmt = null; // 数据库表达式
ResultSet rs = null; // 结果集

try {
// 2. 获取数据库的连接
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/student", "root", "");

// 3. 获取表达式
stmt = conn.createStatement();
System.out.println("请输入操作指令:");
BufferedReader sReader = new BufferedReader(new InputStreamReader(
System.in));
String aaString = sReader.readLine();
String args[]=aaString.split(" ");
// stmt.execute(delsql);
if (args[0].equals("add")) {
// str=new String(rs.getBytes(1),"UTF-8")
// args[2] = new String(args[2]);
String addString="insert into stuinfo (stuno,stuname) values ('"+args[1]+"','"+args[2]+"')";
stmt.execute(addString);
}else if (args[0].equals("del")) {
String delsql = "delete from stuinfo where stuno="+args[1];
stmt.execute(delsql);
}
else  if (args[0].equals("update")) {
String update="update stuinfo set stuname='"+args[2]+"' where stuno='"+args[1]+"'";
stmt.executeUpdate(update);
}

else if (args[0].equals("select")) {

// String sql = "select * from stuinfo ";
// String selString="SELECT * FROM `stuinfo` WHERE stuname="+arg[2];
String selString2="select * from stuinfo where stuno='"+args[1]+"'";
// rs = stmt.executeQuery(selString);
rs=stmt.executeQuery(selString2);
}
// 4. 执行SQL

System.out.println("-----------------");
System.out.println("执行结果如下所示:");
System.out.println("-----------------");
System.out.println(" 学号" + "\t" + " 姓名");
System.out.println("-----------------");
// 5. 现实结果集里面的数据
String name = null;
while (rs.next()) {
name = rs.getString("stuname");
// name = new String(name.getBytes("ISO-8859-1"), "GB2312");
System.out.println("" + rs.getString("stuno") + "\t" + name);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

}
}

posted @ 2011-04-28 19:33 haojinlian 阅读(233) | 评论 (0)编辑 收藏

jsp使用servlet在xml处添加的信息q.html为这个servlet类的链接地址

<servlet>
<servlet-name>sda</servlet-name>
<servlet-class>com.servlet.Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sda</servlet-name>
<url-pattern>/q.html</url-pattern>
</servlet-mapping>

posted @ 2011-04-26 01:25 haojinlian 阅读(135) | 评论 (0)编辑 收藏

java代码实现连接mysql数据库

注意修改下面数据库的名称、登陆的账号密码。
我用的数据库名是:test
账号密码都是:root
=================================
import java.sql.*;
public class JDBCTest {
public static void main(String[] args) {
// 1. 注册驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch(ClassNotFoundException ex) {
ex.printStackTrace();
}
// 声明变量,使用,而后关闭
Connection conn = null;        //数据库连接
Statement stmt = null;         //数据库表达式
ResultSet rs = null;             //结果集
try {
//2. 获取数据库的连接
conn = DriverManager.getConnection
("jdbc:mysql://localhost:3306/test","root","root");
//3. 获取表达式
stmt = conn.createStatement();
//4. 执行SQL
String sql = "select time from shijian where id=123";
rs = stmt.executeQuery(sql);
//5. 现实结果集里面的数据
while(rs.next()) {
System.out.println("id为123的time值=" + rs.getString(1));
}
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
try {
if(rs != null) {
rs.close();
}
if(stmt!= null) {
stmt.close();
}
if(conn != null) {
conn.close();
}
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
}

posted @ 2011-04-26 00:38 haojinlian 阅读(137) | 评论 (0)编辑 收藏

URL对象的创建及使用 URL类中一些很基本的方法

URL类中一些很基本的方法如下:

·  public final Obect getContent() 这个方法取得传输协议。

·  public String getFile() 这个方法取得资源的文件名。

·  public String getHost() 这个方法取得机器的名称。

·  public int getPort() 这个方法取得端口号。

·  public String getProtocol() 这个方法取得传输协议。

·  public String toString() 这个方法把URL转化为字符串。

实例:URL对象的创建及使用

import java.net. *;
import java.io.*;
class Myurl  {
public static void main(String args[])   {
   try {
    URL url=new URL("http://www.tsinghua.edu.cn/chn/index.htm");
    System.out.println("the Protocol: "+url.getProtocol());
    System.out.println("the hostname: " +url.getHost());
    System.out.println("the port: "+url.getPort());
    System.out.println("the file:"+url.getFile());
System.out.println(url.toString());
}catch(MalformedURLException e) {
System.out.println(e);
     }
    }

}

posted @ 2011-04-21 19:50 haojinlian 阅读(1068) | 评论 (0)编辑 收藏

取得主机名和ip地址字符串

import java.net.*;
import java.io.*;
class host {
public static void main(String args[]) {
try {
InetAddress inetadd;
inetadd=InetAddress.getLocalHost();
System. out. println("hostname="+inetadd.getHostName());//取得主机名
System. out. println(inetadd.toString() );//取得主机名和ip地址字符串
}
catch(Exception e) {
System.out.println(e);
}
}
}

posted @ 2011-04-21 19:27 haojinlian 阅读(181) | 评论 (0)编辑 收藏

查询IP地址是IPV4还是IPV6,查询IP的类别

import java.net.*;
import java.io.*;

public class IPVersion {
public static void main(String args[]) {
try {
InetAddress inetadd = InetAddress.getLocalHost();
byte[] address = inetadd.getAddress();
System.out.println(address);
if (address.length == 4) {
System.out.println("The ip version is ipv4");
int firstbyte = address[0];
if (firstbyte < 0)
firstbyte += 256;
if ((firstbyte & 0x80) == 0)
System.out.println("the ip class is A");
else if ((firstbyte & 0xC0) == 0x80)
System.out.println("The ip class is B");
else if ((firstbyte & 0xE0) == 0xC0)
System.out.println("The ip class is C");
else if ((firstbyte & 0xF0) == 0xE0)
System.out.println("The ip class is D");
else if ((firstbyte & 0xF8) == 0xF0)
System.out.println("The ip class is E");
} else if (address.length == 16)
System.out.println("The ip version is ipv6");
} catch (Exception e) {
}
;
}
}

posted @ 2011-04-21 19:14 haojinlian 阅读(1098) | 评论 (1)编辑 收藏