最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android開(kāi)發(fā)獲取短信的內(nèi)容并截取短信

 更新時(shí)間:2015年12月23日 14:15:12   投稿:mrr  
本文給大家介紹android開(kāi)發(fā)獲取短信內(nèi)容并截取短息的相關(guān)內(nèi)容,本文代碼簡(jiǎn)單易懂,感興趣的朋友一起學(xué)習(xí)吧

1、首先我們要寫(xiě)一個(gè)廣播接收器,當(dāng)我們的手機(jī)收到短信時(shí),系統(tǒng)會(huì)自動(dòng)發(fā)送一個(gè)廣播,我們只需要接收到這條廣播就可以了

2、在廣播里面,我們重寫(xiě)的onReceive()方法,通過(guò)里面的Intent寫(xiě)到的Bundle就可以拿到短信的內(nèi)容,

3、清單文件里面我們必須要添加權(quán)限,否則無(wú)法接收到。

4、為了防止我們的廣播接收不到,我們自己寫(xiě)的廣播接收器的權(quán)限必須要大,以防萬(wàn)一,我設(shè)置了1000。

下面上代碼,里面的注釋也比較詳細(xì)..

 <?xml version="." encoding="utf-"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.fanlei.cutnotedemo" >
   //接收短信
   <uses-permission android:name="android.permission.RECEIVE_SMS"/>
   <application
     android:allowBackup="true"
     android:icon="@drawable/ic_launcher"
     android:label="@string/app_name"
     android:theme="@style/AppTheme" >
     <!-- action:name = 的名稱是固定的 -->
     <receiver android:name=".NoteReceiver">
       <intent-filter android:priority="">
         <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
       </intent-filter>
     </receiver>
     <activity
       android:name=".MainActivity"
       android:label="@string/app_name" >
       <intent-filter>
         <action android:name="android.intent.action.MAIN" />
         <category android:name="android.intent.category.LAUNCHER" />
       </intent-filter>
     </activity>
   </application>
 </manifest>

寫(xiě)一個(gè)類,繼承BroadcastReceiver

Android--獲取短信的內(nèi)容,截取短信
 package com.example.fanlei.cutnotedemo;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.telephony.SmsMessage;
 import android.widget.Toast;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 /**
 * 廣播接收器
 */
 public class NoteReceiver extends BroadcastReceiver {
   private static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";
   @Override
   public void onReceive(Context context, Intent intent) {
     String action = intent.getAction();
     //判斷廣播消息
     if (action.equals(SMS_RECEIVED_ACTION)){
       Bundle bundle = intent.getExtras();
       //如果不為空
       if (bundle != null){
         //將pdus里面的內(nèi)容轉(zhuǎn)化成Object[]數(shù)組
         Object pdusData[] = (Object[]) bundle.get("pdus");
         //解析短信
         SmsMessage[] msg = new SmsMessage[pdusData.length];
         for (int i = ;i < msg.length;i++){
           byte pdus[] = (byte[]) pdusData[i];
           msg[i] = SmsMessage.createFromPdu(pdus);
         }
         StringBuffer content = new StringBuffer();//獲取短信內(nèi)容
         StringBuffer phoneNumber = new StringBuffer();//獲取地址
         StringBuffer receiveData = new StringBuffer();//獲取時(shí)間
         //分析短信具體參數(shù)
         for (SmsMessage temp : msg){
           content.append(temp.getMessageBody());
           phoneNumber.append(temp.getOriginatingAddress());
           receiveData.append(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS")
               .format(new Date(temp.getTimestampMillis())));
         }
         /**
         * 這里還可以進(jìn)行好多操作,比如我們根據(jù)手機(jī)號(hào)進(jìn)行攔截(取消廣播繼續(xù)傳播)等等
         */
         Toast.makeText(context,phoneNumber.toString()+content+receiveData, Toast.LENGTH_LONG).show();//短信內(nèi)容
       }
     }
   }
 }

ps:android獲取短信所有內(nèi)容

