(一)基于android平台的智能家居控制系统之 android客户端篇 - suosui

时间:2024-03-06 09:04:29

(一)基于android平台的智能家居控制系统之 android客户端篇

根据项目要求,要写andoid客户端,web服务端程序。下面先来简单说说 android客户端程序(先不说通信方面)

1   本人用的数据库是SQLite.(原因:轻量级,手机内嵌)

     因为本人是学C#的,对调取本地数据库中的数据有着一个几乎定理般的逻辑,就是数据是单向传递的!但是android中的SQLite好像不是这样,它不仅传Entity实体类,而且还要把Context类对象传给DBhelper类对象。这顿时让我很不爽,找了很多资料(度娘),也没有准确的答案,只能硬着头皮先这么写了。

2  先写个小小的Demo,只要把这个做了,其他的就复制粘贴的问题了。(不讨论具体细节,适合于初学者) 

     架构:三层架构(Activity,BLL,DAL(DBHelper,Entity))

     Activity层写了一个Button和一个TextView. 

     业务:(1)点击OK Button,向SQLite传递数据。(2)同时调取SQLite数据,在TextView里面显示。

                                                   

                                                        图1 程序界面

                                      (效果就不贴了,想想业务也能知道了吧)

3 代码

Activity层:界面,新建bll对象并把实体类和Context类对象传给bll对象。

 1 public class MainActivity extends Activity {
 2 
 3     public static String dbpath;
 4     
 5     public static Bll_Test bll_Test;
 6 
 7     @Override
 8     protected void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.activity_main);
11         
12         bll_Test = new Bll_Test(this);
13         
14         Button button = (Button) findViewById(R.id.OK);
15         
16         button.setOnClickListener(new Button.OnClickListener()
17                 {
18                     
19                     
20                     
21                     @Override
22                     public void onClick(View v) {
23                         // TODO Auto-generated method stub
24                         int i = 7;
25                         Test t = new Test();
26                         TextView textView = (TextView)findViewById(R.id.Show);
27                         
28                             t.Set_Value(i);
29                             //t.Set_ID(4);
30                             bll_Test.InsertValue(t);
31                             //textView.setText((bll_Test.GetValue().get(0)).Get_Value());
32                             
33                             textView.setText(Integer.toString(bll_Test.GetValue().get(0).Get_Value()));
34                             //String string = Integer.toString(bll_Test.GetValue().get(0).Get_Value());
35                             //System.out.println(string);
36                         
37                         //textView.setText("Haaaaa");
38                         
39                         
40                     }});
41         
42         
43         
44     }
45 
46     @Override
47     public boolean onCreateOptionsMenu(Menu menu) {
48         // Inflate the menu; this adds items to the action bar if it is present.
49         getMenuInflater().inflate(R.menu.main, menu);
50         return true;
51         
52         
53         
54         
55     }
56 
57     @Override
58     public boolean onOptionsItemSelected(MenuItem item) {
59         // Handle action bar item clicks here. The action bar will
60         // automatically handle clicks on the Home/Up button, so long
61         // as you specify a parent activity in AndroidManifest.xml.
62         int id = item.getItemId();
63         if (id == R.id.action_settings) {
64             return true;
65         }
66         return super.onOptionsItemSelected(item);
67     }
68 }
View Code

BLL层:业务逻辑,新建dal对象,并把逻辑写好,便于activity调用。

 1 public class Bll_Test 
 2 {
 3      private Context context;
 4      private Dal_Test dal_Test;
 5      //private Test test;
 6     public Bll_Test(Context c)
 7     {
 8         this.context = c;
 9         dal_Test = new Dal_Test(this.context);
10     }
11     
12     public  void InsertValue(Test t)
13     {
14          dal_Test.InsertValue(t);
15     }
16     public ArrayList<Test>  GetValue()
17     {
18         return dal_Test.GetValue();
19     }
20 }
View Code

DAl层:具体操作数据库,新建DBHelper类对象,并把sql语句传给该对象。

 1 public class Dal_Test 
 2 {
 3     private Context context;
 4     private SQLiteDatabase db;
 5     public Dal_Test(Context c)
 6     {
 7         try{
 8         this.context = c;
 9         DBHelpr_SQLite dbhelper = new DBHelpr_SQLite(this.context);
10         db = dbhelper.getReadableDatabase();
11         }
12         catch(android.database.sqlite.SQLiteException e)
13         {
14             db.close();
15             throw e;
16         }
17     }
18     public void InsertValue(Test t)
19     {
20          String sql = "INSERT INTO Test(value)"+"VALUES(?)";
21          db.beginTransaction();
22          db.execSQL(sql, new Object[]{t.Get_Value()});
23       db.setTransactionSuccessful();
24       db.endTransaction();
25     }
26     
27     public ArrayList<Test> GetValue()
28     {
29         ArrayList<Test> tests  = new ArrayList<Test>(); 
30         String sql = "SELECT value from Test";
31         Cursor cursor =db.rawQuery(sql,null);
32         while(cursor.moveToNext())
33         {
34             //int id = cursor.getInt(0);
35             int value = cursor.getInt(0);
36             
37             Test test = new Test();
38             //test.Set_ID(id);
39             test.Set_Value(value);
40             tests.add(test);
41         }
42         cursor.close();
43         return tests;
44         
45         
46     }
47 }
View Code

DBHelper类:调取数据库,连接数据库,返回数据。

 1 public class DBHelpr_SQLite extends SQLiteOpenHelper{  
 2     public DBHelpr_SQLite(Context context, String name, CursorFactory factory, int version) {
 3         super(context, name, factory, version);
 4         // TODO Auto-generated constructor        
 5       
 6     }
 7     
 8    //private static Context context;
 9    //private static SQLiteDatabase db;
10    private static final String dbname = "Test.db";
11    private static final int version = 1;
12    
13    public DBHelpr_SQLite(Context c)
14    {
15      this(c,dbname,null,version);
16       
17    } 
18   
19     
20    
21    @Override
22     public void onCreate(SQLiteDatabase db) 
23     {
24         // TODO Auto-generated method stub
25         db.execSQL("CREATE TABLE Test(ID INTEGER PRIMARY KEY AUTOINCREMENT, value INTEGER)");    
26     }
27 
28     @Override
29     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
30         // TODO Auto-generated method stub
31         
32     }  
33   
34 }  
View Code

实体类

 1 public class Test 
 2 {
 3     private int id;
 4     private int value;
 5     public int get_ID() {
 6         
 7         return this.id;
 8     }
 9 
10     
11     public void Set_ID(int i) {
12         this.id = i;
13     }
14 
15     public int Get_Value()
16     {
17         return this.value;
18     }
19         
20     public void Set_Value(int i)
21     {
22         this.value =i;
23     }
24 
25 }
View Code

     难点:存取数据库(像我这种学C#的脑袋就是转不过弯来)

     环境:android17,javaEEIDE(eclipse的一个版本)   

     提示:如果喜欢的话,关注推荐哦!