package blackice.android.product;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast; public class SmsAssistantActivity extends ListActivity {
private List<Map<String, Object>> mData;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
/*
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
*/
super.onCreate(savedInstanceState);
mData = GetSmsData();
MyAdapter adapter = new MyAdapter(this);
setListAdapter(adapter);
}
//菜单操作
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, Menu.FIRST + 1, 3, "退出").setIcon(android.R.drawable.ic_delete);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { case Menu.FIRST + 1:
//Toast.makeText(this, "删除菜单被点击了", Toast.LENGTH_LONG).show();
finish();
break;
}
return false; }
//列表点击
@Override
protected void onListItemClick(ListView l, View v, int position, long id) { }
public void showInfo(){
new AlertDialog.Builder(this)
.setTitle("自定义listview")
.setMessage("自定义内容!")
.setPositiveButton("关闭", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}
public void SendMessageToServer(int position){
Toast.makeText(this, (String)mData.get(position).get("title"), Toast.LENGTH_LONG).show(); }
//////////////////////////////////自定义列表///////////////////////////////////////////
public class MyAdapter extends BaseAdapter
{
private LayoutInflater mInflater;
public MyAdapter(Context context){
this.mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mData.size();
} @Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
} @Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
} @Override
public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder = null;
if (convertView == null) { holder=new ViewHolder(); convertView = mInflater.inflate(R.layout.vlist2, null);
holder.img = (ImageView)convertView.findViewById(R.id.img);
holder.title = (TextView)convertView.findViewById(R.id.title);
holder.info = (TextView)convertView.findViewById(R.id.info);
holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn);
convertView.setTag(holder); }else {
holder = (ViewHolder)convertView.getTag();
} holder.img.setBackgroundResource((Integer)mData.get(position).get("img"));
holder.title.setText((String)mData.get(position).get("title"));
holder.info.setText((String)mData.get(position).get("info")); holder.viewBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SendMessageToServer(position);
}
});
return convertView;
} }
public final class ViewHolder{
public ImageView img;
public TextView title;
public TextView info;
public Button viewBtn;
}
///////////////////////////////////短信读取//////////////////////////////////////////
protected List<Map<String, Object>> GetSmsData()
{
SmsHander smsHander = new SmsHander(this,"15802023933"); smsHander.createSMSDatabase(); // 创建SQLite数据库
smsHander.insertSMSToDatabase(); // 读取手机短信,插入SQLite数据库
Cursor cursor = smsHander.querySMSInDatabase(100); // 获取前100条短信(日期排序) cursor.moveToPosition(-1);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
while (cursor.moveToNext()) {
//短信内容
//String strAddress = cursor.getString(cursor.getColumnIndex("address"));
String strDate = cursor.getString(cursor.getColumnIndex("date"));
String strBody = cursor.getString(cursor.getColumnIndex("body"));
String strAddress = cursor.getString(cursor.getColumnIndex("address")); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(Long.parseLong(strDate));
strDate = dateFormat.format(date);
Map<String, Object> map = new HashMap<String, Object>();
map.put("img", android.R.drawable.ic_menu_add);//图像资源的ID
map.put("title", strDate);
map.put("info", strBody);
list.add(map);
} //smsList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData()));
if (!cursor.isClosed()) {
cursor.close();
cursor = null;
} smsHander.closeSMSDatabase();
return list;
}
public class SmsHander { SQLiteDatabase db;
Context context;
String phoneNumber; public SmsHander(Context context) {
this.context = context;
}
public SmsHander(Context context,String mobile){
this.context = context;
this.phoneNumber = mobile;
}
//建库
public void createSMSDatabase() {
String sql = "create table if not exists sms("
+ "_id integer primary key autoincrement,"
+ "address varchar(255)," + "person varchar(255),"
+ "body varchar(1024)," + "date varchar(255),"
+ "type integer,isnew integer)";
db = SQLiteDatabase.openOrCreateDatabase(context.getFilesDir().toString() + "/data.db3", null); // 创建数据库连接
db.execSQL(sql);
}
// 获取手机短信
private Cursor getSMSInPhone() {
Uri SMS_CONTENT = Uri.parse("content://sms/");
String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };
Cursor cursor = context.getContentResolver().query(SMS_CONTENT, projection, "address=?", new String[]{this.phoneNumber}, "date desc"); // 获取手机短信
//Cursor cursor = context.getContentResolver().query(SMS_CONTENT, projection, null,null, "date desc"); // 获取手机短信
return cursor;
} // 保存手机短信到 SQLite 数据库
public void insertSMSToDatabase() {
Long lastTime;
Cursor dbCount = db.rawQuery("select count(*) from sms", null);
dbCount.moveToFirst();
if (dbCount.getInt(0) > 0) {
Cursor dbcur = db.rawQuery("select * from sms order by date desc limit 1", null);
dbcur.moveToFirst();
lastTime = Long.parseLong(dbcur.getString(dbcur.getColumnIndex("date")));
} else {
lastTime = new Long(0);
}
dbCount.close();
dbCount = null; Cursor cur = getSMSInPhone(); // 获取短信(游标)
db.beginTransaction(); // 开始事务处理
if (cur.moveToFirst()) {
String address;
String person;
String body;
String date;
int type;
long intDate; int iAddress = cur.getColumnIndex("address");
int iPerson = cur.getColumnIndex("person");
int iBody = cur.getColumnIndex("body");
int iDate = cur.getColumnIndex("date");
int iType = cur.getColumnIndex("type");
int IsNew = 0;
do { address = cur.getString(iAddress);
person = cur.getString(iPerson);
body = cur.getString(iBody);
date = cur.getString(iDate);
type = cur.getInt(iType);
intDate = Long.parseLong(date);
/* */
if((!ExistSms(address,date))&&address.equalsIgnoreCase(this.phoneNumber)){
System.out.println("此日期此短信不在库中");
if (intDate > lastTime) {
String sql = "insert into sms values(null, ?, ?, ?, ?, ?,?)";
Object[] bindArgs = new Object[] { address, person, body, date, type,IsNew};
db.execSQL(sql, bindArgs);
} else {
break;
}
}else
{
System.out.println("此日期此短信已在库中或不是此号码");
} } while (cur.moveToNext());
cur.close();
cur = null;
db.setTransactionSuccessful(); // 设置事务处理成功,不设置会自动回滚不提交
db.endTransaction(); // 结束事务处理
} } // 获取 SQLite 数据库中的全部短信
public Cursor querySMSFromDatabase() {
String sql = "select * from sms order by date desc";
return db.rawQuery(sql, null);
}
// 获取 SQLite 数据库中的最新 size 条短信
public Cursor querySMSInDatabase(int size) {
String sql; Cursor dbCount = db.rawQuery("select count(*) from sms", null);
dbCount.moveToFirst();
if (size < dbCount.getInt(0)) { // 不足 size 条短信,则取前 size 条
sql = "select * from sms order by date desc limit " + size;
} else {
sql = "select * from sms order by date desc";
}
dbCount.close();
dbCount = null; return db.rawQuery(sql, null);
}
// 获取 SQLite 数据库中指定号码的最新 size 条短信
public Cursor querySMSInDatabase(int size,String mobile) {
String sql;
String testSql = "select count(*) from sms where address='"+ mobile +"%'"; Cursor dbCount = db.rawQuery(testSql, null); dbCount.moveToFirst();
if (size < dbCount.getInt(0)) { // 不足 size 条短信,则取前 size 条
sql = "select * from sms where address='"+ mobile ;
sql += "' order by date desc";
sql += " limit " + size;
} else {
sql = "select * from sms where address='"+ mobile +"'";
sql += " order by date desc";
} dbCount.close();
dbCount = null; return db.rawQuery(sql, null);
}
// 获取 SQLite数据库的前 second秒短信
public Cursor getSMSInDatabaseFrom(long second) {
long time = System.currentTimeMillis() / 1000 - second;
String sql = "select * from sms order by date desc where date > " + time;
return db.rawQuery(sql, null);
}
//检查此日期此号码是否有过短信
protected boolean ExistSms(String mobile,String time){
String sql = "select * from sms where address='"+mobile+"' and date='" +time +"'";
Cursor dbCount = db.rawQuery(sql, null);
return dbCount.getCount()>0; }
// 关闭数据库
public void closeSMSDatabase() {
if (db != null && db.isOpen()) {
db.close();
db = null;
}
}
}
}

Android 短信箱操作的更多相关文章

  1. 一枚Android "短信小偷" 病毒的分析

    一.样本简介 样本来自于吾爱破解论坛链接地址为http://www.52pojie.cn/thread-410238-1-1.html,样本不是很复杂有空就分析了一下.Android病毒样本还是很有意 ...

  2. Android短信监听实现,及Android4.4之后短信机制变更

    前阵子公司有一个项目,简单的监听短信应用,功能只有如下两个: 1.监听短信并获取短信内容上传服务器: 2.从服务器获取短信内容,发送出去    按照传统的思路,监听短信我们有两种方式:第一种是使用广播 ...

  3. 【mob】Android短信验证+源码

    在很多的应用当中,都涉及到了短信验证的功能,比如在注册或者找回密码的时候,那么我们如何通过第三方的平台来完成这个功能呢? 本面博文就实现短信验证,来做一个小的栗子. 第一步-下载开发包 第二步-将SD ...

  4. Android短视频中如何实现720P磨皮美颜录制?

    视频中磨皮.美颜功能已成为刚需,那么如何在Android短视频中实现720P磨皮美颜录制?本篇文章中,网易云信资深开发工程师将向大家介绍具体的操作方法. 相关阅读推荐 <短视频技术详解:Andr ...

  5. 如何实现 Android 短视频跨页面的流畅续播?

    在一切皆可视频化的今天,短视频内容作为移动端产品新的促活点,受到了越来越多的重视与投入,同时短视频也是增加用户粘性.增加用户停留时长的一把利器.那么如何快速实现移动端短视频功能呢?前两篇我们介绍了盒马 ...

  6. [转]Android - 文件读写操作 总结

     转自:http://blog.csdn.net/ztp800201/article/details/7322110 Android - 文件读写操作 总结 分类: Android2012-03-05 ...

  7. Android 短信的还原

    上篇文章讲到<Android 短信的备份>,本文主要实现Android 短信的还原,即是将一条 布局文件: <RelativeLayout xmlns:android="h ...

  8. android管理联系人操作

    ContentProvider扩展之管理系统联系人 我们都知道ContentProvider是用来共享数据的,然而android本身就提供了大量的ContentProvider,例如联系人信息,系统的 ...

  9. 【转】Android 使用ORMLite 操作数据库

    Android 使用ORMLite 操作数据库   用过ssh,s2sh的肯定不会陌生 ,应该一学就会 第一步: 下载ormlite-android-4.41.jar和ormlite-core-4.4 ...

