目 录
一、需求分析
二、系统总框图和功能模块说明
2.1 系统用例图
2.2 系统总框图 2
2.3 功能模块说明 3
三、系统设计 4
3.1 系统类结构设计 4
3.2 系统部分重要功能的时序图 7
3.3 数据库设计 9
3.3.1 数据库实体E-R图分析
3.3.2 详细设计
四、系统调试
五、界面效果 20
六、总结 24
七、源程序清单 24
7.1 MainActivity.java文件 24
7.2 Fragment文件 28
7.3 DBHeiper.java文件 51
7.4 MusicService.java 54
一、需求分析
在信息高度发达的背景之下,总是无时无刻诞生出许多创新的事物。对于中医而言人脑无法时时刻刻记住许多繁杂的药方。中华自古以来先人积累了很多有用的药方和宝贵的经验,在西药的冲击之下,我们仍然需要去学习中医,并取西医的精华,做到中西合璧。而互联网时代利用安卓相关技术可以制作一款方便的手机app,用以随时记录药方,查询药方,修改药方等。还可以通过摄像头拍摄药草、多媒体应用等让中药软件变得更加具有实用性。
该软件需要实现,其一辅助中医大夫储备药方、药名药理备查。让大夫能腾出精力集中诊断分析,而不必消耗精力去记忆琐碎的细节。其二,基于安卓,便于随时随地方便时录入储备或完善经验方。更加有利于查询。其三,使用通用的中医药数据库不需要上网就可以查询一部分药方。其四,查询功能方便快捷,有友好方便的图形界面等。
图1 功能流程图
通过上述流程图可以看出该软件包括注册登录,播放背景音乐,加减/新增药方,注册新用户,相机记录药材,药方介绍视频播放,管理药方信息,查看药方信息等模块。
二、系统总框图和功能模块说明
2.1 系统用例图
图2 用例图
、
2.2 系统总框图
图3 系统总框图
2.3 功能模块说明
从需求分析可以找到,该系统分为用户使用的药方基本功能和多媒体相关的功能。
中医大夫助理信息系统包括以下主要功能:
(1)注册登录功能:用户注册,登录;
(2)查看药方功能:用户查找药方,并选择需要药方具体查看;
(3)管理药方:用户可增加药方,修改药方,删除药方;
(4)播放音乐:点击播放可以播放后台音乐,暂停和停止可以停止音乐;
(5)药方介绍视频:有暂停、播放、快进、快退等功能。
(6)相机记录药材:用手机的相机功能记录药材,增加中医大夫助理信息系统的可用性。
(7)补间结合帧动画动态显示:补间动画和帧动画的结合,增加中医大夫主力信息系统的界面友好性。
三、系统设计
3.1 系统类结构设计
图4 系统类图
3.2 系统部分重要功能的时序图(或者流程图)
图5 用户登录时序图
图6 用户查询药方时序图
图7 用户查看指定药方细节
图8 用户添加药方细节时序图
图9 用户删除药方细节等
3.3 数据库设计
3.3.1 数据库实体E-R图分析
图11 用户实体
-
图12 药物列表
图13 辩证分类
图14 方剂列表
图15 方剂药物对应
图16 总系统实体E-R图
3.3.2 详细设计
(一)设计表
将E-R图转换为关系模型一般遵循如下原则:
(1)一个实体型转换为一个关系模式。实体的属性就是关系的属性,实体的码就是关系的码。
(2)一个m:n联系转换为一个关系模式。与该联系相连的各实体的码以及联系本身的属性均转换为关系的属性,而关系的码为各实体码的组合。
(3)一个1:n联系可以转换为一个独立的关系模式,也可以与n端对应的关系模式合并。如果转换为一个独立的关系模式,则与该联系相连的各实体的码以及联系本身的属性均转换为关系的属性,而关系的码为n端实体的码。
(4)一个1:1联系可以转换为一个独立的关系模式,也可以与任意一端对应的关系模式合并。
根据上述原则设计的数据表如下:
用户实体:用户(用户ID、用户名、密码)
药物列表实体:药物(药物ID、药物名字、拼音代码、所属类别、别名、药量单位、药效、功效、应用)
辩证分类实体:分类(辩证类别、拼音代码、父类、序号)
方剂药物对应实体:对应(方剂名称、药物名称、药量、序号)
方剂列表实体:方剂(用法及注意、歌诀、功能、拼音代码、别名、所属类别、方剂名称、序号、主治)
(二)设计数据字典
数据字典:中医大夫助手管理信息系统会涉及大量的静态数据,如用户姓名、药材名称、方剂名称、药材别名、拼音代码、辨证类别等等,这些数据,其数据的组织结构基本一致,现为该系统设计四个数据字典:用户字典、药材字典、辩证字典、方剂字典。
用户字典user:
属性名称 |
属性类型 |
是否为空 |
约束 |
含义 |
id |
integer |
NOT NULL |
主键 |
用户ID |
name |
varchar(20) |
NOT NULL |
|
用户名 |
password |
varchar(20) |
NOT NULL |
|
密码 |
|
|
|
|
|
表1 用户表
药材字典equipment:
属性名称 |
属性类型 |
是否为空 |
约束 |
含义 |
PK_ID |
integer |
NOT NULL |
主键 |
序号 |
NAME |
text |
NOT NULL |
|
药物名 |
ALIAS |
text |
|
|
别名 |
KEY_CODES |
text |
NOT NULL |
|
拼音代码 |
UNIT |
text |
NOT NULL |
|
单位 |
POISON |
int |
|
|
毒性 |
SYNDROME_ID |
integer |
|
|
|
PROPERTY |
text |
|
|
|
EFFICACY |
text |
|
|
|
INDICATION |
text |
|
|
|
SYNERGIST |
text |
|
|
|
DESCRIPTION |
text |
|
|
描述 |
表2 药物表
辩证字典:
属性名称 |
属性类型 |
是否为空 |
约束 |
含义 |
PK_ID |
integer |
NOT NULL |
主键 |
序号 |
NAME |
TEXT |
NOT NULL |
|
药物名字 |
KEY_CODES |
TEXT |
NOT NULL |
|
拼音代码 |
DESCRIPTION |
TEXT |
|
|
描述 |
PARENT_ID |
integer |
|
外键 |
父类 |
|
|
|
|
|
表3 辩证表
方剂字典:
属性名称 |
属性类型 |
是否为空 |
约束 |
含义 |
PK_ID |
integer |
NOT NULL |
主键 |
序号 |
NAME |
Text |
NOT NULL |
|
方剂名称 |
ALIAS |
text |
|
|
别名 |
KEY_CODES |
text |
NOT NULL |
|
|
SYNDROME_ID |
integer |
|
外键 |
|
EFFICACY |
text |
|
|
|
INDICATION |
text |
|
|
|
DESCRIPTION |
Text |
|
|
|
ORIGIN |
text |
|
|
出处 |
BASE_ID |
integer |
|
外键 |
|
表4 方剂表
属性名称 |
属性类型 |
是否为空 |
约束 |
含义 |
RX_RECIPE_ID |
integer |
NOT NULL |
外键 |
方剂序号 |
MEDICINE_ID |
Integer |
NOT NULL |
外键 |
药物序号 |
ORDER_NUM |
real |
NOT NULL |
|
|
IS_OPTIONAL |
char |
|
UK_MAP_RX_RECIPE_MEDICINE |
|
表5 药物方剂对应表
创建 SYNDROME_SUBJECT 表的语句
create table SYNDROME_SUBJECT
(
PK_ID integer primary key autoincrement,
NAME text unique not null,
KEY_CODES text not null, -- 关键代码(拼音), comma seperated to including alias, etc.
DESCRIPTION text,
PARENT_ID integer
references SYNDROME_SUBJECT(PK_ID) on delete set null
);
创建 RX_RECIPE 表的语句
create table RX_RECIPE
(
PK_ID integer primary key autoincrement,
NAME text unique not null,
ALIAS text,
KEY_CODES text not null,
SYNDROME_ID integer
references SYNDROME_SUBJECT(PK_ID) on delete restrict,
EFFICACY text,
INDICATION text,
DESCRIPTION text,
ORIGIN text, -- 出处
BASE_ID integer
references RX_RECIPE(PK_ID) on delete restrict
);
创建 MEDICINE 表的语句
create table MEDICINE
(
PK_ID integer primary key autoincrement,
NAME text unique not null,
ALIAS text,
KEY_CODES text not null,
UNIT text not null
check(UNIT in ('克', '毫升', '枚')) default '克', -- 药量单位: "克" 或 "毫升" 或 "枚"
POISON int not null default 3, -- 毒性: 是药三分毒
SYNDROME_ID integer
references SYNDROME_SUBJECT(PK_ID) on delete restrict,
PROPERTY text,
EFFICACY text,
INDICATION text,
SYNERGIST text,
DESCRIPTION text
);
创建 admin 表的语句
create table equipment(
id integer primary key,
name varchar(20),
)
创建MAP_RX_RECIPE_MEDICINE表的语句
create table MAP_RX_RECIPE_MEDICINE
(
RX_RECIPE_ID integer not null
references RX_RECIPE(PK_ID) on delete restrict,
MEDICINE_ID integer not null
references MEDICINE(PK_ID) on delete restrict,
ORDER_NUM real not null,
QUANTITY real not null,
IS_OPTIONAL char -- 常用可加减药物
check(IS_OPTIONAL in ('Y', null)) default null,
constraint UK_MAP_RX_RECIPE_MEDICINE unique(RX_RECIPE_ID, MEDICINE_ID)
);
四、系统调试
(1)背景音乐
问题:app启动后可以选择播放背景音乐,使用service无法在视觉上判断背景音乐是否进行播放。播放音乐无法暂停等。
解决方法:同时使用service 和broadcast,当点击菜单选项时,通过广播的方式通知页面发生改变。
图15 背景音乐调试
(2)补间动画和帧动画
问题:要完成一个位置和图片内容不断变化的动态显示图,点击按钮可以实现该图片的动态显示
解决方法:结合所给的代码,将补间动画和帧动画进行结合,实现图片一位置不断顺序变化,图片内容也不断变化
(3)播放视频
问题:播放视频时无法快进、快退等,控制视频播放的时长和快进到自己想要的位置
解决方法:增加MediaController对视频播放进行控制
(4)相机功能
问题:相机功能单一,无法调整亮度等设置,拍摄出来的结果不尽人意
解决方法:修改代码,直接调用手机本身照相机功能拍摄。
五、界面效果
图17 登录和注册界面(开启动画) 图18 登录和注册界面
图19 主界面 图20 搜索方剂
图21 方剂细节 图22修改方剂
图23 视频播放 图24 音乐播放
六、总结
经过这一学期对安卓的学习以及这两周的安卓大作业的完成,让我对安卓编程有了一定的了解,期间也遇到很多虚拟机调试的问题,其中耽误我最多时间的是视频播放,虚拟机是播放不了视频的,需要真机调试。除此之外,大部分时间在数据库那一部分,这一部分很多SQL操作的忘记了,需要一遍遍翻书和查找网上资料进行了解,其中,药方的内容属于网上查找的部分药方,用于调试内容,网上有现成的药方xls文件等。关于帧动画和补间动画的结合也研究了一下,属于一个小扩展。
最后,这次的安卓线上线下教授和自主学习让我受益匪浅,从一个对安卓编程小白到初入门槛,后面的时间我会不断完善我的APP,让它更实用。
七、源程序清单
7.1 MainActivity.java文件
package efan.zz.android.activity;
import android.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.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import efan.zz.android.R;
//import efan.zz.android.ZZ;
//import efan.zz.android.util.ZzUtil;
public class MainActivity extends Activity implements OnClickListener {
/*
*<string name="welcome_main">中医药助手大夫菜单</string>
<string name="user_register">1.用户登录注册</string>
<string name="medicine_query"> 2.中药药方查询</string>
<string name="medicine_change">3.药方增删改查</string>
<string name="music_play">4.播放背景音乐</string>
<string name="camera_record">5.相机记录药材</string>
<string name="video_demo">6.使用视频演示</string>
*
**/
private Button btn_user_register;
private Button btn_medicine_query;
private Button btn_medicine_change;
private Button btn_medicine_music_play;
private Button btn_medicine_camera_record;
private Button btn_video_demo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.medicine_main);
btn_user_register = (Button) this.findViewById(R.id.button1);
btn_medicine_query= (Button) this.findViewById(R.id.button2);
btn_medicine_change = (Button) this.findViewById(R.id.button3);
btn_medicine_music_play = (Button) this.findViewById(R.id.button4);
btn_medicine_camera_record = (Button) this.findViewById(R.id.button5);
btn_video_demo = (Button) this.findViewById(R.id.button6);
btn_user_register.setOnClickListener(this);
btn_medicine_query.setOnClickListener(this);
btn_medicine_change.setOnClickListener(this);
btn_medicine_music_play.setOnClickListener(this);
btn_medicine_camera_record.setOnClickListener(this);
btn_video_demo.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
Intent intent1 = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent1);
break;
case R.id.button2:
Intent intent2 = new Intent(MainActivity.this, WelcomeRxRecipeQuery.class);
startActivity(intent2);
break;
case R.id.button3:
Intent intent3 = new Intent(MainActivity.this,RxRecipeDetailEdit.class);
startActivity(intent3);
break;
case R.id.button4:
Intent intent4 = new Intent(MainActivity.this, background_music.class);
startActivity(intent4);
break;
case R.id.button5:
Intent intent5 = new Intent(MainActivity.this, photo_record.class);
startActivity(intent5);
break;
case R.id.button6:
Intent intent6 = new Intent(MainActivity.this, video_play.class);
startActivity(intent6);
break;
}
}
}
7.2 Fragment文件
(1)LoginActivity
package efan.zz.android.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.AnimationDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import efan.zz.android.R;
import efan.zz.android.UserData;
import efan.zz.android.UserDataManager;
//import efan.zz.android.ZZ;
//import efan.zz.android.util.ZzUtil;
public class LoginActivity extends Activity implements OnClickListener {
private EditText mAccount;
private EditText mPwd;
private Button mRegisterButton;
private Button mLoginButton;
private Button mCancleButton;
private View loginView;
private View loginSuccessView;
private TextView loginSuccessShow;
private UserDataManager mUserDataManager;
private ImageView iv_flower;
private Button btn_start;
private AnimationDrawable animation;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.medicine_login);
mAccount = (EditText) findViewById(R.id.login_edit_account);
mPwd = (EditText) findViewById(R.id.login_edit_pwd);
mRegisterButton = (Button) findViewById(R.id.login_btn_register);
mLoginButton = (Button) findViewById(R.id.login_btn_login);
mCancleButton = (Button) findViewById(R.id.login_btn_cancle);
loginView=findViewById(R.id.login_view);
//loginSuccessView=findViewById(R.id.login_success_view);
//loginSuccessShow=(TextView) findViewById(R.id.login_success_show);
iv_flower = (ImageView) findViewById(R.id.iv_flower);
iv_flower.setVisibility(View.INVISIBLE);
btn_start=(Button) findViewById(R.id.btn_play);
iv_flower.setOnClickListener(this);
btn_start.setOnClickListener(this);
//拿到AnimationDrawable对象
animation = (AnimationDrawable)iv_flower.getBackground();
mRegisterButton.setOnClickListener(mListener);
mLoginButton.setOnClickListener(mListener);
mCancleButton.setOnClickListener(mListener);
if (mUserDataManager == null) {
mUserDataManager = new UserDataManager(this);
mUserDataManager.openDataBase();
}
}
OnClickListener mListener = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.login_btn_register:
register();
break;
case R.id.login_btn_login:
login();
break;
case R.id.login_btn_cancle:
cancle();
break;
}
}
};
public void login() {
if (isUserNameAndPwdValid()) {
String userName = mAccount.getText().toString().trim();
String userPwd = mPwd.getText().toString().trim();
int result=mUserDataManager.findUserByNameAndPwd(userName, userPwd);
if(result==1){
//login success
//loginView.setVisibility(View.GONE);
//loginSuccessView.setVisibility(View.VISIBLE);
//loginSuccessShow.setText(getString(R.string.user_login_sucess, userName));
Toast.makeText(this, getString(R.string.login_sucess),
Toast.LENGTH_SHORT).show();
Intent intent1 = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent1);
}else if(result==0){
//login failed,user does't exist
Toast.makeText(this, getString(R.string.login_fail),
Toast.LENGTH_SHORT).show();
}
}
}
public void onClick(View v) {
//播放动画
if(!animation.isRunning()){
iv_flower.setVisibility(View.VISIBLE);
animation.start();
Animation ani1 = AnimationUtils.loadAnimation(this,
R.anim.scale_animation);
iv_flower.startAnimation(ani1);
btn_start.setBackgroundResource(android.R.drawable.ic_media_pause);
}else{
iv_flower.setVisibility(View.INVISIBLE);
Animation ani1 = AnimationUtils.loadAnimation(this,
R.anim.scale_animation);
iv_flower.clearAnimation();
animation.stop();
btn_start.setBackgroundResource(android.R.drawable.ic_media_play);
}
}
public void register() {
if (isUserNameAndPwdValid()) {
String userName = mAccount.getText().toString().trim();
String userPwd = mPwd.getText().toString().trim();
//check if user name is already exist
int count=mUserDataManager.findUserByName(userName);
if(count>0){
Toast.makeText(this, getString(R.string.name_already_exist, userName),
Toast.LENGTH_SHORT).show();
return ;
}
UserData mUser = new UserData(userName, userPwd);
mUserDataManager.openDataBase();
long flag = mUserDataManager.insertUserData(mUser);
if (flag == -1) {
Toast.makeText(this, getString(R.string.register_fail),
Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, getString(R.string.register_sucess),
Toast.LENGTH_SHORT).show();
}
}
}
public void cancle() {
mAccount.setText("");
mPwd.setText("");
}
public boolean isUserNameAndPwdValid() {
if (mAccount.getText().toString().trim().equals("")) {
Toast.makeText(this, getString(R.string.account_empty),
Toast.LENGTH_SHORT).show();
return false;
} else if (mPwd.getText().toString().trim().equals("")) {
Toast.makeText(this, getString(R.string.pwd_empty),
Toast.LENGTH_SHORT).show();
return false;
}
return true;
}
@Override
protected void onResume() {
if (mUserDataManager == null) {
mUserDataManager = new UserDataManager(this);
mUserDataManager.openDataBase();
}
super.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onPause() {
if (mUserDataManager != null) {
mUserDataManager.closeDataBase();
mUserDataManager = null;
}
super.onPause();
}
}
(2) Mymusic
package efan.zz.android.activity;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import efan.zz.android.R;
import efan.zz.android.ZZ;
import efan.zz.android.common.IdentifiedString;
import efan.zz.android.common.android.IdentifiedStringAdapter;
public class Mymusic extends Service {
private NotificationManager mymn;
private MediaPlayer myplayer;
private MediaPlayer mp;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onCreate(){
super.onCreate();
myplayer=MediaPlayer.create(this,R.raw.music);
//mymn=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//@SuppressWarnings("deprecation")
//Notification notification=new Notification(R.drawable.ic_launcher,"Service start",System.currentTimeMillis());
//PendingIntent contentIntent=PendingIntent.getActivity(this, 0, new Intent(this,MainActivity.class), 0);
//notification.setLatestEventInfo(this, "测试Service ", "歌曲正在播放中", contentIntent);
//mymn.notify(R.string.hello_world,notification);
}
public void onStart(Intent intent,int startID){
//myplayer.start();
super.onStart(intent, startID);
int i=intent.getIntExtra("action", 0);
if(i==1){
if(null==mp){
mp=MediaPlayer.create(this, R.raw.music);
mp.setOnCompletionListener(new OnCompletionListener(){
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
stopSelf();
}
});
}
mp.start();
}else if(i==2){
if(mp!=null&&mp.isPlaying()){
mp.pause();
}
}
Log.v("zhuangtai",String.valueOf(i));
Intent in=new Intent("com.user.action");
in.putExtra("action", i);
sendBroadcast(in);
}
/*public void onDestory(){
myplayer.stop();
super.onDestroy();
}*/
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
//先停止 再释放
if(myplayer.isPlaying()){
mp.stop();
Intent in=new Intent("com.user.action");
in.putExtra("action", 3);
sendBroadcast(in);
}
mp.release();
}
}
(3) RxRecipeDetail
/*
* Copyright 2009 eFANsoftware
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package efan.zz.android.activity;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import efan.zz.android.R;
import efan.zz.android.ZZ;
public class RxRecipeDetail extends Activity
{
private String rxId;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.rx_recipe_detail);
init();
editRecipeAction();
}
private void init()
{
final Intent intent = getIntent();
this.rxId = Uri.decode(intent.getDataString());
// query DB for detail...
final String sql = getResources().getString(R.string.SQL_QUERY_RX_RECIPE_BY_ID);
final Cursor cursor = ZZ.db.rawQuery(sql, new String[]{rxId});
String name = null;
String desc = null;
String alias = null;
try
{
cursor.moveToNext();
name = cursor.getString(0);
desc = cursor.getString(2);
alias = cursor.getString(3);
}
finally
{
cursor.close();
}
final TextView nameView = (TextView) findViewById(R.id.rxFormulaName);
if (alias != null && alias.trim().length() > 0)
name += " (" + alias + ")";
nameView.setText(name);
final TextView descView = (TextView) findViewById(R.id.rxFormulaDesc);
descView.setText(desc.replace("\\n", "\n"));
// query medicines & display in table
final TableLayout medTab = (TableLayout) findViewById(R.id.rxFormulaDetailTab);
loadRxMedicines(rxId, medTab);
// Draw bottom line to close the table
final View lineView = new View(this);
lineView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 4));
lineView.setBackgroundResource(R.color.tabLineColor);
medTab.addView(lineView);
}
private void loadRxMedicines(final String rxId, final TableLayout medTab)
{
final String sql = getResources().getString(R.string.SQL_LOAD_RX_MEDICINES_BY_ID);
final Cursor cursor = ZZ.db.rawQuery(sql, new String[]{rxId});
try
{
float defTextSize = getResources().getDimension(R.dimen.default_text_size);
while (cursor.moveToNext())
{
String name = cursor.getString(1);
String quantity = cursor.getString(2);
String unit = cursor.getString(3);
boolean isOptional = "Y".equals(cursor.getString(4));
TextView nameView = new TextView(this);
nameView.setText(name);
nameView.setGravity(Gravity.LEFT);
nameView.setTextSize(defTextSize);
TextView quantityView = new TextView(this);
quantityView.setText(quantity + unit.toString());
quantityView.setGravity(Gravity.CENTER);
quantityView.setTextSize(defTextSize);
try
{
if (Float.parseFloat(quantity) <= 0.0f)
{
quantityView.setTextColor(Color.RED);
}
} catch (Exception e)
{
Log.d(this.getClass().getName(), "", e);
}
CheckBox isOptionalCB = new CheckBox(this);
isOptionalCB.setChecked(isOptional);
isOptionalCB.setClickable(false);
isOptionalCB.setFocusable(false);
isOptionalCB.setGravity(Gravity.RIGHT);
isOptionalCB.setLines(1);
isOptionalCB.setLineSpacing(0, 1);
TableRow row = new TableRow(this);
row.addView(nameView);
row.addView(quantityView);
row.addView(isOptionalCB);
medTab.addView(row);
}
}
finally
{
cursor.close();
}
}
private void editRecipeAction()
{
final Button btn = (Button) findViewById(R.id.rxFormulaEditBtn);
btn.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
Uri data = Uri.parse(Uri.encode(""+rxId));
Intent nextAct = new Intent();
nextAct.setData(data);
nextAct.setAction(Intent.ACTION_INSERT_OR_EDIT);
nextAct.setClass(RxRecipeDetail.this, RxRecipeDetailEdit.class);
startActivity(nextAct);
}
});
}
public static void show(String rxId, Activity caller)
{
Uri data = Uri.parse(Uri.encode(rxId));
Intent nextAct = new Intent();
nextAct.setData(data);
nextAct.setAction(Intent.ACTION_VIEW);
nextAct.setClass(caller, RxRecipeDetail.class);
caller.startActivity(nextAct);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the home menu XML resource.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.prescription, menu);
return true;
}
}
(4) RxRecipeDetailEdit
package efan.zz.android.activity;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import efan.zz.android.R;
import efan.zz.android.ZZ;
import efan.zz.android.common.android.IdentifiedAutoCompleteTextView;
import efan.zz.android.common.android.IdentifiedStringAdapter;
/*
* Implementation decision: copy&paste RxRecipeDetail.java code,
* Only pass rx_recipe id as parameter,
* To query the DB again: if it's slow to user,
* the query must be optimised anyway otherwise it's slow in RxRecipeDetail page already!
*/
public class RxRecipeDetailEdit extends Activity
{
private EditText nameView;
private EditText aliasView;
private EditText keyCodeView;
private EditText descView;
private TableLayout medTab;
private static IdentifiedStringAdapter medsAdapter;
private String rxId0;
private String name0;
private static OnClickListener insertBtnListener, delBtnListener;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.rx_recipe_detail_edit);
init();
initSaveAction();
initCancelAction();
}
private void init()
{
final Intent intent = getIntent();
if(Uri.decode(intent.getDataString())!=null){
rxId0 = Uri.decode(intent.getDataString());
// query DB for detail...
final String sql = getResources().getString(R.string.SQL_QUERY_RX_RECIPE_BY_ID);
final Cursor cursor = ZZ.db.rawQuery(sql, new String[]{rxId0});
String name = null;
String desc = null;
String alias = null;
String keyCode = null;
try
{
cursor.moveToNext();
name = cursor.getString(0);
keyCode = cursor.getString(1);
desc = cursor.getString(2);
alias = cursor.getString(3);
}
finally
{
cursor.close();
}
nameView = (EditText) findViewById(R.id.rxFormulaName);
nameView.setText(name);
name0 = name;
aliasView = (EditText) findViewById(R.id.rxFormulaAlias);
aliasView.setText(alias);
keyCodeView = (EditText) findViewById(R.id.rxFormulaKeyCode);
keyCodeView.setText(keyCode);
descView = (EditText) findViewById(R.id.rxFormulaDesc);
descView.setText(desc.replace("\\n", "\n"));
}else{
String name = null;
String desc = null;
String alias = null;
String keyCode = null;
nameView = (EditText) findViewById(R.id.rxFormulaName);
nameView.setText("");
name0 = name;
aliasView = (EditText) findViewById(R.id.rxFormulaAlias);
aliasView.setText("");
keyCodeView = (EditText) findViewById(R.id.rxFormulaKeyCode);
keyCodeView.setText("");
descView = (EditText) findViewById(R.id.rxFormulaDesc);
descView.setText(desc.replace("\\n", "\n"));
//descView.setText("");
}
}
@Override
public void onStart()
{
super.onStart();
// query medicines & display in table
loadMedsAutoCompAdapter();
initInsertDelListeners();
medTab = (TableLayout) findViewById(R.id.rxFormulaDetailTab);
loadRxMedicines(rxId0, medTab);
// Add empty line at the end for adding medicines to the end
TableRow nilRow = new TableRow(this);
Button lastInsertBtn = new Button(this);
lastInsertBtn.setGravity(Gravity.CENTER | Gravity.LEFT);
lastInsertBtn.setText("+");
lastInsertBtn.setOnClickListener(insertBtnListener);
EditText nilText = new EditText(this);
nilText.setEnabled(false);
nilText.setText("...");
nilRow.addView(lastInsertBtn);
nilRow.addView(nilText);
medTab.addView(nilRow);
}
private void loadMedsAutoCompAdapter()
{
if (medsAdapter != null)
return;
medsAdapter = new IdentifiedStringAdapter(this, android.R.layout.simple_dropdown_item_1line);
final String sql = getResources().getString(R.string.SQL_LOAD_MEDICINE_FOR_AC);
medsAdapter.loadNameOrKeyAutoCompAdapter(sql);
}
private void initInsertDelListeners()
{
insertBtnListener = new OnClickListener()
{
public void onClick(View v)
{
((TableRow) v.getParent()).requestFocus();
final TableRow newRow = buildTableRow(-1, "", "", false);
final View currentRow = medTab.getFocusedChild();
final int idx = medTab.indexOfChild(currentRow);
medTab.addView(newRow, idx);
newRow.requestFocus();
}
};
delBtnListener = new OnClickListener()
{
public void onClick(View v)
{
((TableRow) v.getParent()).requestFocus();
final View currentRow = medTab.getFocusedChild();
medTab.removeView(currentRow);
}
};
}
private void loadRxMedicines(final String rxId, final TableLayout medTab)
{
final String sql = getResources().getString(R.string.SQL_LOAD_RX_MEDICINES_BY_ID);
final Cursor cursor = ZZ.db.rawQuery(sql, new String[]{rxId});
try
{
while (cursor.moveToNext())
{
int id = cursor.getInt(0);
String name = cursor.getString(1);
String quantity = cursor.getString(2);
boolean isOptional = "Y".equals(cursor.getString(4));
TableRow row = buildTableRow(id, name, quantity, isOptional);
medTab.addView(row);
}
}
finally
{
cursor.close();
}
}
private TableRow buildTableRow(int id, String name, String quantity, boolean isOptional)
{
Button insertBtn = new Button(this);
insertBtn.setGravity(Gravity.CENTER | Gravity.CENTER);
insertBtn.setText("+");
insertBtn.setOnClickListener(insertBtnListener);
final IdentifiedAutoCompleteTextView medNameText = new IdentifiedAutoCompleteTextView(this);
medNameText.setValueId(id);
final EditText qtyText = new EditText(this);
final StringBuilder isClicked = new StringBuilder();
medNameText.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
medNameText.setValueId(medsAdapter.getItem(position).id);
isClicked.append(Boolean.TRUE);
qtyText.requestFocus();
}
});
medNameText.setThreshold(1);
medNameText.setGravity(Gravity.CENTER | Gravity.LEFT);
medNameText.setAdapter(medsAdapter);
final StringBuilder nameStr0 = new StringBuilder();
medNameText.setOnFocusChangeListener(new OnFocusChangeListener()
{
public void onFocusChange(View v, boolean hasFocus)
{
String nameVal0 = medNameText.getText().toString();
if (hasFocus)
{
nameStr0.replace(0, nameStr0.length(), nameVal0);
if (! medsAdapter.isReady())
// ZzUtil.doWait(medsAdapter);
medNameText.setText("");
ZZ.inputMethodMgr.hideSoftInputFromWindow(v.getWindowToken(), 0);
medNameText.showDropDown();
isClicked.delete(0, isClicked.length());
}
else
{
medNameText.dismissDropDown();
if (isClicked.length() == 0)
{
medNameText.setText(nameStr0.toString());
}
}
}
});
medNameText.setText(name);
qtyText.setInputType(InputType.TYPE_CLASS_NUMBER);
qtyText.setGravity(Gravity.CENTER | Gravity.RIGHT);
qtyText.setText(quantity);
qtyText.setMaxEms(4);
CheckBox optionalCBox = new CheckBox(this);
optionalCBox.setGravity(Gravity.CENTER);
optionalCBox.setChecked(isOptional);
Button delBtn = new Button(this);
delBtn.setGravity(Gravity.CENTER | Gravity.CENTER);
delBtn.setText("-");
delBtn.setOnClickListener(delBtnListener);
TableRow row = new TableRow(this);
row.addView(insertBtn);
row.addView(medNameText);
row.addView(qtyText);
row.addView(optionalCBox);
row.addView(delBtn);
return row;
}
private void initSaveAction()
{
Button.OnClickListener listener = new Button.OnClickListener()
{
public void onClick(View v)
{
int confirmMsgId = R.string.rxFormula_detail_edit_save_confirm_insert;
String name1 = nameView.getText().toString().trim();
final boolean isNew = ! name0.equals(name1);
if (! isNew)
{
confirmMsgId = R.string.rxFormula_detail_edit_save_confirm_update;
}
new AlertDialog.Builder(RxRecipeDetailEdit.this)
.setTitle(R.string.alert_dialog_confirm_title)
.setIcon(R.drawable.alert_dialog_icon)
.setMessage(confirmMsgId)
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
String rxId = saveData(isNew);
RxRecipeDetail.show(rxId, RxRecipeDetailEdit.this);
}
})
.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton) {}
})
.create()
.show();
}
};
Button btn1 = (Button) findViewById(R.id.rxFormulaSaveBtn1);
Button btn2 = (Button) findViewById(R.id.rxFormulaSaveBtn2);
btn1.setOnClickListener(listener);
btn2.setOnClickListener(listener);
}
private void initCancelAction()
{
Button btn = (Button) findViewById(R.id.rxFormulaCancelBtn);
btn.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
RxRecipeDetail.show(rxId0, RxRecipeDetailEdit.this);
}
});
}
private String saveData(boolean isNew)
{
ZZ.db.beginTransaction();
try
{
final String name = nameView.getText().toString().trim();
final String alias = aliasView.getText().toString().trim();
final String keyCode = keyCodeView.getText().toString().trim();
final String desc = descView.getText().toString().trim();
ContentValues recipeValues = new ContentValues();
recipeValues.put("NAME", name);
recipeValues.put("ALIAS", alias.length() != 0 ? alias : null);
recipeValues.put("KEY_CODES", keyCode);
recipeValues.put("DESCRIPTION", desc);
String resultId = rxId0;
if (isNew)
// Insert Data
{
// Insert RX_RECIPE 1st
recipeValues.put("BASE_ID", rxId0);
resultId = "" + ZZ.db.insertOrThrow("RX_RECIPE", "", recipeValues);
// Insert MAP_RX_RECIPE_MEDICINE 2nd
insertMapRxMeds(ZZ.db, resultId);
}
else
// Update Data
{
// Update RX_RECIPE 1st
ZZ.db.update("RX_RECIPE", recipeValues, "PK_ID=?", new String[]{rxId0});
// delete old MAP_RX_RECIPE_MEDICINE 2nd
ZZ.db.delete("MAP_RX_RECIPE_MEDICINE", "RX_RECIPE_ID=?", new String[]{rxId0});
// Insert new MAP_RX_RECIPE_MEDICINE last
insertMapRxMeds(ZZ.db, rxId0);
}
ZZ.db.setTransactionSuccessful();
return resultId;
}
finally
{
ZZ.db.endTransaction();
}
}
private void insertMapRxMeds(SQLiteDatabase rwDB, String rxId)
{
int n = medTab.getChildCount()-1;
for (int i=2; i<n; i++) // 0 & 1 are table header & head-line
{
TableRow row = (TableRow) medTab.getChildAt(i);
int medId = -1;
IdentifiedAutoCompleteTextView nameView = (IdentifiedAutoCompleteTextView) row.getChildAt(1);
medId = nameView.getValueId();
float qty = 0.0f;
try
{
qty = Float.parseFloat(((EditText)row.getChildAt(2)).getText().toString().trim());
} catch (Exception e)
{
Log.d(this.getClass().getName(), "", e);
}
boolean isOptional = ((CheckBox) row.getChildAt(3)).isChecked();
ContentValues mapValues = new ContentValues();
mapValues.put("RX_RECIPE_ID", rxId);
mapValues.put("MEDICINE_ID", medId);
mapValues.put("ORDER_NUM", i);
mapValues.put("QUANTITY", qty);
mapValues.put("IS_OPTIONAL", isOptional ? "Y" : null);
rwDB.insertOrThrow("MAP_RX_RECIPE_MEDICINE", "", mapValues);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the home menu XML resource.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.prescription, menu);
return true;
}
}
(5) welcomeRxRecipeQuery
package efan.zz.android.activity;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.util.Linkify;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import efan.zz.android.R;
import efan.zz.android.ZZ;
import efan.zz.android.common.IdentifiedString;
import efan.zz.android.common.android.IdentifiedStringAdapter;
public class WelcomeRxRecipeQuery extends Activity
{
private IdentifiedString selectedRxRecipe;
private IdentifiedStringAdapter selectedRxRecipeAdapter;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
init();
}
private void init()
{
final IdentifiedStringAdapter syndromeAdapter = new IdentifiedStringAdapter(this,
android.R.layout.simple_spinner_item);
String sql = getResources().getString(R.string.SQL_LOAD_SYNDROME_FOR_AC);
syndromeAdapter.loadNameOrKeyAutoCompAdapter(sql);
syndromeAdapter.insert(new IdentifiedString("------", -1), 0);
final IdentifiedStringAdapter allRxRecipeAdapter = new IdentifiedStringAdapter(this,
android.R.layout.simple_dropdown_item_1line);
sql = getResources().getString(R.string.SQL_LOAD_RX_RECIPE_FOR_AC);
allRxRecipeAdapter.preLoadNameOrKeyAutoCompAdapter(sql);
final Spinner syndromeText = (Spinner) findViewById(R.id.syndrome_subject);
final AutoCompleteTextView rxRecipeText = (AutoCompleteTextView) findViewById(R.id.key_code_formula);
final Button queryBtn = (Button) findViewById(R.id.queryButton);
syndromeText.setAdapter(syndromeAdapter);
syndromeText.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
IdentifiedString selectedSyndrome = syndromeAdapter.getItem(position);
if (selectedSyndrome.id == -1)
return;
String sql = getResources().getString(R.string.SQL_LOAD_RX_RECIPE_FOR_AC_BY_SYNDROME);
sql = sql.replace("?", ""+selectedSyndrome.id);
IdentifiedStringAdapter partAdapter = new IdentifiedStringAdapter(WelcomeRxRecipeQuery.this,
android.R.layout.simple_dropdown_item_1line);
partAdapter.loadNameOrKeyAutoCompAdapter(sql);
selectedRxRecipeAdapter = partAdapter;
rxRecipeText.setAdapter(selectedRxRecipeAdapter);
rxRecipeText.setText("");
rxRecipeText.refreshDrawableState();
ZZ.inputMethodMgr.hideSoftInputFromWindow(rxRecipeText.getWindowToken(), 0);
rxRecipeText.showDropDown();
}
public void onNothingSelected(AdapterView<?> parent)
{}
});
rxRecipeText.setAdapter(allRxRecipeAdapter);
selectedRxRecipeAdapter = allRxRecipeAdapter;
rxRecipeText.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@SuppressLint("ResourceAsColor")
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
selectedRxRecipe = selectedRxRecipeAdapter.getItem(position);
RxRecipeDetail.show("" + selectedRxRecipe.id, WelcomeRxRecipeQuery.this);
rxRecipeText.setTextColor(R.color.BlackColor);
}
});
queryBtn.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
selectedRxRecipeAdapter = allRxRecipeAdapter;
rxRecipeText.setAdapter(selectedRxRecipeAdapter);
rxRecipeText.setText("");
rxRecipeText.refreshDrawableState();
ZZ.inputMethodMgr.hideSoftInputFromWindow(rxRecipeText.getWindowToken(), 0);
rxRecipeText.showDropDown();
return;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the home menu XML resource.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.home, menu);
return true;
}
}
(5)video_play
package efan.zz.android.activity;
import android.app.Activity;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.VideoView;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.util.Linkify;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import efan.zz.android.R;
import efan.zz.android.ZZ;
import efan.zz.android.common.IdentifiedString;
import efan.zz.android.common.android.IdentifiedStringAdapter;
public class video_play extends Activity {
Button vedio_play;
private VideoView mp;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(efan.zz.android.R.layout.video_play);
vedio_play=(Button) findViewById(R.id.button3);
mp=(VideoView)findViewById(R.id.video_view);
//mediaController = new MediaController(this);
vedio_play.setOnClickListener(listentvediobutton);
}
private OnClickListener listentvediobutton=new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//mp.reset();
Toast.makeText(getApplicationContext(), "开始播放视频", Toast.LENGTH_SHORT).show();
//加载指定的视频文件
//String path = Environment.getExternalStorageDirectory().getPath()+"/"+R.raw.video_play;
// mp.setVideoPath(path);
//VideoView与MediaController建立关联
//mp.setMediaController(mediaController);
//让VideoView获取焦点
//mp.requestFocus();
mp.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.video_play));
MediaController mediaController=new MediaController(video_play.this);
mp.setMediaController(mediaController);
//mp.requestFocus();
mp.start();
}
};
}
(6) photo_record
package efan.zz.android.activity;
import java.io.IOException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.app.Activity;
import android.app.AliasActivity;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import efan.zz.android.R;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class photo_record extends Activity {
private Button button;
private ImageView showimage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photo_record);
button = (Button) findViewById(R.id.btn_take);
showimage= (ImageView) findViewById(R.id.img_show);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(it, Activity.DEFAULT_KEYS_DIALER);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Activity.DEFAULT_KEYS_DIALER) {
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");
showimage.setImageBitmap(bitmap);
}
}
}
7.3 DBasehelper.java
package efan.zz.android;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DbHelper extends SQLiteOpenHelper
{
private static final String VERSION = "1.00";
private static final int DB_VERSION = (int) (Float.parseFloat(VERSION)*100);
private static final String DB_PATH = "ZZ.db";
private static DbHelper dbHelper;
private Context ctx;
private DbHelper(Context ctx)
{
super(ctx, DB_PATH, null, DB_VERSION); //创建数据库,ZZ文件目录下,名为db
this.ctx = ctx;
}
public static DbHelper getInstance(Context ctx)
{
if (dbHelper == null)
{
dbHelper = new DbHelper(ctx);
}
return dbHelper;
}
@Override
public void onCreate(SQLiteDatabase db)//创建数据库后对数据库的操作
{
try
{
db.beginTransaction();//开启一个事务,判断事务是否成功
// create db ZZ
applySQLs(db, R.raw.db_create);
// Initialise / load db ZZ
applySQLs(db, R.raw.zz_data_subject); //执行raw目录下对应sql文件
applySQLs(db, R.raw.zz_data_medicine);
applySQLs(db, R.raw.zz_data_recipe);
applySQLs(db, R.raw.zz_data_recipe_medicine_map);
db.setTransactionSuccessful();//事务执行成功
}
catch(Exception e)
{
Log.e(this.getClass().getName(), "", e);
throw new RuntimeException("Database create error! Please contact the support or developer.", e);
}
finally
{
db.endTransaction(); //若没有执行Successful 则会回滚事务,此处是为了保证事务的一致性
}
}
private void applySQLs(SQLiteDatabase db, int sqlResourceId) throws IOException //数据库名、文件名
{
InputStream tmpIS = ctx.getResources().openRawResource(sqlResourceId); //获得文件
InputStreamReader tmpReader = new InputStreamReader(tmpIS); //读取文件
BufferedReader tmpBuf = new BufferedReader(tmpReader);
StringBuffer sql = new StringBuffer(); //字符缓冲
String tmpStr = null;
while((tmpStr = tmpBuf.readLine()) != null) //一行行读取
{
sql.append(tmpStr);
sql.append('\n');
if (tmpStr.trim().endsWith(";")) //删除额外的空格
{
db.execSQL(sql.toString()); //执行语句
sql = new StringBuffer();
}
}
tmpBuf.close();
tmpReader.close();
tmpIS.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
try
{
/*** NO upgrade before 1.0 ***
// upgrade db ZZ
applySQLs(db, R.raw.db_upgrade);
*/
db.beginTransaction(); //开启事务
applySQLs(db, R.raw.db_clean);//更新数据库,删除原有的所有表,重新进行建表等
onCreate(db);
db.setTransactionSuccessful();
}
catch(Exception e)
{
Log.e(this.getClass().getName(), "", e);
throw new RuntimeException("Database upgrade error! Please contact the support or developer.", e);
}
finally
{
db.endTransaction();
}
}
}
7.4 Mymusic.java
package efan.zz.android.activity;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import efan.zz.android.R;
import efan.zz.android.ZZ;
import efan.zz.android.common.IdentifiedString;
import efan.zz.android.common.android.IdentifiedStringAdapter;
public class Mymusic extends Service {
private NotificationManager mymn;
private MediaPlayer myplayer;
private MediaPlayer mp;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onCreate(){
super.onCreate();
myplayer=MediaPlayer.create(this,R.raw.music);
//mymn=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//@SuppressWarnings("deprecation")
//Notification notification=new Notification(R.drawable.ic_launcher,"Service start",System.currentTimeMillis());
//PendingIntent contentIntent=PendingIntent.getActivity(this, 0, new Intent(this,MainActivity.class), 0);
//notification.setLatestEventInfo(this, "测试Service ", "歌曲正在播放中", contentIntent);
//mymn.notify(R.string.hello_world,notification);
}
public void onStart(Intent intent,int startID){
//myplayer.start();
super.onStart(intent, startID);
int i=intent.getIntExtra("action", 0);
if(i==1){
if(null==mp){
mp=MediaPlayer.create(this, R.raw.music);
mp.setOnCompletionListener(new OnCompletionListener(){
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
stopSelf();
}
});
}
mp.start();
}else if(i==2){
if(mp!=null&&mp.isPlaying()){
mp.pause();
}
}
Log.v("zhuangtai",String.valueOf(i));
Intent in=new Intent("com.user.action");
in.putExtra("action", i);
sendBroadcast(in);
}
/*public void onDestory(){
myplayer.stop();
super.onDestroy();
}*/
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
//先停止 再释放
if(myplayer.isPlaying()){
mp.stop();
Intent in=new Intent("com.user.action");
in.putExtra("action", 3);
sendBroadcast(in);
}
mp.release();
}
}