获取手机短信

在做手机应用中,常常会碰到要求获取手机短信需求。所以记录下来,有需要的可以一看~!

因为获取短信肯定不止一条,所以我们定义的一个方法,获取返回一个list.

然后liist里面装一个javabean也就是实体类,我们创建一个实体类

public class SMSItem {
    private String phonenumber = null;
    private String content = null;
    private String time = null;
    private int type = -1;

    public SMSItem(String phonenumber, String content, String time, int type) {
        super();
        this.phonenumber = phonenumber;
        this.content = content;
        this.time = time;
        this.type = type;
    }

}get,和set方法我就省去了。

定义一个返回list方法接收

public static ArrayList<SMSItem> getSMS(Context context) {
    final String SMS_URI_ALL = "content://sms/";
    ArrayList<SMSItem> smeArrayList = new ArrayList<SMSItem>();
    try {
        Uri uri = Uri.parse(SMS_URI_ALL);
        String[] projection = new String[]{"_id", "address", "person", "body", "date", "type"};
 Cursor cur = context.getContentResolver().query(uri, projection, "date<=" + System.currentTimeMillis(), null, "date desc");
        // 获取手机内部短信
        if (null != cur && cur.moveToFirst()) {
            int indexAddress = cur.getColumnIndex("address");
            int indexBody = cur.getColumnIndex("body");
            int indexDate = cur.getColumnIndex("date");
            int indexType = cur.getColumnIndex("type");
            cur.getColumnIndex(Sms.PERSON);
            do {
                String strAddress = cur.getString(indexAddress);
                String strbody = cur.getString(indexBody);
                long longDate = cur.getLong(indexDate);
                int intType = cur.getInt(indexType);
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                Date d = new Date(longDate);
                String strDate = dateFormat.format(d);
                smeArrayList.add(new SMSItem(strAddress, strbody, strDate, intType));
            } while (cur.moveToNext());
            if (!cur.isClosed()) {
                cur.close();
            }
        }// end if
    } catch (SQLiteException ex) {
        ex.printStackTrace();
    }
    return smeArrayList;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取手机短信验证码需要使用 Android 的 SMS 模块,代码如下: 1. 在 AndroidManifest.xml 文件中添加以下权限: ``` <uses-permission android:name="android.permission.READ_SMS" /> <uses-permission android:name="android.permission.RECEIVE_SMS" /> ``` 2. 在 Activity 中注册 BroadcastReceiver,用来接收短信: ``` private BroadcastReceiver smsReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) { 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++) { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); } String messageBody = messages[0].getMessageBody(); // 在这里获取短信验证码并进行处理 } } } } }; ``` 3. 在 Activity 的 onStart() 方法中注册 BroadcastReceiver: ``` @Override protected void onStart() { super.onStart(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED"); registerReceiver(smsReceiver, intentFilter); } ``` 4. 在 Activity 的 onStop() 方法中取消注册 BroadcastReceiver: ``` @Override protected void onStop() { super.onStop(); unregisterReceiver(smsReceiver); } ``` 注意:在获取短信验证码时要注意权限问题,另外不同手机型号上获取短信验证码的方式可能会有所不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值