第一个Android小应用MagicTablet ---基于Android APIDemo GestureBuilder 开发的手势应用

  MagicTablet是我的第一个android应用程序,也是自己这几周来学习android基础的一个总结。总体上来讲,MagicTablet的实用性不是很强,算是一个练手的作品。写这篇博客目的,是为了更好地梳理知识,当然如果你有好的建议和不同的见解可以加本人QQ:386363026 和本人的Android交流群 219228300。

 

  一:MagicTablet的框架结构

 

  二:相应的代码

  1: MagicTabletActivity.java

View Code
package persen.android;

import java.io.File;
import java.util.ArrayList;

import persen.android.R.raw;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.SyncStateContract.Constants;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;


//主界面
public class MagicTabletActivity extends Activity implements OnGesturePerformedListener{
/** Called when the activity is first created. */
GestureLibrary library;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//加载手势库
File mStoreFile = new File(Environment.getExternalStorageDirectory(), "gestures");
library = GestureLibraries.fromFile(mStoreFile);
library.load();
GestureOverlayView gestures = (GestureOverlayView)findViewById(R.id.gestureOverlayView1);
//添加手势监听函数
gestures.addOnGesturePerformedListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.defaultmenu, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
int choose = item.getItemId();
switch(choose){
case R.id.itemCreateGesture: Intent createGestureIntent = new Intent(this,
GestureBuilderActivity.class);
startActivity(createGestureIntent);
break;
case R.id.itemMore: Intent moreIntent = new Intent(MagicTabletActivity.this,MoreListView.class);
startActivity(moreIntent);
break;
case R.id.itemSetting: Intent settingIntent = new Intent(MagicTabletActivity.this,SettingListView.class);
startActivity(settingIntent);
break;
}
return super.onMenuItemSelected(featureId, item);
}

public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
// TODO Auto-generated method stub
//判断手势是否在library中
ArrayList<Prediction> predictions = library.recognize(gesture);
if(!predictions.isEmpty()){
Prediction prediction = predictions.get(0);

//获取数据库中输入手势的ID号和号码(可空)
MySQLiteOpenHelper help = new MySQLiteOpenHelper(this,"MagicTablet.db",null,1);
SQLiteDatabase db =help.getReadableDatabase();

String gestureName = prediction.name;

Cursor cursor = db.rawQuery("select GestureId , PhoneNumber from Gestures where GestureName='"+gestureName+"';",
new String[]{});
int idColumn = cursor.getColumnIndex("GestureId");
int phoneColnum = cursor.getColumnIndex("PhoneNumber");
cursor.moveToFirst();
int gestureId = cursor.getInt(idColumn);
String gesturePhone = cursor.getString(phoneColnum);
db.close();
Toast.makeText(this, "正在识别中...", 500).show();
if(prediction.score>4){

switch(gestureId){
case 0: Intent CAMIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(CAMIntent);
break;
case 1: Intent PICKIntent = new Intent(Intent.ACTION_ANSWER);
startActivity(PICKIntent);
break;
case 2:
Intent CALLIntent = new Intent(Intent.ACTION_CALL);
CALLIntent.setData(Uri.parse("tel:"+ gesturePhone));
startActivity(CALLIntent);
break;

case 3: Uri smsToUri = Uri.parse("smsto:"+gesturePhone);
Intent SMSIntent = new Intent(Intent.ACTION_SENDTO,smsToUri);
startActivity(SMSIntent);
break;
case 4: Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
//设置邮件默认地址
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, "");
//设置邮件默认标题
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
//设置要默认发送的内容
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"");
//调用系统的邮件系统
startActivity(Intent.createChooser(emailIntent, "请选择邮件发送软件"));
break;
default:Toast.makeText(this, "手势分辨率太低", 1000);
break;
}
}else{
Toast.makeText(this, "该手势未加入手势库", 1000);
}
}
}
}

  2: SettingListView.java

    一:调用系统中存在的浏览器

      Intent browserIntent = new Intent(Intent.ACTION_VIEW);     

      browserIntent.setType("plain/text");     

      browserIntent.setData(Uri.parse("http://www.cnblogs.com/persen-lee/"));     

      startActivity(Intent.createChooser(browserIntent, "请选择浏览器")); //通过相同的方法可以调用系统中存在的邮箱...

详解见代码

View Code
package persen.android;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

public class SettingListView extends Activity implements OnItemClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.set);
String[] items = new String[]{"检查更新","外观设置","关于我们"};


ListView listViewSetting = (ListView)findViewById(R.id.listViewSetting);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1, items);

