获取手机短信 博客分类: Android android
程序员文章站
2024-02-16 21:39:40
...
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"/>
推荐阅读
-
获取手机短信 博客分类: Android android
-
Java获取客户端真实IP地址 博客分类: JavaSE JavaJSP应用服务器浏览器Apache
-
android如何获取手机联系人的数据库示例代码
-
git ss代理配置 博客分类: Android gitss
-
使用aapt工具获取android package信息 博客分类: Android android
-
Android Server Push - GCM 博客分类: Android
-
使用aapt工具获取android package信息 博客分类: Android android
-
Android - Device Administration 博客分类: Android
-
获取手机短信 博客分类: Android android
-
git ss代理配置 博客分类: Android gitss