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. php.ini配置与中国间隔12小时间设置方法

    打开php.ini 配置文件找到date.timezone把=号后面的参数改成这个date.timezone = Etc/GMT+4即可,这样与中国的时间误差即能达到12小时

  2. ConcurrentBag同线程元素的添加和删除

    https://www.mgenware.com/blog/?p=232 ConcurrentBag<T>对于同一个线程值的添加和删除是非常快的,因为ConcurrentBag内部将数据按 ...

  3. WCF实现客户端和服务端

    service side 1.定义ServiceContract: 2.new a ServiceHost 3. add endpoint using System.ServiceModel; nam ...

  4. 分布式协调服务ZooKeeper工作原理

    分布式协调服务ZooKeeper工作原理 原创 2016-02-19 杜亦舒 性能与架构 性能与架构 性能与架构 微信号 yogoup 功能介绍 网站性能提升与架构设计 大数据处理框架Hadoop.R ...

  5. Adreno Profiler连接安卓手机profile第三方app渲染过程

    1,下载Android SDK,adb在Android SDK/platform-tools下. 2,添加adb环境变量:http://www.cnblogs.com/xwlyun/archive/2 ...

  6. c#动态类型

    class Program { static void Main(string[] args) { dynamic test = new ExpandoObject(); test.Name = &q ...

  7. WEB前端的性能优化

    转自:http://www.2cto.com/kf/201604/498725.html 网站的划分一般为二:前端和后台.我们可以理解成后台是用来实现网站的功能的,比如:实现用户注册,用户能够为文章发 ...

  8. AdapterView 和 RecyclerView 的连续滚动

    AdapterView 和 RecyclerView 的连续滚动 android RecyclerView tutorial 概述 ListView 和 GridView 的实现方式 Recycler ...

  9. 基于ffmpeg 直播推流和播放rtmp (IOS源码)

    ios直播推流每秒能达到30帧,比安卓要强,视频采用软编码的话手机会发烫,得采用码编码,播放视频采用opengl渲染. ffmpeg初始化代码如下: int init_Code(int width, ...

  10. 5plus

    http://124.173.121.89/wx/index.html?1410766859789