基于安卓的课程设计——加速度检测应用
压力监测应用
(1) 检测压力是否超过阈值
(2) 若超过压力阈值,则通过一条短信通知联系人,短信内容需包含当前压力。
(3) 可设置联系人名称和联系手机号码
(4) 可设置压力阈值
(5) 记录告警信息到数据库,方便查询
以下开始直接贴代码:
1、MainAcivity
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
package com.example.shiyan;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.util.List;
import com.njupt.db.DBDao;
import com.njupt.pojo.Info;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.sqlite.SQLiteDatabase;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Sensor sensor;
private float x, y, z;//用来记录各个方向上的加速度的值
float xLimit = 5;
float yLimit = 9;//用来记录y方向上的阈值。这里只是用到了y方向上的阈值
float zLimit = 5;
String content;//发短信的内容
String phone = "15951992898";//短信的号码
String smsName = "小明";//联系人名称
String yuzhi;//TextView显示的时候的阈值
SmsManager smsManager;
int smsTime = 5;
private TextView tv_name;
private TextView tv_phone;
private TextView tv_yuzhi;
private Button bt_name;
private Button bt_phone;
private Button bt_yuzhi;
DBDao dao;
public PackageManager pm;
int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
smsManager = SmsManager.getDefault();
// 准备显示信息的UI组建
final TextView tx1 = (TextView) findViewById(R.id.tv);
// 从系统服务中获得传感器管理器
SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
// 在title上显示重力传感器的变化
sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
SensorEventListener lsn = new SensorEventListener() {
public void onSensorChanged(SensorEvent e) {
x = e.values[SensorManager.DATA_X];
y = e.values[SensorManager.DATA_Y];
z = e.values[SensorManager.DATA_Z];
tx1.setText("x=" + (int) x + "," + "y=" + (int) y + "," + "z="
+ (int) z);
checkAccelerometer(getApplicationContext(), x, y, z);
}
public void onAccuracyChanged(Sensor s, int accuracy) {
}
whpx.net/peixunjigou/20140627105127421/show.html?iid=653162
whpx.net/peixunjigou/20140627105127421/show.html?iid=653076
whpx.net/peixunjigou/20140627105127421/show.html?iid=653162
whpx.net/peixunjigou/20140627105127421/show.html?iid=653280
whpx.net/peixunjigou/20140627105127421/show.html?iid=653309
whpx.net/peixunjigou/20140627105127421/show.html?iid=653338
whpx.net/peixunjigou/20140627105127421/show.html?iid=653362
whpx.net/peixunjigou/20140627105127421/show.html?iid=653393
whpx.net/peixunjigou/20140627105127421/show.html?iid=653437
whpx.net/peixunjigou/20140627105127421/show.html?iid=653466
whpx.net/peixunjigou/20140627105127421/show.html?iid=653908
};
// 注册listener,第三个参数是检测的精确度
sm.registerListener(lsn, sensor, SensorManager.SENSOR_DELAY_GAME);
tv_name = (TextView) findViewById(R.id.tv_name);
tv_phone = (TextView) findViewById(R.id.tv_phone);
tv_yuzhi = (TextView) findViewById(R.id.tv_yuzhi);
bt_name = (Button) findViewById(R.id.bt_name);
bt_phone = (Button) findViewById(R.id.bt_phone);
bt_yuzhi = (Button) findViewById(R.id.bt_yuzhi);
dao = new DBDao(getApplicationContext());
pm = getPackageManager();//用来获取包名
}
//设置联系人名称
public void setName(View view) {
Intent i = new Intent(MainActivity.this,SetNameActivity.class);
startActivityForResult(i, 100);
}
//查询历史记录
public void goHistory(View view){
Intent i = new Intent(MainActivity.this,MyListActivity.class);
startActivity(i);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == 10){
String name = data.getExtras().getString("name");
tv_name.setText("联系人名称:" + name);
smsName = name;
}else if(resultCode == 20){
String phonee = data.getExtras().getString("phone");
tv_phone.setText("联系人号码:" + phonee);
phone = phonee;
}else if(resultCode == 30){
int yuzhi1 = data.getExtras().getInt("yuzhi");
tv_yuzhi.setText("阈值:" + yuzhi1);
yLimit = yuzhi1;
}
whpx.net/peixunjigou/20140627105127421/show.html?iid=658216
whpx.net/peixunjigou/20140627105127421/show.html?iid=658271
whpx.net/peixunjigou/20140627105127421/show.html?iid=658337
whpx.net/peixunjigou/20140627105127421/show.html?iid=658374
whpx.net/peixunjigou/20140627105127421/show.html?iid=658486
whpx.net/peixunjigou/20140627105127421/show.html?iid=658529
whpx.net/peixunjigou/20140627105127421/show.html?iid=658571
whpx.net/peixunjigou/20140627105127421/show.html?iid=658620
whpx.net/peixunjigou/20140627105127421/show.html?iid=658657
whpx.net/peixunjigou/20140627105127421/show.html?iid=659571
whpx.net/peixunjigou/20140627105127421/show.html?iid=659873
whpx.net/peixunjigou/20140627105127421/show.html?iid=660153
whpx.net/peixunjigou/20140627105127421/show.html?iid=660223
whpx.net/peixunjigou/20140627105127421/show.html?iid=660427
whpx.net/peixunjigou/20140627105127421/show.html?iid=660770
whpx.net/peixunjigou/20140627105127421/show.html?iid=660824
whpx.net/peixunjigou/20140627105127421/show.html?iid=660866
whpx.net/peixunjigou/20140627105127421/show.html?iid=660919
whpx.net/peixunjigou/20140627105127421/show.html?iid=660961
whpx.net/peixunjigou/20140627105127421/show.html?iid=661010
whpx.net/peixunjigou/20140627105127421/show.html?iid=661074
whpx.net/peixunjigou/20140627105127421/show.html?iid=662357
whpx.net/peixunjigou/20140627105127421/show.html?iid=662422
whpx.net/peixunjigou/20140627105127421/show.html?iid=662466
whpx.net/peixunjigou/20140627105127421/show.html?iid=662533
whpx.net/peixunjigou/20140627105127421/show.html?iid=662592
whpx.net/peixunjigou/20140627105127421/show.html?iid=662688
whpx.net/peixunjigou/20140627105127421/show.html?iid=662710
whpx.net/peixunjigou/20140627105127421/show.html?iid=662737
whpx.net/peixunjigou/20140627105127421/show.html?iid=662761
whpx.net/peixunjigou/20140627105127421/show.html?iid=662790
whpx.net/peixunjigou/20140627105127421/show.html?iid=662954
whpx.net/peixunjigou/20140627105127421/show.html?iid=663043
whpx.net/peixunjigou/20140627105127421/show.html?iid=663285
whpx.net/peixunjigou/20140627105127421/show.html?iid=663344
whpx.net/peixunjigou/20140627105127421/show.html?iid=664082
whpx.net/peixunjigou/20140627105127421/show.html?iid=664125
whpx.net/peixunjigou/20140627105127421/show.html?iid=664160
whpx.net/peixunjigou/20140627105127421/show.html?iid=664197
whpx.net/peixunjigou/20140627105127421/show.html?iid=664229
whpx.net/peixunjigou/20140627105127421/show.html?iid=664273
whpx.net/peixunjigou/20140627105127421/show.html?iid=664310
whpx.net/peixunjigou/20140627105127421/show.html?iid=664344
super.onActivityResult(requestCode, resultCode, data);
}
public void setPhone(View view) {
Intent intent = new Intent(MainActivity.this,SetPhoneActivity.class);
startActivityForResult(intent, 200);
}
public void setYuzhi(View view) {
Intent intent = new Intent(MainActivity.this,SetYuzhiActivity.class);
startActivityForResult(intent, 300);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* 发短信的逻辑
* @param content
*/
public void sendSms(String content) {
// if (smsTime > 0) {
// return;
// }
List<String> texts = smsManager.divideMessage(content);
System.out.println("--------->content: " + content);
System.out.println("phone:" + phone);
for (int i = 0; i < texts.size(); ++i) {
smsManager.sendTextMessage(phone, null, content, null, null);
System.out.println("短信已发送...");
// smsTime++;
}
}
/**
* 检查加速度是否超过了阈值
* @param context
* @param x
* @param y
* @param z
*/
public void checkAccelerometer(Context context, float x, float y, float z) {
if (y > yLimit) {
if(counter % 70 == 0){
Toast.makeText(
context,
"accelerometer已经超过了阈值,这时候的x: " + x + ",y: " + y + ",z: "
+ z, 0).show();
content = smsName + " 先生/女士,accelerometer已经超过了阈值,这时候的x: " + x + ",y: "
+ y + ",z: " + z;
sendSms(content);
Info info = new Info();
info.setName(smsName);
info.setX(x);
info.setY(y);
info.setZ(z);
dao.save(info);
if(counter > 70){
counter = 0;
}
}else{
}
counter++;
}
}
/**
* 将DB文件复制到SD卡中(推荐使用下面一种)
* @param context
* @param oldName
* @param newName
*/
public void copyDBFile(Context context, String oldName, String newName) {
try {
PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
String packageName = info.packageName;
FileReader reader = new FileReader("data/data/" + packageName + "/"
+ "/databases/" + oldName);
System.out.println("Environment.getExternalStorageDirectory(): "
+ Environment.getExternalStorageDirectory());
FileWriter writer = new FileWriter(
Environment.getExternalStorageDirectory() + "/" + newName);
int ch;
while ((ch = reader.read()) != -1) {
writer.write(ch);
}
reader.close();
writer.close();
System.out.println("--------->db文件备份成功...");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 按钮的单击事件
* @param view
*/
public void saveDB(View view) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
// copyDBFile(getApplicationContext(), "njupt.db", "njupt.db");//这种方法不行,不知道为什么..
copyDatabaseFile(getApplicationContext(), false);
}
});
t.start();
Toast.makeText(getApplicationContext(), "DB文件备份成功", 0).show();
}
/**
* 数据库文件的拷贝...
* @param context
* @param isfored
*/
public void copyDatabaseFile(Context context, boolean isfored) {
try {
PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
String packageName = info.packageName;
File dir = new File("data/data/" + packageName + "/"
+ "/databases/" + "njupt.db");
if (!dir.exists() || isfored) {
try {
dir.mkdir();
} catch (Exception e) {
e.printStackTrace();
}
}
File dest = new File(Environment.getExternalStorageDirectory() + "/" + "njupt1.db");
if (dest.exists() && !isfored) {
return;
}
whpx.net/peixunjigou/20140627105127421/show.html?iid=653940
whpx.net/peixunjigou/20140627105127421/show.html?iid=653977
whpx.net/peixunjigou/20140627105127421/show.html?iid=654031
whpx.net/peixunjigou/20140627105127421/show.html?iid=654074
whpx.net/peixunjigou/20140627105127421/show.html?iid=654123
whpx.net/peixunjigou/20140627105127421/show.html?iid=654153
whpx.net/peixunjigou/20140627105127421/show.html?iid=654209
whpx.net/peixunjigou/20140627105127421/show.html?iid=654249
whpx.net/peixunjigou/20140627105127421/show.html?iid=654289
whpx.net/peixunjigou/20140627105127421/show.html?iid=654642
whpx.net/peixunjigou/20140627105127421/show.html?iid=654772
whpx.net/peixunjigou/20140627105127421/show.html?iid=654819
try {
if (dest.exists()) {
dest.delete();
}
dest.createNewFile();
InputStream in = new FileInputStream(dir);
int size = in.available();
byte buf[] = new byte[size];
in.read(buf);
in.close();
FileOutputStream out = new FileOutputStream(dest);
out.write(buf);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
2、MyListActivity
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
package com.njupt.shiyan;
import java.util.List;
import com.example.shiyan.R;
import com.njupt.db.DBDao;
import com.njupt.pojo.Info;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
/**
* 查询历史记录的Activity
* @author Administrator
*
*/
public class MyListActivity extends Activity {
private ListView infoLV;
List<Info> infos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
infoLV = (ListView) findViewById(R.id.infoLV);
DBDao dao = new DBDao(this);
infos = dao.query();
infoLV.setAdapter(new MyAdapter());
}
public class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return infos.size();
}
whpx.net/peixunjigou/20140627105127421/show.html?iid=654865
whpx.net/peixunjigou/20140627105127421/show.html?iid=654900
whpx.net/peixunjigou/20140627105127421/show.html?iid=654942
whpx.net/peixunjigou/20140627105127421/show.html?iid=655076
whpx.net/peixunjigou/20140627105127421/show.html?iid=655681
whpx.net/peixunjigou/20140627105127421/show.html?iid=655740
whpx.net/peixunjigou/20140627105127421/show.html?iid=655786
whpx.net/peixunjigou/20140627105127421/show.html?iid=656071
whpx.net/peixunjigou/20140627105127421/show.html?iid=656122
whpx.net/peixunjigou/20140627105127421/show.html?iid=656158
whpx.net/peixunjigou/20140627105127421/show.html?iid=657260
whpx.net/peixunjigou/20140627105127421/show.html?iid=657297
whpx.net/peixunjigou/20140627105127421/show.html?iid=657320
whpx.net/peixunjigou/20140627105127421/show.html?iid=657388
whpx.net/peixunjigou/20140627105127421/show.html?iid=657566
whpx.net/peixunjigou/20140627105127421/show.html?iid=657625
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return infos.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = View.inflate(getApplicationContext(), R.layout.item,
null);// 根据布局文件创建View(LinearLayout)
TextView idTV = (TextView) item.findViewById(R.id.idTV);// 获取这个新生成的View中的TextView
TextView nameTV = (TextView) item.findViewById(R.id.nameTV);
TextView xTV = (TextView) item.findViewById(R.id.xTV);
TextView yTV = (TextView) item.findViewById(R.id.yTV);
TextView zTV = (TextView) item.findViewById(R.id.zTV);
Info info = infos.get(position);// 根据位置获取Person对象
idTV.setText(info.get_id() + "");// 给TextView设置文本
nameTV.setText(info.getName());
xTV.setText(info.getX() + "");
yTV.setText(info.getY() + "");
zTV.setText(info.getZ() + "");
return item;
}
}
}
3、SetNameActivity
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
package com.njupt.shiyan;
import com.example.shiyan.R;
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.widget.EditText;
import android.widget.Toast;
public class SetNameActivity extends Activity {
private EditText et_name;
private Button bt_name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setname);
et_name = (EditText) findViewById(R.id.et_name);
bt_name = (Button) findViewById(R.id.bt_name);
bt_name.setOnClickListener(new MyOnClickListener());
}
class MyOnClickListener implements OnClickListener{
@Override
public void onClick(View v) {
String name = et_name.getText().toString();
Intent intent = new Intent();
intent.putExtra("name", name);
setResult(10, intent);
Toast.makeText(getApplicationContext(), "设置联系人名称成功..", 0).show();
finish();
}
}
}
4、SetPhoneActivity
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
package com.njupt.shiyan;
import com.example.shiyan.R;
import com.njupt.utils.Utils;
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.widget.EditText;
import android.widget.Toast;
public class SetPhoneActivity extends Activity {
private EditText et_phone;
private Button bt_phone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setphone);
et_phone = (EditText) findViewById(R.id.et_phone);
bt_phone = (Button) findViewById(R.id.bt_phone);
bt_phone.setOnClickListener(new MyOnClickListener1());
}
class MyOnClickListener1 implements OnClickListener{
@Override
public void onClick(View v) {
String phone = et_phone.getText().toString();
if(Utils.checkStringIsNumber(getApplicationContext(), phone) == false){
return ;
}
Intent intent = new Intent();
intent.putExtra("phone", phone);
setResult(20, intent);
Toast.makeText(getApplicationContext(), "设置联系人号码成功..", 0).show();
finish();
}
}
}
5、SetYuzhiActivity
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
package com.njupt.shiyan;
import com.example.shiyan.R;
import com.njupt.utils.Utils;
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.widget.EditText;
import android.widget.Toast;
public class SetYuzhiActivity extends Activity {
private EditText et_yuzhi;
private Button bt_yuzhi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setyuzhi);
et_yuzhi = (EditText) findViewById(R.id.et_yuzhi);
bt_yuzhi = (Button) findViewById(R.id.bt_yuzhi);
bt_yuzhi.setOnClickListener(new MyOnClickListener2());
}
class MyOnClickListener2 implements OnClickListener{
@Override
public void onClick(View v) {
String yuzhi = et_yuzhi.getText().toString();
if(Utils.checkStringIsNumber(getApplicationContext(), yuzhi) == false){
return ;
}
int yuzhi1 = Integer.parseInt(yuzhi);
Intent intent = new Intent();
intent.putExtra("yuzhi", yuzhi1);
setResult(30, intent);
Toast.makeText(getApplicationContext(), "设置阈值成功..", 0).show();
finish();
}
}
}
6、SplashActivity
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
package com.njupt.shiyan;
import com.example.shiyan.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
public class SplashActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// 取消状态栏
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
ImageView logoImage = (ImageView) findViewById(R.id.iv_logo);
AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
alphaAnimation.setDuration(3000);
logoImage.startAnimation(alphaAnimation);
alphaAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
Intent intent = new Intent();
intent.setClass(SplashActivity.this,SplashActivity1.class);
startActivity(intent);
finish();
}
});
}
}
7、SplashActivity1
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
package com.njupt.shiyan;
import com.example.shiyan.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
public class SplashActivity1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// 取消状态栏
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash1);
ImageView logoImage = (ImageView) findViewById(R.id.iv_logo);
AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
alphaAnimation.setDuration(3000);
logoImage.startAnimation(alphaAnimation);
alphaAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
Intent intent = new Intent();
intent.setClass(SplashActivity1.this, MainActivity.class);
startActivity(intent);
finish();
}
});
}
}
8、DBDao
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
package com.njupt.db;
import java.util.ArrayList;
import java.util.List;
import com.njupt.pojo.Info;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class DBDao {
DBOpenHelper dbOpenHelper;
SQLiteDatabase db;
public DBDao(Context context){
this.dbOpenHelper=new DBOpenHelper(context);
db = dbOpenHelper.getWritableDatabase();
}
/**
* 添加一条数据
* @param info
*/
public void save(Info info){
SQLiteDatabase db=dbOpenHelper.getWritableDatabase();
System.out.println("----->save: " + info.getX() + ", "+info.getY() + ","+info.getZ());
db.execSQL("insert into info(name,x,y,z) values(?,?,?,?)", new Object[]{info.getName(),info.getX(),info.getY(),info.getZ()});
System.out.println("------------>保存成功.x: " + info.getX() + ",y: " + info.getY());
db.close();
}
public List<Info> query() {
ArrayList<Info> infos = new ArrayList<Info>();
Cursor c = queryTheCursor();
while (c.moveToNext()) {
Info info = new Info();
info._id = c.getInt(c.getColumnIndex("_id"));
info.name = c.getString(c.getColumnIndex("name"));
info.x = c.getFloat(c.getColumnIndex("x"));
info.y = c.getFloat(c.getColumnIndex("y"));
info.z = c.getFloat(c.getColumnIndex("z"));
infos.add(info);
}
c.close();
db.close();
return infos;
}
/**
* query all persons, return cursor
* @return Cursor
*/
public Cursor queryTheCursor() {
Cursor c = db.rawQuery("SELECT * FROM info", null);
return c;
}
}
9、DBOpenHelper
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
package com.njupt.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBOpenHelper extends SQLiteOpenHelper {
public DBOpenHelper(Context context) {
super(context, "njupt.db", null, 1);
}
//数据库第一次创建时候调用,
public void onCreate(SQLiteDatabase db) {
System.out.println("---------->创建了一张表...");
db.execSQL("create table info(_id INTEGER PRIMARY KEY AUTOINCREMENT,name varchar(20),x float(10), y float(10),z float(10))");
}
//数据库文件版本号发生变化时调用
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
}
}
10、Info
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
package com.njupt.pojo;
/**
* 用来记录一条异常信息
* @author 黄俊东大神
*
*/
public class Info {
public int _id;
public String name;//发生异常信息时,短信是给谁发的
public float x;//x方向上的加速度
public float y;//y轴方向上的加速度
public float z;//z轴方向上的加速度
public int get_id() {
return _id;
}
public void set_id(int _id) {
this._id = _id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public float getZ() {
return z;
}
public void setZ(float z) {
this.z = z;
}
}
11、Uitls
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
package com.njupt.utils;
import android.content.Context;
import android.widget.Toast;
public class Utils {
/**
* 判断一个函数是否是一个数字
* @param context
* @param str
* @return
*/
public static boolean checkStringIsNumber(Context context,String str){
int size = str.length();
if (size <= 0) {
Toast.makeText(context, "所输入的内容不能为空", 0).show();
return false;
}
for (int i = 0; i < size; ++i) {
if (Character.isDigit(str.charAt(i)) == false) {
Toast.makeText(context, "所输入的内容必须是一个数字..", 1).show();
return false;
}
}
return true;
}
}
12、DBTest
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
package com.njupt.test;
import java.util.List;
import com.njupt.db.DBDao;
import com.njupt.db.DBOpenHelper;
import com.njupt.pojo.Info;
import android.database.sqlite.SQLiteDatabase;
import android.test.AndroidTestCase;
public class DBTest extends AndroidTestCase{
public void testCreateDatabase() {
DBOpenHelper helper = new DBOpenHelper(getContext());
helper.getWritableDatabase();
/*
* 获取可写的数据连接
* 数据库文件不存在时, 会创建数据库文件, 并且执行onCreate()方法
* 数据库文件存在, 并且版本没有改变时, 不执行任何方法
* 数据库文件存在, 版本提升, 执行onUpgrade()方法
*/
}
public void testQuery(){
DBDao dao = new DBDao(getContext());
List<Info> infos = dao.query();
for(Info info : infos){
System.out.println("_id:" +info.get_id() + ",name: " + info.getName() + ",x: " + info.getX() + ",y: " + info.getY() + ",z: " + info.getZ());
}
}
}
压力监测应用
(1) 检测压力是否超过阈值
(2) 若超过压力阈值,则通过一条短信通知联系人,短信内容需包含当前压力。
(3) 可设置联系人名称和联系手机号码
(4) 可设置压力阈值
(5) 记录告警信息到数据库,方便查询
以下开始直接贴代码:
1、MainAcivity
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
package com.example.shiyan;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.util.List;
import com.njupt.db.DBDao;
import com.njupt.pojo.Info;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.sqlite.SQLiteDatabase;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Sensor sensor;
private float x, y, z;//用来记录各个方向上的加速度的值
float xLimit = 5;
float yLimit = 9;//用来记录y方向上的阈值。这里只是用到了y方向上的阈值
float zLimit = 5;
String content;//发短信的内容
String phone = "15951992898";//短信的号码
String smsName = "小明";//联系人名称
String yuzhi;//TextView显示的时候的阈值
SmsManager smsManager;
int smsTime = 5;
private TextView tv_name;
private TextView tv_phone;
private TextView tv_yuzhi;
private Button bt_name;
private Button bt_phone;
private Button bt_yuzhi;
DBDao dao;
public PackageManager pm;
int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
smsManager = SmsManager.getDefault();
// 准备显示信息的UI组建
final TextView tx1 = (TextView) findViewById(R.id.tv);
// 从系统服务中获得传感器管理器
SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
// 在title上显示重力传感器的变化
sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
SensorEventListener lsn = new SensorEventListener() {
public void onSensorChanged(SensorEvent e) {
x = e.values[SensorManager.DATA_X];
y = e.values[SensorManager.DATA_Y];
z = e.values[SensorManager.DATA_Z];
tx1.setText("x=" + (int) x + "," + "y=" + (int) y + "," + "z="
+ (int) z);
checkAccelerometer(getApplicationContext(), x, y, z);
}
public void onAccuracyChanged(Sensor s, int accuracy) {
}
whpx.net/peixunjigou/20140627105127421/show.html?iid=653162
whpx.net/peixunjigou/20140627105127421/show.html?iid=653076
whpx.net/peixunjigou/20140627105127421/show.html?iid=653162
whpx.net/peixunjigou/20140627105127421/show.html?iid=653280
whpx.net/peixunjigou/20140627105127421/show.html?iid=653309
whpx.net/peixunjigou/20140627105127421/show.html?iid=653338
whpx.net/peixunjigou/20140627105127421/show.html?iid=653362
whpx.net/peixunjigou/20140627105127421/show.html?iid=653393
whpx.net/peixunjigou/20140627105127421/show.html?iid=653437
whpx.net/peixunjigou/20140627105127421/show.html?iid=653466
whpx.net/peixunjigou/20140627105127421/show.html?iid=653908
};
// 注册listener,第三个参数是检测的精确度
sm.registerListener(lsn, sensor, SensorManager.SENSOR_DELAY_GAME);
tv_name = (TextView) findViewById(R.id.tv_name);
tv_phone = (TextView) findViewById(R.id.tv_phone);
tv_yuzhi = (TextView) findViewById(R.id.tv_yuzhi);
bt_name = (Button) findViewById(R.id.bt_name);
bt_phone = (Button) findViewById(R.id.bt_phone);
bt_yuzhi = (Button) findViewById(R.id.bt_yuzhi);
dao = new DBDao(getApplicationContext());
pm = getPackageManager();//用来获取包名
}
//设置联系人名称
public void setName(View view) {
Intent i = new Intent(MainActivity.this,SetNameActivity.class);
startActivityForResult(i, 100);
}
//查询历史记录
public void goHistory(View view){
Intent i = new Intent(MainActivity.this,MyListActivity.class);
startActivity(i);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == 10){
String name = data.getExtras().getString("name");
tv_name.setText("联系人名称:" + name);
smsName = name;
}else if(resultCode == 20){
String phonee = data.getExtras().getString("phone");
tv_phone.setText("联系人号码:" + phonee);
phone = phonee;
}else if(resultCode == 30){
int yuzhi1 = data.getExtras().getInt("yuzhi");
tv_yuzhi.setText("阈值:" + yuzhi1);
yLimit = yuzhi1;
}
whpx.net/peixunjigou/20140627105127421/show.html?iid=658216
whpx.net/peixunjigou/20140627105127421/show.html?iid=658271
whpx.net/peixunjigou/20140627105127421/show.html?iid=658337
whpx.net/peixunjigou/20140627105127421/show.html?iid=658374
whpx.net/peixunjigou/20140627105127421/show.html?iid=658486
whpx.net/peixunjigou/20140627105127421/show.html?iid=658529
whpx.net/peixunjigou/20140627105127421/show.html?iid=658571
whpx.net/peixunjigou/20140627105127421/show.html?iid=658620
whpx.net/peixunjigou/20140627105127421/show.html?iid=658657
whpx.net/peixunjigou/20140627105127421/show.html?iid=659571
whpx.net/peixunjigou/20140627105127421/show.html?iid=659873
whpx.net/peixunjigou/20140627105127421/show.html?iid=660153
whpx.net/peixunjigou/20140627105127421/show.html?iid=660223
whpx.net/peixunjigou/20140627105127421/show.html?iid=660427
whpx.net/peixunjigou/20140627105127421/show.html?iid=660770
whpx.net/peixunjigou/20140627105127421/show.html?iid=660824
whpx.net/peixunjigou/20140627105127421/show.html?iid=660866
whpx.net/peixunjigou/20140627105127421/show.html?iid=660919
whpx.net/peixunjigou/20140627105127421/show.html?iid=660961
whpx.net/peixunjigou/20140627105127421/show.html?iid=661010
whpx.net/peixunjigou/20140627105127421/show.html?iid=661074
whpx.net/peixunjigou/20140627105127421/show.html?iid=662357
whpx.net/peixunjigou/20140627105127421/show.html?iid=662422
whpx.net/peixunjigou/20140627105127421/show.html?iid=662466
whpx.net/peixunjigou/20140627105127421/show.html?iid=662533
whpx.net/peixunjigou/20140627105127421/show.html?iid=662592
whpx.net/peixunjigou/20140627105127421/show.html?iid=662688
whpx.net/peixunjigou/20140627105127421/show.html?iid=662710
whpx.net/peixunjigou/20140627105127421/show.html?iid=662737
whpx.net/peixunjigou/20140627105127421/show.html?iid=662761
whpx.net/peixunjigou/20140627105127421/show.html?iid=662790
whpx.net/peixunjigou/20140627105127421/show.html?iid=662954
whpx.net/peixunjigou/20140627105127421/show.html?iid=663043
whpx.net/peixunjigou/20140627105127421/show.html?iid=663285
whpx.net/peixunjigou/20140627105127421/show.html?iid=663344
whpx.net/peixunjigou/20140627105127421/show.html?iid=664082
whpx.net/peixunjigou/20140627105127421/show.html?iid=664125
whpx.net/peixunjigou/20140627105127421/show.html?iid=664160
whpx.net/peixunjigou/20140627105127421/show.html?iid=664197
whpx.net/peixunjigou/20140627105127421/show.html?iid=664229
whpx.net/peixunjigou/20140627105127421/show.html?iid=664273
whpx.net/peixunjigou/20140627105127421/show.html?iid=664310
whpx.net/peixunjigou/20140627105127421/show.html?iid=664344
super.onActivityResult(requestCode, resultCode, data);
}
public void setPhone(View view) {
Intent intent = new Intent(MainActivity.this,SetPhoneActivity.class);
startActivityForResult(intent, 200);
}
public void setYuzhi(View view) {
Intent intent = new Intent(MainActivity.this,SetYuzhiActivity.class);
startActivityForResult(intent, 300);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* 发短信的逻辑
* @param content
*/
public void sendSms(String content) {
// if (smsTime > 0) {
// return;
// }
List<String> texts = smsManager.divideMessage(content);
System.out.println("--------->content: " + content);
System.out.println("phone:" + phone);
for (int i = 0; i < texts.size(); ++i) {
smsManager.sendTextMessage(phone, null, content, null, null);
System.out.println("短信已发送...");
// smsTime++;
}
}
/**
* 检查加速度是否超过了阈值
* @param context
* @param x
* @param y
* @param z
*/
public void checkAccelerometer(Context context, float x, float y, float z) {
if (y > yLimit) {
if(counter % 70 == 0){
Toast.makeText(
context,
"accelerometer已经超过了阈值,这时候的x: " + x + ",y: " + y + ",z: "
+ z, 0).show();
content = smsName + " 先生/女士,accelerometer已经超过了阈值,这时候的x: " + x + ",y: "
+ y + ",z: " + z;
sendSms(content);
Info info = new Info();
info.setName(smsName);
info.setX(x);
info.setY(y);
info.setZ(z);
dao.save(info);
if(counter > 70){
counter = 0;
}
}else{
}
counter++;
}
}
/**
* 将DB文件复制到SD卡中(推荐使用下面一种)
* @param context
* @param oldName
* @param newName
*/
public void copyDBFile(Context context, String oldName, String newName) {
try {
PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
String packageName = info.packageName;
FileReader reader = new FileReader("data/data/" + packageName + "/"
+ "/databases/" + oldName);
System.out.println("Environment.getExternalStorageDirectory(): "
+ Environment.getExternalStorageDirectory());
FileWriter writer = new FileWriter(
Environment.getExternalStorageDirectory() + "/" + newName);
int ch;
while ((ch = reader.read()) != -1) {
writer.write(ch);
}
reader.close();
writer.close();
System.out.println("--------->db文件备份成功...");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 按钮的单击事件
* @param view
*/
public void saveDB(View view) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
// copyDBFile(getApplicationContext(), "njupt.db", "njupt.db");//这种方法不行,不知道为什么..
copyDatabaseFile(getApplicationContext(), false);
}
});
t.start();
Toast.makeText(getApplicationContext(), "DB文件备份成功", 0).show();
}
/**
* 数据库文件的拷贝...
* @param context
* @param isfored
*/
public void copyDatabaseFile(Context context, boolean isfored) {
try {
PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
String packageName = info.packageName;
File dir = new File("data/data/" + packageName + "/"
+ "/databases/" + "njupt.db");
if (!dir.exists() || isfored) {
try {
dir.mkdir();
} catch (Exception e) {
e.printStackTrace();
}
}
File dest = new File(Environment.getExternalStorageDirectory() + "/" + "njupt1.db");
if (dest.exists() && !isfored) {
return;
}
whpx.net/peixunjigou/20140627105127421/show.html?iid=653940
whpx.net/peixunjigou/20140627105127421/show.html?iid=653977
whpx.net/peixunjigou/20140627105127421/show.html?iid=654031
whpx.net/peixunjigou/20140627105127421/show.html?iid=654074
whpx.net/peixunjigou/20140627105127421/show.html?iid=654123
whpx.net/peixunjigou/20140627105127421/show.html?iid=654153
whpx.net/peixunjigou/20140627105127421/show.html?iid=654209
whpx.net/peixunjigou/20140627105127421/show.html?iid=654249
whpx.net/peixunjigou/20140627105127421/show.html?iid=654289
whpx.net/peixunjigou/20140627105127421/show.html?iid=654642
whpx.net/peixunjigou/20140627105127421/show.html?iid=654772
whpx.net/peixunjigou/20140627105127421/show.html?iid=654819
try {
if (dest.exists()) {
dest.delete();
}
dest.createNewFile();
InputStream in = new FileInputStream(dir);
int size = in.available();
byte buf[] = new byte[size];
in.read(buf);
in.close();
FileOutputStream out = new FileOutputStream(dest);
out.write(buf);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
2、MyListActivity
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
package com.njupt.shiyan;
import java.util.List;
import com.example.shiyan.R;
import com.njupt.db.DBDao;
import com.njupt.pojo.Info;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
/**
* 查询历史记录的Activity
* @author Administrator
*
*/
public class MyListActivity extends Activity {
private ListView infoLV;
List<Info> infos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
infoLV = (ListView) findViewById(R.id.infoLV);
DBDao dao = new DBDao(this);
infos = dao.query();
infoLV.setAdapter(new MyAdapter());
}
public class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return infos.size();
}
whpx.net/peixunjigou/20140627105127421/show.html?iid=654865
whpx.net/peixunjigou/20140627105127421/show.html?iid=654900
whpx.net/peixunjigou/20140627105127421/show.html?iid=654942
whpx.net/peixunjigou/20140627105127421/show.html?iid=655076
whpx.net/peixunjigou/20140627105127421/show.html?iid=655681
whpx.net/peixunjigou/20140627105127421/show.html?iid=655740
whpx.net/peixunjigou/20140627105127421/show.html?iid=655786
whpx.net/peixunjigou/20140627105127421/show.html?iid=656071
whpx.net/peixunjigou/20140627105127421/show.html?iid=656122
whpx.net/peixunjigou/20140627105127421/show.html?iid=656158
whpx.net/peixunjigou/20140627105127421/show.html?iid=657260
whpx.net/peixunjigou/20140627105127421/show.html?iid=657297
whpx.net/peixunjigou/20140627105127421/show.html?iid=657320
whpx.net/peixunjigou/20140627105127421/show.html?iid=657388
whpx.net/peixunjigou/20140627105127421/show.html?iid=657566
whpx.net/peixunjigou/20140627105127421/show.html?iid=657625
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return infos.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = View.inflate(getApplicationContext(), R.layout.item,
null);// 根据布局文件创建View(LinearLayout)
TextView idTV = (TextView) item.findViewById(R.id.idTV);// 获取这个新生成的View中的TextView
TextView nameTV = (TextView) item.findViewById(R.id.nameTV);
TextView xTV = (TextView) item.findViewById(R.id.xTV);
TextView yTV = (TextView) item.findViewById(R.id.yTV);
TextView zTV = (TextView) item.findViewById(R.id.zTV);
Info info = infos.get(position);// 根据位置获取Person对象
idTV.setText(info.get_id() + "");// 给TextView设置文本
nameTV.setText(info.getName());
xTV.setText(info.getX() + "");
yTV.setText(info.getY() + "");
zTV.setText(info.getZ() + "");
return item;
}
}
}
3、SetNameActivity
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
package com.njupt.shiyan;
import com.example.shiyan.R;
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.widget.EditText;
import android.widget.Toast;
public class SetNameActivity extends Activity {
private EditText et_name;
private Button bt_name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setname);
et_name = (EditText) findViewById(R.id.et_name);
bt_name = (Button) findViewById(R.id.bt_name);
bt_name.setOnClickListener(new MyOnClickListener());
}
class MyOnClickListener implements OnClickListener{
@Override
public void onClick(View v) {
String name = et_name.getText().toString();
Intent intent = new Intent();
intent.putExtra("name", name);
setResult(10, intent);
Toast.makeText(getApplicationContext(), "设置联系人名称成功..", 0).show();
finish();
}
}
}
4、SetPhoneActivity
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
package com.njupt.shiyan;
import com.example.shiyan.R;
import com.njupt.utils.Utils;
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.widget.EditText;
import android.widget.Toast;
public class SetPhoneActivity extends Activity {
private EditText et_phone;
private Button bt_phone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setphone);
et_phone = (EditText) findViewById(R.id.et_phone);
bt_phone = (Button) findViewById(R.id.bt_phone);
bt_phone.setOnClickListener(new MyOnClickListener1());
}
class MyOnClickListener1 implements OnClickListener{
@Override
public void onClick(View v) {
String phone = et_phone.getText().toString();
if(Utils.checkStringIsNumber(getApplicationContext(), phone) == false){
return ;
}
Intent intent = new Intent();
intent.putExtra("phone", phone);
setResult(20, intent);
Toast.makeText(getApplicationContext(), "设置联系人号码成功..", 0).show();
finish();
}
}
}
5、SetYuzhiActivity
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
package com.njupt.shiyan;
import com.example.shiyan.R;
import com.njupt.utils.Utils;
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.widget.EditText;
import android.widget.Toast;
public class SetYuzhiActivity extends Activity {
private EditText et_yuzhi;
private Button bt_yuzhi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setyuzhi);
et_yuzhi = (EditText) findViewById(R.id.et_yuzhi);
bt_yuzhi = (Button) findViewById(R.id.bt_yuzhi);
bt_yuzhi.setOnClickListener(new MyOnClickListener2());
}
class MyOnClickListener2 implements OnClickListener{
@Override
public void onClick(View v) {
String yuzhi = et_yuzhi.getText().toString();
if(Utils.checkStringIsNumber(getApplicationContext(), yuzhi) == false){
return ;
}
int yuzhi1 = Integer.parseInt(yuzhi);
Intent intent = new Intent();
intent.putExtra("yuzhi", yuzhi1);
setResult(30, intent);
Toast.makeText(getApplicationContext(), "设置阈值成功..", 0).show();
finish();
}
}
}
6、SplashActivity
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
package com.njupt.shiyan;
import com.example.shiyan.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
public class SplashActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// 取消状态栏
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
ImageView logoImage = (ImageView) findViewById(R.id.iv_logo);
AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
alphaAnimation.setDuration(3000);
logoImage.startAnimation(alphaAnimation);
alphaAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
Intent intent = new Intent();
intent.setClass(SplashActivity.this,SplashActivity1.class);
startActivity(intent);
finish();
}
});
}
}
7、SplashActivity1
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
package com.njupt.shiyan;
import com.example.shiyan.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
public class SplashActivity1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// 取消状态栏
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash1);
ImageView logoImage = (ImageView) findViewById(R.id.iv_logo);
AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
alphaAnimation.setDuration(3000);
logoImage.startAnimation(alphaAnimation);
alphaAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
Intent intent = new Intent();
intent.setClass(SplashActivity1.this, MainActivity.class);
startActivity(intent);
finish();
}
});
}
}
8、DBDao
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
package com.njupt.db;
import java.util.ArrayList;
import java.util.List;
import com.njupt.pojo.Info;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class DBDao {
DBOpenHelper dbOpenHelper;
SQLiteDatabase db;
public DBDao(Context context){
this.dbOpenHelper=new DBOpenHelper(context);
db = dbOpenHelper.getWritableDatabase();
}
/**
* 添加一条数据
* @param info
*/
public void save(Info info){
SQLiteDatabase db=dbOpenHelper.getWritableDatabase();
System.out.println("----->save: " + info.getX() + ", "+info.getY() + ","+info.getZ());
db.execSQL("insert into info(name,x,y,z) values(?,?,?,?)", new Object[]{info.getName(),info.getX(),info.getY(),info.getZ()});
System.out.println("------------>保存成功.x: " + info.getX() + ",y: " + info.getY());
db.close();
}
public List<Info> query() {
ArrayList<Info> infos = new ArrayList<Info>();
Cursor c = queryTheCursor();
while (c.moveToNext()) {
Info info = new Info();
info._id = c.getInt(c.getColumnIndex("_id"));
info.name = c.getString(c.getColumnIndex("name"));
info.x = c.getFloat(c.getColumnIndex("x"));
info.y = c.getFloat(c.getColumnIndex("y"));
info.z = c.getFloat(c.getColumnIndex("z"));
infos.add(info);
}
c.close();
db.close();
return infos;
}
/**
* query all persons, return cursor
* @return Cursor
*/
public Cursor queryTheCursor() {
Cursor c = db.rawQuery("SELECT * FROM info", null);
return c;
}
}
9、DBOpenHelper
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
package com.njupt.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBOpenHelper extends SQLiteOpenHelper {
public DBOpenHelper(Context context) {
super(context, "njupt.db", null, 1);
}
//数据库第一次创建时候调用,
public void onCreate(SQLiteDatabase db) {
System.out.println("---------->创建了一张表...");
db.execSQL("create table info(_id INTEGER PRIMARY KEY AUTOINCREMENT,name varchar(20),x float(10), y float(10),z float(10))");
}
//数据库文件版本号发生变化时调用
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
}
}
10、Info
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
package com.njupt.pojo;
/**
* 用来记录一条异常信息
* @author 黄俊东大神
*
*/
public class Info {
public int _id;
public String name;//发生异常信息时,短信是给谁发的
public float x;//x方向上的加速度
public float y;//y轴方向上的加速度
public float z;//z轴方向上的加速度
public int get_id() {
return _id;
}
public void set_id(int _id) {
this._id = _id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public float getZ() {
return z;
}
public void setZ(float z) {
this.z = z;
}
}
11、Uitls
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
package com.njupt.utils;
import android.content.Context;
import android.widget.Toast;
public class Utils {
/**
* 判断一个函数是否是一个数字
* @param context
* @param str
* @return
*/
public static boolean checkStringIsNumber(Context context,String str){
int size = str.length();
if (size <= 0) {
Toast.makeText(context, "所输入的内容不能为空", 0).show();
return false;
}
for (int i = 0; i < size; ++i) {
if (Character.isDigit(str.charAt(i)) == false) {
Toast.makeText(context, "所输入的内容必须是一个数字..", 1).show();
return false;
}
}
return true;
}
}
12、DBTest
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
package com.njupt.test;
import java.util.List;
import com.njupt.db.DBDao;
import com.njupt.db.DBOpenHelper;
import com.njupt.pojo.Info;
import android.database.sqlite.SQLiteDatabase;
import android.test.AndroidTestCase;
public class DBTest extends AndroidTestCase{
public void testCreateDatabase() {
DBOpenHelper helper = new DBOpenHelper(getContext());
helper.getWritableDatabase();
/*
* 获取可写的数据连接
* 数据库文件不存在时, 会创建数据库文件, 并且执行onCreate()方法
* 数据库文件存在, 并且版本没有改变时, 不执行任何方法
* 数据库文件存在, 版本提升, 执行onUpgrade()方法
*/
}
public void testQuery(){
DBDao dao = new DBDao(getContext());
List<Info> infos = dao.query();
for(Info info : infos){
System.out.println("_id:" +info.get_id() + ",name: " + info.getName() + ",x: " + info.getX() + ",y: " + info.getY() + ",z: " + info.getZ());
}
}
}