編輯:關於Android編程
主要思路:
1. 分析出短信數據庫結構,信息都存儲在mmssms.db中的sms表中。關鍵字段有:_id(短信序號),body(短信內容),thread_id(聯系人序號),person(名字,但經常得到null)
while (cur.moveToNext()) { String number=cur.getString(cur.getColumnIndex(“address”));//手機號 String name = cur.getString(cur.getColumnIndex(“person”));//聯系人姓名列表 String body = cur.getString(cur.getColumnIndex(“body”));//信息 String id=cur.getString(cur.getColumnIndex(“_id”));
2. 通過Pattern .Match 匹配關鍵字來檢索數據庫裡的信息,若匹配則獲取。
Pattern pattern=Pattern.compile(“.(驗證碼|密碼).“); Matcher matcher=pattern.matcher(body);
3. 新信息到來時提醒並將信息顯示在頁面上:繼承ContentObserver 觀察者模式
@Override public void onChange(boolean selfChange) { super.onChange(selfChange); super.onChange(selfChange); //每當有新短信到來時,使用我們獲取短消息的方法 smsInfos=getSmsFromPhone(); adapter.notifyDataSetChanged(); //如何判斷是否private message? Toast.makeText(MainActivity.this,”There have a new message!”,Toast.LENGTH_SHORT).show(); // Message message=new Message(); // message.what=1; // message.obj=newSmsInfo; // handler.sendMessage(message); Log.i(SMS_MESSAGE,”correct”); }
4. 刪除信息,再次讀取數據庫
while (position>=0) { String id = smsInfos.get(position).getId(); //通過協議讀取mmssms.db ,不能直接讀取 Uri deleteUri = Uri.parse(“content://sms”); getContentResolver().delete(deleteUri, “sms._id=?”, new String[]{id}); smsInfos.remove(position); position = position - 1; }
具體代碼:
? class MainActivity extends Activity { private ListView showNote; private Uri SMS_INBOX = Uri.parse("content://sms"); private SmsObserver smsObserver; private Button deleteButton; private SmsAdapter adapter; private final static String SMS_MESSAGE="newSmsInfo"; // private List newSmsInfo=new ArrayList(); // public ArrayList note; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setTitle(R.string.title);// setContentView(R.layout.app_login); showNote=(ListView)findViewById(R.id.showList); smsInfos=getSmsFromPhone(); //檢測返回的列表長度是否一致 // final int count=smsInfos.size(); smsObserver = new SmsObserver(this, smsHandler); getContentResolver().registerContentObserver(SMS_INBOX, true, smsObserver); adapter=new SmsAdapter(this); showNote.setAdapter(adapter); deleteButton=(Button)findViewById(R.id.delete); deleteButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //返回列表長度 int position=smsInfos.size(); int count=position; //從零計數 position=position-1; while (position>=0) { String id = smsInfos.get(position).getId(); //通過協議讀取mmssms.db ,不能直接讀取 Uri deleteUri = Uri.parse("content://sms"); getContentResolver().delete(deleteUri, "sms._id=?", new String[]{id}); smsInfos.remove(position); position = position - 1; } // SmsAdapter adapter=new SmsAdapter(MainActivity.this); // //刷新列表 // // showNote.setAdapter(adapter); //通知數據改變 // adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged(); Toast.makeText(MainActivity.this,"刪除了"+count+"條信息!",Toast.LENGTH_SHORT).show(); } }); } private List smsInfos=new ArrayList(); //讀取短信數據庫mmssms.db 的基本表sms 並返回查找的字段 public List getSmsFromPhone() { ContentResolver cr = getContentResolver(); List smsInfoList=new ArrayList(); //查詢字段 String []projection=new String []{"address","person","body","_id"}; Cursor cur = cr.query(SMS_INBOX, projection, null, null, "date desc"); if (cur!=null){ while (cur.moveToNext()) { String number = cur.getString(cur.getColumnIndex("address"));//手機號 String name = cur.getString(cur.getColumnIndex("person"));//聯系人姓名列表 String body = cur.getString(cur.getColumnIndex("body"));//信息 String id=cur.getString(cur.getColumnIndex("_id")); // 過濾條件為密碼或驗證碼 Pattern pattern=Pattern.compile(".*(驗證碼|密碼).*"); Matcher matcher=pattern.matcher(body); if (matcher.matches()){ SmsInfo smsInfo = new SmsInfo(); smsInfo.setName(name); smsInfo.setAddress(number); smsInfo.setText(body); smsInfo.setId(id); smsInfoList.add(smsInfo); } } cur.close(); } // } return smsInfoList; } Handler smsHandler=new Handler(); // { // @Override // public void handleMessage(Message msg) { // super.handleMessage(msg); // if (msg.what==1){ // SmsAdapter smsAdapter=new SmsAdapter(MainActivity.this); // showNote.setAdapter(smsAdapter); // smsAdapter.notifyDataSetChanged(); // // // } // } // }; public class SmsObserver extends ContentObserver { private Context context; private Handler handler; // private final static int SMS_MESSAGE=1; public SmsObserver(Context context, android.os.Handler handler) { super(handler); this.context=context; this.handler=handler; } @Override public void onChange(boolean selfChange) { super.onChange(selfChange); //每當有新短信到來時,使用我們獲取短消息的方法 smsInfos=getSmsFromPhone(); adapter.notifyDataSetChanged(); //如何判斷是否private message? Toast.makeText(MainActivity.this,"There have a new message!",Toast.LENGTH_SHORT).show(); // Message message=new Message(); // message.what=1; // message.obj=newSmsInfo; // handler.sendMessage(message); Log.i(SMS_MESSAGE,"correct"); } } class SmsAdapter extends BaseAdapter{ // private View mView;/ private LayoutInflater layoutInflater=getLayoutInflater(); public SmsAdapter(Context context){ layoutInflater=LayoutInflater.from(context); } @Override public int getCount() { return smsInfos.size(); } @Override public Object getItem(int position) { return smsInfos.get(position); } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // ViewHolder viewHolder; if (convertView==null){ convertView=layoutInflater.inflate(R.layout.item,null); viewHolder=new ViewHolder(); viewHolder.name=(TextView)convertView.findViewById(R.id.name); viewHolder.address=(TextView)convertView.findViewById(R.id.address); viewHolder.text=(TextView)convertView.findViewById(R.id.text); viewHolder.id=(TextView)convertView.findViewById(R.id.id); convertView.setTag(viewHolder); }viewHolder=(ViewHolder)convertView.getTag(); viewHolder.name.setText(smsInfos.get(position).getName()); viewHolder.address.setText(smsInfos.get(position).getAddress()); viewHolder.text.setText(smsInfos.get(position).getText()); viewHolder.id.setText(smsInfos.get(position).getId()); return convertView; } public final class ViewHolder{ public TextView name; public TextView address; public TextView text; public TextView id; } } } SmsInfo.class: ?public class SmsInfo { private String name; private String address; private String text; private String id; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public String getText() { return text; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public void setText(String text) { this.text = text; } public void setName(String name) { this.name = name; } }
關於動畫的實現,Android提供了Animation,在Android SDK介紹了2種Animation模式: 1. Tween Animation:通過對場景裡的對
package com.gc.alertdialogdemo; /** * AlertDialog: * 1、AlertDialog的功能很強大,它可以生成各種內容的
首先來看看效果圖:1,SweepGradient(梯度渲染)public SweepGradient (float cx, float cy, int[] colors,
Android Emulator 給用戶提供 GPU on 選項,意思是利用 Host ( 就是運行 Emulator 的PC機) 的 GPU. 當然PC機必須把 Op