日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

安卓的SMS 短信的增删改查

發布時間:2024/1/1 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 安卓的SMS 短信的增删改查 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這是安卓學習之旅1——-

關于安卓的SMS 短信的增刪改查

首先我們要知道是的是我們用的是 ContentResolver cr = getContentResolver(); 這個是內容提供者。
cr有4個方法
1:查詢
2:增加
3:刪除
4:更新

1.查詢:
原型:query(table,columns, selection, selectionArgs, groupBy, having, orderBy, limit)
例子:Cursor cur = cr.query(SMS_INBOX, projection, null, null, “date desc”);
參數解析: 可以參考這個
table:表名。
columns:列名。
selection:選擇條件 類似 where 后的一串
selectionArgs:這是為了避免出現 name=’1’傳入” 的做法,可以new String[] {name}, 在selection前面使用?代替 (name 是 字符類型 ,所以在sql里 是要加引號的),可參考query() 中的 selectionArgs 的用法
groupBy:分組
having :
orderby desc(倒序 大——小) asc(正序 小——大)


例子解析:
private Uri SMS_INBOX = Uri.parse(“content://sms/”);
SMS_INBOX 是 一個URI 地址,指向的是所有的全部短信
projection 是選出來的列,當然也可以默認。
String[] projection = new String[] { “_id”, “address”, “person”, “body”, “date”, “type”, };
之后要解釋的是有多少個列名:
  _id:短信序號,如100
  
  thread_id:對話的序號,如100,與同一個手機號互發的短信,其序號是相同的
  
  address:發件人地址,即手機號,如+86138138000
  
  person:發件人,如果發件人在通訊錄中則為具體姓名,陌生人為null
  
  date:日期,long型,如1346988516,可以對日期顯示格式進行設置
  
  protocol:協議0SMS_RPOTO短信,1MMS_PROTO彩信
  
  read:是否閱讀0未讀,1已讀
  
  status:短信狀態-1接收,0complete,64pending,128failed
  
  type:短信類型1是接收到的,2是已發出
  
  body:短信具體內容
  
  service_center:短信服務中心號碼編號,如+8613800755500


下一步是getColumnIndex,獲取列名 :例: int index_Address = cur.getColumnIndex(“address”);
之后獲取對應的值:String strbody = cur.getString(index_Body); //獲取了短信的內容

這里注意的是Data 時間的獲取。 這里時間是用的Unix 時間戳。需要用

//轉換Linux 時間戳 很關鍵//long longDate = cur.getLong(index_Date); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH時mm分ss秒"); Date d = new Date(longDate); String strDate = dateFormat.format(d); // 格式化時間 //轉換Linux 時間戳 很關鍵//

java.lang.String.contains(“sms_body”) 方法返回true,當且僅當此字符串包含指定的char值序列
可以用來匹配短信內容
代碼如下:
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
private Button myButton;
private Uri SMS_INBOX = Uri.parse(“content://sms/”);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = (Button) findViewById(R.id.button1);

}public void xtx(View view) {Toast.makeText(getApplicationContext(), "ok", 0).show();getSmsFromPhone(); }private void getSmsFromPhone() {// TODO Auto-generated method stubContentResolver cr = getContentResolver(); String[] projection = new String[] { "_id", "address", "person","body", "date", "type", }; //"_id", "address", "person",, "date", "type Cursor cur = cr.query(SMS_INBOX, projection, null, null, "date desc"); if (cur.moveToFirst()) { int index_Address = cur.getColumnIndex("address"); int index_Person = cur.getColumnIndex("person"); int index_Body = cur.getColumnIndex("body"); int index_Date = cur.getColumnIndex("date"); int index_Type = cur.getColumnIndex("type"); do { String strAddress = cur.getString(index_Address); int intPerson = cur.getInt(index_Person); String strbody = cur.getString(index_Body); int intType = cur.getInt(index_Type); //轉換Linux 時間戳 很關鍵//long longDate = cur.getLong(index_Date); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH時mm分ss秒"); Date d = new Date(longDate); String strDate = dateFormat.format(d); // 格式化時間//轉換Linux 時間戳 很關鍵//Log.e("tag", "發件人:"+strAddress+", "+strDate+","+strbody+"\n");} while (cur.moveToNext()); //增加---ContentValues values = new ContentValues(); values.put("address", "10010"); values.put("type", "1"); values.put("body", "喜歡你啊---小賀志霄"); values.put("date",System.currentTimeMillis()); cr.insert(SMS_INBOX, values); ///----cr.delete(SMS_INBOX, "address=95533",null); //刪除ContentValues values_1 = new ContentValues(); values_1.put("read", "0");cr.update(SMS_INBOX, values_1, "address=10010",null); //更新Log.e("tag", "Other"); } }

}
`
最后權限什么的小注意一下:

<uses-permission android:name="android.permission.READ_SMS" /> <!-- 讀寫短信 -->> <uses-permission android:name="android.permission.WRITE_SMS"/>

第一次寫博客,寫的比較渣:: 還可以在改進,路一步步走,嗯嗯!!

一些其中解決問題的參考鏈接:

http://blog.csdn.net/gubaohua/article/details/575488/ //這是SimpleDateFormat使用詳解
http://blog.csdn.net/tianyitianyi1/article/details/18037911 //這是 Android中讀取短信
http://blog.csdn.net/mad1989/article/details/22426415/ 這是 Android中讀取短信(多了監聽)

總結

以上是生活随笔為你收集整理的安卓的SMS 短信的增删改查的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。