您好,登錄后才能下訂單哦!
這篇文章主要介紹“Android使用ContentProvider怎么實(shí)現(xiàn)查看系統(tǒng)短信功能”,在日常操作中,相信很多人在Android使用ContentProvider怎么實(shí)現(xiàn)查看系統(tǒng)短信功能問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Android使用ContentProvider怎么實(shí)現(xiàn)查看系統(tǒng)短信功能”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
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+="手機(jī)號(hào)碼:"+list.get(i).getAddress()+"\n"; text+="短信內(nèi)容:"+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,"無(wú)權(quán)限",Toast.LENGTH_SHORT).show(); } } } } }
到此,關(guān)于“Android使用ContentProvider怎么實(shí)現(xiàn)查看系統(tǒng)短信功能”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。