listViewSetting.setAdapter(adapter);
listViewSetting.setOnItemClickListener(this);
}

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
switch((int)arg3){
case 0:
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setType("plain/text");
browserIntent.setData(Uri.parse("http://www.cnblogs.com/persen-lee/"));
startActivity(Intent.createChooser(browserIntent, "请选择浏览器"));
/*
String[] dialogList = new String[]{"互联网","uc 浏览器"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("使用以下方式发送").setItems(dialogList, new DialogInterface.OnClickListener(){

public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setData(Uri.parse("
http://www.cnblogs.com/persen-lee/"));
switch(which){
case 0:
browserIntent.setClassName("com.android.browser","com.android.browser.BrowserActivity");
break;
case 1:break;
}
startActivity(browserIntent);
}

});
AlertDialog dialog = builder.create();
dialog.show();
*/
break;
case 1:break;
case 2:String[] items = new String[]{"我们的邮箱(polocoke@163.com)","我们的网址(http://www.cnblogs.com/persen-lee/)"};
AlertDialog.Builder aboutBuilder = new AlertDialog.Builder(this);
aboutBuilder.setTitle("关于我们").setItems(items, new OnClickListener(){

public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

}
});
AlertDialog aboutDlg = aboutBuilder.create();
aboutDlg.show();
break;
}

}


}

  3: MoreListView.java

    一:自定义的Dialog

      Dialog donateDialog = new Dialog(this);     

      donateDialog.setContentView(R.layout.custom_dialog);     //调用自己设计的Dialog布局文件

      donateDialog.setTitle("捐助我们");                                    //设置标题  

      TextView donateText = (TextView) donateDialog.findViewById(R.id.text);     

      donateText.setText("我们的网址是:http://www.cnblogs.com/persen-lee/\n"+"感谢您的支持");     //设置文本

      ImageView donateImage = (ImageView) donateDialog.findViewById(R.id.image);                   //设置标题

      donateImage.setImageResource(R.drawable.donate);   //R.drawable.donate 图标资源    

      donateDialog.show();

详解见代码

View Code
package persen.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

public class MoreListView extends Activity implements OnItemClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.more);
String[] items = new String[]{"MagicTablet 1.0","版本记录和新功能","使用帮助","常见问题","捐助我们","特别致谢"};


ListView listViewSetting = (ListView)findViewById(R.id.listViewMore);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1, items);

listViewSetting.setAdapter(adapter);
// listViewSetting.setOnItemSelectedListener(this);
listViewSetting.setOnItemClickListener(this);
}

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
switch((int)arg3){
case 0: break;
case 1: AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("目前无任何版本更新").setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {

//MoreListView.this.finish();
}
});
AlertDialog dialog = builder.create();
dialog.show();
break;
case 2: Intent usingHelpIntent = new Intent(this,UsingHelp.class);
startActivity(usingHelpIntent);
break;
case 3: Intent faqIntent = new Intent(this,FAQActivity.class);
startActivity(faqIntent);
break;
//Context mContext = getApplicationContext();
case 4: Dialog donateDialog = new Dialog(this);
donateDialog.setContentView(R.layout.custom_dialog);
donateDialog.setTitle("捐助我们");
TextView donateText = (TextView) donateDialog.findViewById(R.id.text);
donateText.setText("我们的网址是:http://www.cnblogs.com/persen-lee/\n"+"感谢您的支持");
ImageView donateImage = (ImageView) donateDialog.findViewById(R.id.image);
donateImage.setImageResource(R.drawable.donate);
donateDialog.show();
break;

case 5: Dialog thanksDialog = new Dialog(this);
thanksDialog.setContentView(R.layout.custom_dialog);
thanksDialog.setTitle("特别感谢");
TextView thanksText = (TextView) thanksDialog.findViewById(R.id.text);
thanksText.setText("感谢那些曾经给予我无私帮助的朋友");
ImageView thanksImage = (ImageView) thanksDialog.findViewById(R.id.image);
thanksImage.setImageResource(R.drawable.thanks);
thanksDialog.show();
break;
}

}

}

     4: GestureBuilderActivity.java 和 CreateGestureActivity.java 来自于Android APIDemo 里的GestureBuilder。

 

  5:读取文本文件并显示在ListView里。

package persen.android;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class FAQActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.faq);

TextView textViewFAQ = (TextView)findViewById(R.id.textViewFAQ);
String data = readFileFromRaw(R.raw.help_problem);
textViewFAQ.setText(data);
}

//读取资源文件里的文本
//若出现文本显示乱码,可以使用文本转换 EncodingUtils
//最更本的做法是保存txt是用utf-8 或unicode
private String readFileFromRaw(int id){

InputStreamReader inputReader = new InputStreamReader(getResources().openRawResource(id));
BufferedReader bufReader = new BufferedReader(inputReader);
String line="";
String data="";
try {
while((line = bufReader.readLine())!=null){
data+=line;
data+="\n";
}
inputReader.close();
bufReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.i("FAQ", "reading file from raw failed");
e.printStackTrace();
}
return data;
}
}

   6:相关问题

  很重要的一点,当使用到android相关服务的时候要设置AndroidManifest。如下是MagicTablet调用的相关系统服务在Manifest里的注册

<uses-permission android:name="android.permission.CALL_PHONE"/>  

<uses-permission android:name="android.permission.SEND_SMS" />  

<uses-permission android:name="android.permission.READ_CONTACTS"/>  

<uses-permission android:name="android.permission.CAMERA" />  

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />"  

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />"

 三:个人总结

  MagicTablet基本用到了Android基础性的东西。比如:android 四大基础组件,Ui,数据存储 和手势。在写MagicTablet中遇到了SQLite相关的一些问题,很感谢网友 Future 和 Android技术交流群 里朋友们 以及 《精通Android3》的作者Dave MacLean 提供的帮助。

 

posted on 2012-03-09 15:50  null_pointer  阅读(704)  评论(0编辑  收藏  举报

导航