您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍“Android使用ContentProvider怎么实现查看系统短信功能”,在日常操作中,相信很多人在Android使用ContentProvider怎么实现查看系统短信功能问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Android使用ContentProvider怎么实现查看系统短信功能”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:id="@+id/tv_show" android:layout_width="match_parent" android:layout_height="0dp" android:textSize="20dp" android:layout_weight="9"></TextView> <Button android:id="@+id/bt_show" android:layout_weight="1" android:text="查看短信" android:onClick="show" android:layout_width="match_parent" android:layout_height="0dp"></Button> </LinearLayout>
Sms.java
public class Sms { private int id; private String address; private String body; public Sms(int id, String address, String body) { this.id = id; this.address = address; this.body = body; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } }
MainActivity.java
public class MainActivity extends AppCompatActivity { private TextView tv_show; private Button bt_show; private List<Sms> list=new ArrayList<>(); private String text=""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_show=findViewById(R.id.tv_show); bt_show=findViewById(R.id.bt_show); } public void show(View view) { ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.READ_SMS},1); } public void getSms(){ Uri uri= Uri.parse("content://sms/"); ContentResolver contentResolver=getContentResolver(); Cursor cursor = contentResolver.query(uri, new String[]{"_id", "address", "body"}, null, null, null); if (cursor!=null&&cursor.getCount()>0){ if (list!=null){ list.clear(); } text=""; while (cursor.moveToNext()){ int id=cursor.getInt(0); String name=cursor.getString(1); String body=cursor.getString(2); Sms sms=new Sms(id,name,body); list.add(sms); } for (int i=0;i<list.size();i++){ text+="手机号码:"+list.get(i).getAddress()+"\n"; text+="短信内容:"+list.get(i).getBody()+"\n\n"; } tv_show.setText(text); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode==1){ for (int i=0;i<permissions.length;i++){ if (grantResults[i]== PackageManager.PERMISSION_GRANTED){ getSms(); }else { Toast.makeText(MainActivity.this,"无权限",Toast.LENGTH_SHORT).show(); } } } } }
到此,关于“Android使用ContentProvider怎么实现查看系统短信功能”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。