public class GetMessageInfo { 
  List<MessageInfo> list; 
  Context context; 
  MessageInfo messageInfo; 
  public GetMessageInfo(Context context) { 
    list = new ArrayList<MessageInfo>(); 
    this.context = context; 
  } 
  // --------------------------------收到的短息信息---------------------------------- 
  public List<MessageInfo> getSmsInfos() { 
    final String SMS_URI_INBOX = "content://sms/inbox";// 收信箱 
    try { 
      ContentResolver cr = context.getContentResolver(); 
      String[] projection = new String[] { "_id", "address", "person","body", "date", "type" }; 
      Uri uri = Uri.parse(SMS_URI_INBOX); 
      Cursor cursor = cr.query(uri, projection, null, null, "date desc"); 
      while (cursor.moveToNext()) { 
        messageInfo = new MessageInfo(); 
        // -----------------------信息---------------- 
        int nameColumn = cursor.getColumnIndex("person");// 聯(lián)系人姓名列表序號(hào) 
        int phoneNumberColumn = cursor.getColumnIndex("address");// 手機(jī)號(hào) 
        int smsbodyColumn = cursor.getColumnIndex("body");// 短信內(nèi)容 
        int dateColumn = cursor.getColumnIndex("date");// 日期 
        int typeColumn = cursor.getColumnIndex("type");// 收發(fā)類型 1表示接受 2表示發(fā)送 
        String nameId = cursor.getString(nameColumn); 
        String phoneNumber = cursor.getString(phoneNumberColumn); 
        String smsbody = cursor.getString(smsbodyColumn); 
        Date d = new Date(Long.parseLong(cursor.getString(dateColumn))); 
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd " + "\n" + "hh:mm:ss"); 
        String date = dateFormat.format(d); 
        // --------------------------匹配聯(lián)系人名字-------------------------- 
        Uri personUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,phoneNumber); 
        Cursor localCursor = cr.query(personUri, new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup.PHOTO_ID,PhoneLookup._ID }, null, null, null); 
        System.out.println(localCursor.getCount()); 
        System.out.println("之前----"+localCursor); 
        if (localCursor.getCount()!=0) { 
          localCursor.moveToFirst(); 
          System.out.println("之后----"+localCursor); 
          String name = localCursor.getString(localCursor.getColumnIndex(PhoneLookup.DISPLAY_NAME)); 
          long photoid = localCursor.getLong(localCursor.getColumnIndex(PhoneLookup.PHOTO_ID)); 
          long contactid = localCursor.getLong(localCursor.getColumnIndex(PhoneLookup._ID)); 
          messageInfo.setName(name); 
          // 如果photoid 大于0 表示聯(lián)系人有頭像 ,如果沒(méi)有給此人設(shè)置頭像則給他一個(gè)默認(rèn)的 
          if (photoid > 0) { 
           Uri uri1 = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,contactid); 
            InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri1); 
            messageInfo.setContactPhoto(BitmapFactory.decodeStream(input)); 
          } else { 
            messageInfo.setContactPhoto(BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_launcher)); 
          } 
        }else{ 
            messageInfo.setName(phoneNumber); 
            messageInfo.setContactPhoto(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher)); 
          } 
        localCursor.close(); 
        messageInfo.setSmsContent(smsbody); 
        messageInfo.setSmsDate(date); 
        list.add(messageInfo); 
      } 
    } catch (SQLiteException e) { 
      e.printStackTrace(); 
    } 
    return list; 
  } 
} 

以上內(nèi)容是小編給大家分享的Android開(kāi)發(fā)獲取短信的內(nèi)容并截取短信,希望大家喜歡。

相關(guān)文章

  • AndroidStduio3.0 使用gradle將module打包jar文件的方法

    AndroidStduio3.0 使用gradle將module打包jar文件的方法

    這篇文章主要介紹了AndroidStduio3.0 使用gradle將module打包jar文件的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-04-04
  • Android添加ButterKnife時(shí)報(bào)錯(cuò)Error:(2, 0) Cannot add extension with name ''android''的解決辦法

    Android添加ButterKnife時(shí)報(bào)錯(cuò)Error:(2, 0) Cannot add extension wit

    今天小編就為大家分享一篇關(guān)于Android添加ButterKnife時(shí)報(bào)錯(cuò)Error:(2, 0) Cannot add extension with name 'android'的解決辦法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-12-12
  • Android ScrollView只能添加一個(gè)子控件問(wèn)題解決方法

    Android ScrollView只能添加一個(gè)子控件問(wèn)題解決方法

    這篇文章主要介紹了Android ScrollView只能添加一個(gè)子控件問(wèn)題解決方法,涉及Android界面布局的相關(guān)技巧,需要的朋友可以參考下
    2016-02-02
  • Phonegap使用拍照功能時(shí)的內(nèi)存問(wèn)題

    Phonegap使用拍照功能時(shí)的內(nèi)存問(wèn)題

    最近幾天在學(xué)習(xí)使用phonegap進(jìn)行android應(yīng)用的開(kāi)發(fā),網(wǎng)上的資料比較亂,個(gè)人參考了很多資料,也試驗(yàn)了很多次,一直在摸索,總算小有心得,這此過(guò)程中也遇到了一些問(wèn)題,這里給大家分享下解決Phonegap使用拍照功能時(shí)的內(nèi)存問(wèn)題的方法,這里簡(jiǎn)單的整理一下
    2015-05-05
  • Kotlin與Java相互調(diào)用的完整實(shí)例

    Kotlin與Java相互調(diào)用的完整實(shí)例

    Kotlin的設(shè)計(jì)過(guò)程中就考慮到了與Java的互操作性,在Kotlin中可以直接調(diào)用既有的Java代碼,反過(guò)來(lái)在Java中也可以很流暢地使用Kotlin代碼,這篇文章主要給大家介紹了關(guān)于Kotlin與Java相互調(diào)用的相關(guān)資料,需要的朋友可以參考下
    2021-12-12
  • textView 添加超鏈接(兩種實(shí)現(xiàn)方式)

    textView 添加超鏈接(兩種實(shí)現(xiàn)方式)

    在textView添加超鏈接,有兩種方式,第一種通過(guò)HTML格式化你的網(wǎng)址,一種是設(shè)置autolink,讓系統(tǒng)自動(dòng)識(shí)別超鏈接,下面為大家介紹下這兩種方法的實(shí)現(xiàn)
    2013-06-06
  • Android實(shí)現(xiàn)保存圖片到本地并在相冊(cè)中顯示

    Android實(shí)現(xiàn)保存圖片到本地并在相冊(cè)中顯示

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)保存圖片到本地并在相冊(cè)中顯示的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Android  LayoutInflater加載布局詳解及實(shí)例代碼

    Android LayoutInflater加載布局詳解及實(shí)例代碼

    這篇文章主要介紹了Android LayoutInflater加載布局詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Android編程檢測(cè)手機(jī)錄音權(quán)限是否打開(kāi)的方法

    Android編程檢測(cè)手機(jī)錄音權(quán)限是否打開(kāi)的方法

    這篇文章主要介紹了Android編程檢測(cè)手機(jī)錄音權(quán)限是否打開(kāi)的方法,涉及Android針對(duì)音頻操作的相關(guān)技巧與注意事項(xiàng),需要的朋友可以參考下
    2017-11-11
  • Android實(shí)現(xiàn)漸變色水波紋效果

    Android實(shí)現(xiàn)漸變色水波紋效果

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)漸變色水波紋效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11

最新評(píng)論

晋中市| 邵阳县| 台州市| 尼木县| 抚宁县| 五寨县| 奈曼旗| 买车| 黑龙江省| 万年县| 长兴县| 彰武县| 新宾| 龙胜| 贵德县| 武宣县| 宁化县| 许昌县| 齐齐哈尔市| 金华市| 特克斯县| 习水县| 周宁县| 连江县| 光山县| 偏关县| 井研县| 武川县| 灵台县| 浪卡子县| 古田县| 丘北县| 长兴县| 略阳县| 涟水县| 安乡县| 灵武市| 集贤县| 城口县| 凤山市| 克东县|