阅读量:0
Android读取拨号记录功能
Android读取拨号记录功能
首先会检测应用是否有读取拨号记录的权限
MainActivity.java
public class MainActivity extends AppCompatActivity { private ListView listCalls; private List<Map<String, Object>> mapList; private static final int REQUEST_CODE = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); onShowCallLog(); } public void initView() { listCalls = (ListView) super.findViewById(R.id.call_list); SimpleAdapter simpleAdapter = new SimpleAdapter( this, mapList, R.layout.call_item, new String[]{CallLog.Calls.NUMBER, CallLog.Calls.DATE}, new int[]{R.id.call_mobile, R.id.call_date}); listCalls.setAdapter(simpleAdapter); } private void initDate() { ContentResolver contentResolver = getContentResolver(); Cursor cursor = contentResolver.query(CallLog.Calls.CONTENT_URI, new String[]{CallLog.Calls.NUMBER, CallLog.Calls.DATE}, null, null, null); mapList = new ArrayList<>(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); while (cursor.moveToNext()) { Map<String, Object> stringObjectMap = new HashMap<>(); stringObjectMap.put(CallLog.Calls.NUMBER, cursor.getString(0)); stringObjectMap.put(CallLog.Calls.DATE, simpleDateFormat.format(new Date(cursor.getLong(1)))); mapList.add(stringObjectMap); } cursor.close(); } private void onShowCallLog() { int checkCALL_LOGPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG); if (checkCALL_LOGPermission != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CALL_LOG}, REQUEST_CODE); } else { initDate(); initView(); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == REQUEST_CODE) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, "获取权限成功", Toast.LENGTH_SHORT).show(); initDate(); initView(); } else { Toast.makeText(this, "获取权限失败", Toast.LENGTH_SHORT).show(); this.finish(); } } else { super.onRequestPermissionsResult(requestCode, permissions, grantResults); } } }
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" tools:context=".MainActivity" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="号码" android:textSize="26sp" /> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="时间" android:textSize="26sp" /> </LinearLayout> <ListView android:id="@+id/call_list" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView> </LinearLayout>
call_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <TextView android:id="@+id/call_mobile" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:textSize="24sp" /> <TextView android:id="@+id/call_date" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:textSize="24sp" /> </LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="android.permission.READ_CALL_LOG"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.Learn"> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>