1、电池电量获得:package com.lee0000.BatteryCount;import android.app.Activity;import android.app.Dialog;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.view.View;import android.vie
2、w.Window;import android.view.WindowManager;import android.widget.Button;import android.widget.TextView;public class BatteryCount extends Activity /* Called when the activity is first created. */private int intLevel;private int intScale;private Button mButton01;private BroadcastReceiver mBatInfoRecei
3、ver = new BroadcastReceiver()public void onReceive(Context context,Intent intent)String action = intent.getAction();if(Intent.ACTION_BATTERY_CHANGED.equals(action)intLevel = intent.getIntExtra(“level“,0);intScale = intent.getIntExtra(“scale“, 100);onBatteryInfoReceiver(intLevel,intScale);Overridepub
4、lic void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.main);mButton01 = (Button) findViewById(R.id.myButton1);mButton01.setOnClickListener(new Button.OnClickListener()Overridepublic void onClick(View v) / TODO Auto-generated method stubregisterReceiv
5、er(mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED););public void onBatteryInfoReceiver(int intLevel,int intScale)final Dialog d = new Dialog(BatteryCount.this); d.setTitle(R.string.str_dialog_title);d.setContentView(R.layout.mydialog);Window window = d.getWindow();window.setFlags(W
6、indowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);TextView mTextView02 = (TextView) d.findViewById(R.id.mytextView2);mTextView02.setText(getResources().getString(R.string.str_dialog_body)+String.valueOf(intLevel * 100 / intScale)+“%“);Button mButton02 = (Button
7、) d.findViewById(R.id.button2);mButton02.setOnClickListener(new Button.OnClickListener()Overridepublic void onClick(View v) / TODO Auto-generated method stubunregisterReceiver(mBatInfoReceiver);d.dismiss(););d.show();解题思路: 1. 设计主界面 UI,以 LinearLayout 嵌套布局,加上组件:进度条 SeekBar、专辑封面、以及 5 个控制按钮 iamgeButton(
8、上一首、停止、开始、暂停、下一首)。2. 实现主界面 activitiy ,从 main.xml 布局中获得 ImageButton 对象、进度条对象,设置按钮监听器,监听器方法。由于要有后台播放功能。在点击播放按钮时,启动一个service,只有点击停止按钮,才能停止该 service。3. 实现 service 功能,并在 onStart 方法启动一个 MediaPlayer 对象,播放音乐;在onDestroy 中停止服务和音乐。4. 在 manifest.xml 中注册服务。 程序运行截图:实现代码:package com.android.lee0000;import android.
9、app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageButton;public class HomeWorkII extends Activity /* Called when the activity is first created. */Overridepublic
10、 void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.main);/获得 button1 的 idImageButton button1 = (ImageButton)findViewById(R.id.ImageButton03);/建立监听器button1.setOnClickListener(startIt);/获得 button2 的 idImageButton button2 = (ImageButton)findViewById(R.i
11、d.ImageButton02);/建立监听器button2.setOnClickListener(stopIt);private OnClickListener startIt = new OnClickListener()public void onClick(View v) /启动指定名字的服务startService(new Intent(“com.studio.android.START_AUDIO_SERVICE“);private OnClickListener stopIt = new OnClickListener()public void onClick(View v)/启
12、动指定名字的服务stopService(new Intent(“com.studio.android.START_AUDIO_SERVICE“);finish(); ;package com.android.lee0000;import android.app.Service;import android.content.Intent;import android.media.MediaPlayer;import android.os.IBinder;public class Music extends Service /这是一个在 MediaPlay 的对象 playerprivate Me
13、diaPlayer player;Overridepublic IBinder onBind(Intent intent) / TODO Auto-generated method stubreturn null;/播放开启public void onStart(Intent intent, int startId) super.onStart(intent, startId);/开始时播放的文件名,播放的文件命名是 a-z,0-9player = MediaPlayer.create(this, R.drawable.jayrainbow);player.start();/播放结束,释放资源
14、public void onDestroy() super.onDestroy();player.stop();package XMLandSQL;import java.awt.Color;import java.awt.Container;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import java
15、x.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JTextField;public class XMLandSQL extends JFrame implements ActionListenerprivate JLabel jLabelName;private JLabel jLabelPassword;private JTextField jTextFieldName;private JTextField jTextFieldPassword;private JBu
16、tton jButtonLogin;private JButton jLoginButton;private JButton jButtonClear;public XMLandSQL(String title)super(title);jLabelName = new JLabel(“用户名:“);jLabelPassword = new JLabel(“密码:“);jTextFieldName = new JTextField();jTextFieldName.setText(“请输入 SQL 数据库的登陆名字“);jTextFieldName.setForeground(Color.gr
17、ay);jTextFieldName.addFocusListener(new textFieldNameFocusListener();jTextFieldPassword = new JTextField();jTextFieldPassword.setText(“请输入 SQL 数据库的密码 “);jTextFieldPassword.setForeground(Color.gray);jTextFieldPassword.addFocusListener(new textFieldPasswordFocusListener();jButtonLogin = new JButton(“数
18、据库导入到 XML“);jButtonLogin.addActionListener(this);jLoginButton = new JButton(“XML 导入到数据库 “);jLoginButton.addActionListener(this);jButtonClear = new JButton(“清除“);jButtonClear.addActionListener(this);Container contentPane = this.getContentPane();contentPane.setLayout(new GridLayout(4,2);contentPane.ad
19、d(jLabelName);contentPane.add(jTextFieldName);contentPane.add(jLabelPassword);contentPane.add(jTextFieldPassword);contentPane.add(jButtonLogin);contentPane.add(jLoginButton);contentPane.add(jButtonClear);setBounds(80, 60, 400, 550);setVisible(true);setDefaultCloseOperation(EXIT_ON_CLOSE);public stat
20、ic void main(String a)new XMLandSQL(“);private class textFieldNameFocusListener implements FocusListenerOverridepublic void focusGained(FocusEvent e) / TODO Auto-generated method stubif(jTextFieldName.getForeground().equals(Color.gray)jTextFieldName.setText(“);jTextFieldName.setForeground(Color.BLAC
21、K);Overridepublic void focusLost(FocusEvent e) / TODO Auto-generated method stubif(jTextFieldName.getText().trim().isEmpty()jTextFieldName.setText(“请输入 SQL 数据库的登陆名字“);jTextFieldName.setForeground(Color.gray);private class textFieldPasswordFocusListener implements FocusListenerOverridepublic void foc
22、usGained(FocusEvent e) / TODO Auto-generated method stubif(jTextFieldPassword.getForeground().equals(Color.gray)jTextFieldPassword.setText(“);jTextFieldPassword.setForeground(Color.BLACK);Overridepublic void focusLost(FocusEvent e) / TODO Auto-generated method stubif(jTextFieldPassword.getText().tri
23、m().isEmpty()jTextFieldPassword.setText(“请输入 SQL 数据库的密码“);jTextFieldPassword.setForeground(Color.gray);Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif(e.getActionCommand().equals(“清除“)jTextFieldName.setText(“);jTextFieldPassword.setText(“);else if(e.getActionCo
24、mmand().equals(“数据库导入到 XML“)if(!(jTextFieldName.getText().trim().isEmpty()else if(e.getActionCommand().equals(“XML 导入到数据库 “)if(!(jTextFieldName.getText().trim().isEmpty()d.dom4j();package XMLandSQL;import java.io.File;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet
25、;import java.sql.Statement;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerFactory;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import org.
26、w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.w3c.dom.Text;public class append static String log;static String pass;public append(String log,String pass)this.log = log;this.pass = pass;tryDocumentBuilderFact
27、ory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();Document doc = builder.parse(“schema.xml“);doc.normalize();Element root = doc.getDocumentElement();accessDB(doc,root);if(doc!=null)printNode(root);TransformerFactory tf = TransformerFactory.newI
28、nstance();Transformer transformer = tf.newTransformer();DOMSource source = new DOMSource(doc);StreamResult result = new StreamResult(new File(“schema.xml“);transformer.transform(source, result);catch(Exception e)e.printStackTrace();/访问数据库public static void accessDB(Document doc,Element root)tryClass
29、.forName(“com.mysql.jdbc.Driver“);Connection con = DriverManager.getConnection(“jdbc:mysql:/localhost/lee00002008“,log,pass);Statement stmt = con.createStatement();ResultSet rs = stmt.executeQuery(“select * from users“);createXML(doc,root,rs);rs.close();stmt.close();con.close();catch(Exception e)e.p
30、rintStackTrace();/创建 XML 结点public static void createXML(Document doc,Element root,ResultSet rs)tryboolean flag = rs.next();while(flag)Element Users = doc.createElement(“Users“);Element userid = doc.createElement(“userid“);Text userid_text = doc.createTextNode(“+rs.getInt(“userid“);userid.appendChild
31、(userid_text);Element username = doc.createElement(“username“);Text username_text = doc.createTextNode(“+rs.getString(“username“);username.appendChild(username_text);Element password = doc.createElement(“password“);Text password_text = doc.createTextNode(“+rs.getString(“password“);password.appendChi
32、ld(password_text);/Users.appendChild(userid);Users.appendChild(username);Users.appendChild(password);root.appendChild(Users);flag=rs.next();catch(Exception e)e.printStackTrace();private static void printNode(Element element)NodeList children = element.getChildNodes();NamedNodeMap attr = element.getAttributes();int r = children.getLength();if(attr!=null)System.out.print(“);else if(attr = null)System.out.println(“);if(element.hasChildNodes()for(int k=0;k“);