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 閱讀(1308) | 評論 (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 閱讀(5591) | 評論 (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 閱讀(1690) | 評論 (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 閱讀(238) | 評論 (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 閱讀(136) | 評論 (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 閱讀(138) | 評論 (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 閱讀(1070) | 評論 (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 閱讀(183) | 評論 (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 閱讀(1145) | 評論 (1)編輯 收藏

          主站蜘蛛池模板: 库伦旗| 苍梧县| 武乡县| 微山县| 濮阳市| 兴义市| 元氏县| 尼勒克县| 龙川县| 景德镇市| 惠东县| 喀喇| 齐齐哈尔市| 曲阳县| 桦南县| 嘉祥县| 化州市| 永春县| 临泉县| 乾安县| 佳木斯市| 博兴县| 浦县| 平阳县| 叶城县| 建水县| 福建省| 新竹县| 上饶市| 遵化市| 新泰市| 彭水| 仙游县| 吉隆县| 深州市| 珠海市| 台北县| 元朗区| 伊宁县| 新沂市| 双鸭山市|