随机推荐

  1. Java并发编程(三):并发模拟(工具和Java代码介绍)

    并发模拟工具介绍 ① Postman : Http请求模拟工具 从图上我们可以看出,Postman模拟并发其实是分两步进行操作的.第一步:左边的窗口,在窗口中设置相关接口以及参数,点击运行进行第二步. ...

  2. Apache 虚拟目录和默认首页的设置

    Apache虚拟目录和默认首页的设置,用apache服务器的朋友必须要懂的. 虚拟目录 1.找到"conf/httpd.conf" 文件 2.在节点:<IfModule al ...

  3. Mac OSX下Go语言开发环境的搭建与配置--使用InteliJ IDEA 13

    折腾了一上午终于把go语言的ide配置好了. 其实GO语言的语法和特性早在去年的时候就学习了一遍.结果后来一直没机会进行开发,结果还是个GO小白.感叹一下,要学好一门编程语言唯一的途径就是多写代码.. ...

  4. 【flink training】 打车热点区域实时统计PopularPlaces

    http://training.data-artisans.com/是Apache Flink商业公司DataArtisans提供的一个flink学习平台,主要提供了一些业务场景和flink api结 ...

  5. 跟着百度学PHP[14]-PDO的预处理语句1

    预处理语句有以下两个特点: 1.效率高 2.安全性好 为什么说预处理语句效率高呢? 预处理语句就好比一个模板,比如下面的一串插入语句: insert into admin(id,username,pa ...

  6. flask 中xx.init_app(app)方法

    bootstrap = Bootstrap() mail = Mail() moment = Moment() db = SQLAlchemy() def create_app(config_name ...

  7. Expectation Maximization(EM)算法note

    EM算法,之前上模式识别课上,推导过,在<统计学习方法>中没耐性的看过几次,个人感觉讲的过于理论,当时没怎么看懂,后来学lda,想要自己实现一下em算法,又忘记了,看来还是学的不够仔细,认 ...

  8. website 合集

    1. oracle  http://asktom.oracle.com ( 英文 ) http://itpub.net ( 中文 ) https://www.oracle.com/communitie ...

  9. LigerTree的使用

    效果图: 页面: <div id="divs" style="width: 310px; overflow-x: hidden; overflow-y: hidde ...

  10. 【BZOJ】3391: [Usaco2004 Dec]Tree Cutting网络破坏(dfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3391 显然判断每个点只需要判断子树是否小于等于n/2即可 那么我们虚拟一个根,然后计算每个子树的si ...