Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 淺談Android 數據庫 短信 監聽

淺談Android 數據庫 短信 監聽

編輯:關於Android編程

Android 實現監聽短信(同時監聽廣播和數據庫)代碼如下:

  1. 攔截廣播
    package com.javen.sms.receiver;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.TimeZone;
    
    import com.javen.util.InterceptKeyKeeper;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.telephony.SmsMessage;
    import android.util.Log;
    import android.widget.Toast;
    
    public class SmsReceiver extends BroadcastReceiver {
        private Context mContext;
        public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";
        public static final String SMS_DELIVER_ACTION = "android.provider.Telephony.SMS_DELIVER";
    
        @Override
        public void onReceive(Context context, Intent intent) {
            this.mContext=context;
            Toast.makeText(context, "接收短信執行了.....", Toast.LENGTH_LONG).show();
            Log.e("SMSReceiver, isOrderedBroadcast()=", isOrderedBroadcast()+"");
            Log.e("SmsReceiver onReceive...", "接收短信執行了......");
            String action = intent.getAction();
            if (SMS_RECEIVED_ACTION.equals(action) || SMS_DELIVER_ACTION.equals(action)) {
                Toast.makeText(context, "開始接收短信.....", Toast.LENGTH_LONG).show();
                Log.e("SmsReceiver onReceive...", "開始接收短信.....");
                
                Bundle bundle = intent.getExtras();
                if (bundle != null) {
                    Object[] pdus = (Object[])bundle.get("pdus");  
                    if (pdus != null && pdus.length > 0) {  
                        SmsMessage[] messages = new SmsMessage[pdus.length];  
                        for (int i = 0; i < pdus.length; i++) {  
                            byte[] pdu = (byte[]) pdus[i];  
                            messages[i] = SmsMessage.createFromPdu(pdu);  
                        }  
                        for (SmsMessage message : messages) {  
                            String content = message.getMessageBody();// 得到短信內容  
                            String sender = message.getOriginatingAddress();// 得到發信息的號碼  
                            if (content.contains(InterceptKeyKeeper.getInterceptKey(mContext))) {
                                Toast.makeText(mContext, "內容為:"+content, Toast.LENGTH_LONG).show();
                                //setResultData(null);
                                this.abortBroadcast();// 中止
                            }else if (sender.equals("10010") || sender.equals("10086")) {
                                Toast.makeText(mContext, "內容為:"+content, Toast.LENGTH_LONG).show();
                                this.abortBroadcast();// 中止
                            }
                            Date date = new Date(message.getTimestampMillis());  
                            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            format.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
                            String sendContent = format.format(date) + ":" + sender + "--" + content;  
                            Log.e("SmsReceicer onReceive ",sendContent +" ");
                        }
                    }
                }
            }
        }
    }
     

     


  2. 開啟一個服務開監聽數據庫
    package com.javen.service;
    
    import android.app.Service;
    import android.content.ContentResolver;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.IBinder;
    import android.os.Process;
    import android.widget.Toast;
    
    /**
     * @author Javen
     * 開啟一個服務開監聽數據庫
     */
    public class SmsService extends Service {
    
        private SmsObserver mObserver;
    
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public void onCreate() {
            Toast.makeText(this, "SmsService 服務器啟動了....", Toast.LENGTH_LONG).show();
            // 在這裡啟動
            ContentResolver resolver = getContentResolver();
            mObserver = new SmsObserver(resolver, new SmsHandler(this));
            resolver.registerContentObserver(Uri.parse("content://sms"), true,mObserver);
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            this.getContentResolver().unregisterContentObserver(mObserver);
            Process.killProcess(Process.myPid());
        }
    }
     

     


  3. 數據庫觀察者
    package com.javen.service;
    
    import android.content.ContentResolver;
    import android.database.ContentObserver;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Message;
    import android.util.Log;
    
    /**
     * @author Javen
     * 數據庫觀察者
     */
    public class SmsObserver extends ContentObserver {
    
        private ContentResolver mResolver;
        public SmsHandler smsHandler;
    
        public SmsObserver(ContentResolver mResolver, SmsHandler handler) {
            super(handler);
            this.mResolver = mResolver;
            this.smsHandler = handler;
        }
    
        @Override
        public void onChange(boolean selfChange) {
            Log.i("SmsObserver onChange ", "SmsObserver 短信有改變");
            Cursor mCursor = mResolver.query(Uri.parse("content://sms/inbox"),
                    new String[] { "_id", "address", "read", "body", "thread_id" },
                    "read=?", new String[] { "0" }, "date desc");
    
            if (mCursor == null) {
                return;
            } else {
                while (mCursor.moveToNext()) {
                    SmsInfo _smsInfo = new SmsInfo();
    
                    int _inIndex = mCursor.getColumnIndex("_id");
                    if (_inIndex != -1) {
                        _smsInfo._id = mCursor.getString(_inIndex);
                    }
    
                    int thread_idIndex = mCursor.getColumnIndex("thread_id");
                    if (thread_idIndex != -1) {
                        _smsInfo.thread_id = mCursor.getString(thread_idIndex);
                    }
    
                    int addressIndex = mCursor.getColumnIndex("address");
                    if (addressIndex != -1) {
                        _smsInfo.smsAddress = mCursor.getString(addressIndex);
                    }
    
                    int bodyIndex = mCursor.getColumnIndex("body");
                    if (bodyIndex != -1) {
                        _smsInfo.smsBody = mCursor.getString(bodyIndex);
                    }
    
                    int readIndex = mCursor.getColumnIndex("read");
                    if (readIndex != -1) {
                        _smsInfo.read = mCursor.getString(readIndex);
                    }
                    
                    // 根據你的攔截策略,判斷是否不對短信進行操作;將短信設置為已讀;將短信刪除
                    // TODO
                    System.out.println("獲取的短信內容為:"+_smsInfo.toString());
                    Log.i("SmsObserver ...", "獲取的短信內容為:"+_smsInfo.toString());
                    Message msg = smsHandler.obtainMessage();
                    _smsInfo.action = 2;// 0不對短信進行操作;1將短信設置為已讀;2將短信刪除
                    msg.obj = _smsInfo;
                    smsHandler.sendMessage(msg);
                }
            }
    
            if (mCursor != null) {
                mCursor.close();
                mCursor = null;
            }
        }
    }
     

     


  4. 短信處理類
    package com.javen.service;
    
    import android.content.ContentValues;
    import android.content.Context;
    import android.net.Uri;
    import android.os.Handler;
    import android.os.Message;
    
    /**
     * @author Javen
     * 
     *         短信的處理
     * 
     */
    public class SmsHandler extends Handler {
        private Context mcontext;
    
        public SmsHandler(Context context) {
            this.mcontext = context;
        }
    
        @Override
        public void handleMessage(Message msg) {
            SmsInfo smsInfo = (SmsInfo) msg.obj;
    
            if (smsInfo.action == 1) {
                ContentValues values = new ContentValues();
                values.put("read", "1");
                mcontext.getContentResolver().update(
                        Uri.parse("content://sms/inbox"), values, "thread_id=?",
                        new String[] { smsInfo.thread_id });
            } else if (smsInfo.action == 2) {
                Uri mUri = Uri.parse("content://sms/");
                mcontext.getContentResolver().delete(mUri, "_id=?",
                        new String[] { smsInfo._id });
            }
        }
    }
     

     


  5. SmsInfo 數據結構主要用於短信攔截
    package com.javen.service;
    
    /**
     * 主要用於短信攔截
     * 
     * @author Javen
     * 
     */
    public class SmsInfo {
        public String _id = "";
        public String thread_id = "";
        public String smsAddress = "";
        public String smsBody = "";
        public String read = "";
        public int action = 0;// 1代表設置為已讀,2表示刪除短信
        @Override
        public String toString() {
            return "SmsInfo [_id=" + _id + ", thread_id=" + thread_id
                    + ", smsAddress=" + smsAddress + ", smsBody=" + smsBody
                    + ", read=" + read + ", action=" + action + "]";
        }
        
        
    }
     

     


  6. 需要的權限以及配置信息
    
    
        
        
         
         
         
         
        
            
                
                    
    
                    
                
            
            
             
                
                    
                    
                     
                
            
            
            
        
    
    
     

     

  7. 測試在MainActivity 中啟動SmsService 發送短信到10086 或者10010即可測試

     

            intentService = new Intent(this, SmsService.class);
            startService(intentService);
            Toast.makeText(this, "啟動service中.....", 1).show();
    
     
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved