2016年12月15日星期四

Android获取手机短信


  1. import android.content.pm.PackageManager;  
  2. import android.support.v4.app.ActivityCompat;  
  3. import android.support.v4.content.ContextCompat;  
  4. import android.support.v7.app.AppCompatActivity;  
  5. import android.os.Bundle;  
  6.   
  7. import android.app.Activity;  
  8. import android.database.ContentObserver;  
  9. import android.database.Cursor;  
  10. import android.net.Uri;  
  11. import android.os.Bundle;  
  12. import android.os.Handler;  
  13. import android.os.Message;  
  14. import android.widget.TextView;  
  15.   
  16. public class MainActivity extends AppCompatActivity {  
  17.   
  18.     private TextView vSms;//短信内容TextView  
  19.     private SMSContent smsObsever;//短信观察者  
  20.   
  21.     private Handler handler = new Handler() {  
  22.         public void handleMessage(android.os.Message msg) {  
  23.             Bundle bundle = msg.getData();  
  24.             String body = bundle.getString("body");  
  25.             vSms.setText(body);  
  26.         }  
  27.   
  28.         ;  
  29.     };  
  30.   
  31.     @Override  
  32.     protected void onCreate(Bundle savedInstanceState) {  
  33.         super.onCreate(savedInstanceState);  
  34.         setContentView(R.layout.activity_main);  
  35.         vSms = (TextView) this.findViewById(R.id.tx_sms);//短信内容显示  
  36.         smsObsever = new SMSContent(handler);//实例化短信观察者  
  37.         //注册短信观察者  
  38.         getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, smsObsever);  
  39.     }  
  40.   
  41.     /** 
  42.      * @author Administrator 
  43.      * @description 短信观察者 
  44.      */  
  45.     class SMSContent extends ContentObserver {  
  46.         private Handler mHandler;  
  47.   
  48.         public SMSContent(Handler handler) {  
  49.             super(handler);  
  50.             mHandler = handler;  
  51.         }  
  52.   
  53.         @Override  
  54.         public void onChange(boolean selfChange) {  
  55.             super.onChange(selfChange);  
  56.             Cursor cursor = null;  
  57.             String body = null;  
  58.   
  59.             //申请读取短信的权限,需要与用户交互  
  60.             final int REQUEST_CODE_ASK_PERMISSIONS = 123;  
  61.             ActivityCompat.requestPermissions(MainActivity.thisnew String[]{"android.permission.READ_SMS"}, REQUEST_CODE_ASK_PERMISSIONS);  
  62.   
  63.             //读取之前判断一下是否已经渠道权限  
  64.             if (ContextCompat.checkSelfPermission(getBaseContext(), "android.permission.READ_SMS") == PackageManager.PERMISSION_GRANTED) {  
  65.                 try {  
  66.                     cursor = getContentResolver().query(  
  67.                             Uri.parse("content://sms/inbox"), nullnullnull,  
  68.                             "date desc");  
  69.                     if (cursor != null) {  
  70.                         if (cursor.moveToNext()) {//不遍历只拿当前最新的一条短信  
  71.                             //获取当前的短信内容  
  72.                             body = cursor.getString(cursor.getColumnIndex("body"));  
  73.                             Message msg = Message.obtain();  
  74.                             Bundle bundle = new Bundle();  
  75.                             bundle.putString("body", body);  
  76.                             msg.setData(bundle);  
  77.                             mHandler.sendMessage(msg);  
  78.                         }  
  79.   
  80.                     }  
  81.                 } catch (Exception e) {  
  82.                     e.printStackTrace();  
  83.                 } finally {  
  84.                     if (cursor != null) {  
  85.                         cursor.close();  
  86.                     }  
  87.   
  88.                 }  
  89.             }  
  90.         }  
  91.     }  
  92.   
  93.     @Override  
  94.     protected void onDestroy() {  
  95.         super.onDestroy();  
  96.         //取消注册  
  97.         getContentResolver().unregisterContentObserver(smsObsever);  
  98.     }  



  1. <!--收短信的权限-->  
  2. <uses-permission android:name="android.permission.RECEIVE_SMS"/>  
  3. <!--读取短信信息的权限-->  
  4. <uses-permission android:name="android.permission.READ_SMS"/>  

没有评论:

发表评论