`
fanfq
  • 浏览: 263643 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

获取手机短信

阅读更多

import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.app.Activity;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView vSms;//短信内容TextView
    private SMSContent smsObsever;//短信观察者

    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            Bundle bundle = msg.getData();
            String body = bundle.getString("body");
            vSms.setText(body);
        }

        ;
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vSms = (TextView) this.findViewById(R.id.tx_sms);//短信内容显示
        smsObsever = new SMSContent(handler);//实例化短信观察者
        //注册短信观察者
        getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, smsObsever);
    }

    /**
     * @author Administrator
     * @description 短信观察者
     */
    class SMSContent extends ContentObserver {
        private Handler mHandler;

        public SMSContent(Handler handler) {
            super(handler);
            mHandler = handler;
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            Cursor cursor = null;
            String body = null;

            //申请读取短信的权限,需要与用户交互
            final int REQUEST_CODE_ASK_PERMISSIONS = 123;
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{"android.permission.READ_SMS"}, REQUEST_CODE_ASK_PERMISSIONS);

            //读取之前判断一下是否已经渠道权限
            if (ContextCompat.checkSelfPermission(getBaseContext(), "android.permission.READ_SMS") == PackageManager.PERMISSION_GRANTED) {
                try {
                    cursor = getContentResolver().query(
                            Uri.parse("content://sms/inbox"), null, null, null,
                            "date desc");
                    if (cursor != null) {
                        if (cursor.moveToNext()) {//不遍历只拿当前最新的一条短信
                            //获取当前的短信内容
                            body = cursor.getString(cursor.getColumnIndex("body"));
                            Message msg = Message.obtain();
                            Bundle bundle = new Bundle();
                            bundle.putString("body", body);
                            msg.setData(bundle);
                            mHandler.sendMessage(msg);
                        }

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (cursor != null) {
                        cursor.close();
                    }

                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //取消注册
        getContentResolver().unregisterContentObserver(smsObsever);
    }
}





<!--收短信的权限-->
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<!--读取短信信息的权限-->
<uses-permission android:name="android.permission.READ_SMS"/>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics