Created
October 16, 2024 23:32
-
-
Save yasriady/6574bc2b575baf1e3ee2f90ee643e926 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@+id/main" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
tools:context=".MainActivity" | |
android:padding="16dp"> | |
<Button | |
android:id="@+id/readContactsButton" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Read Contacts" /> | |
<ListView | |
android:id="@+id/contactsListView" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" /> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools"> | |
<uses-permission android:name="android.permission.READ_CONTACTS"/> | |
<application | |
android:allowBackup="true" | |
android:dataExtractionRules="@xml/data_extraction_rules" | |
android:fullBackupContent="@xml/backup_rules" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:roundIcon="@mipmap/ic_launcher_round" | |
android:supportsRtl="true" | |
android:theme="@style/Theme.PhoneBook" | |
tools:targetApi="31"> | |
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.phonebook; | |
import android.content.pm.PackageManager; | |
import android.database.Cursor; | |
import android.os.Bundle; | |
import android.provider.ContactsContract; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.ArrayAdapter; | |
import android.widget.Button; | |
import android.widget.ListView; | |
import androidx.activity.EdgeToEdge; | |
import androidx.annotation.NonNull; | |
import androidx.appcompat.app.AppCompatActivity; | |
import androidx.core.app.ActivityCompat; | |
import androidx.core.content.ContextCompat; | |
import androidx.core.graphics.Insets; | |
import androidx.core.view.ViewCompat; | |
import androidx.core.view.WindowInsetsCompat; | |
import java.util.ArrayList; | |
public class MainActivity extends AppCompatActivity { | |
private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 100; | |
private ListView contactsListView; | |
private ArrayList<String> contacts; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
EdgeToEdge.enable(this); | |
setContentView(R.layout.activity_main); | |
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { | |
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); | |
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); | |
return insets; | |
}); | |
Button readContactsButton = findViewById(R.id.readContactsButton); | |
contactsListView = findViewById(R.id.contactsListView); | |
contacts = new ArrayList<>(); | |
readContactsButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.READ_CONTACTS) | |
!= PackageManager.PERMISSION_GRANTED) { | |
ActivityCompat.requestPermissions(MainActivity.this, | |
new String[]{android.Manifest.permission.READ_CONTACTS}, | |
PERMISSIONS_REQUEST_READ_CONTACTS); | |
} else { | |
readContacts(); | |
} | |
} | |
}); | |
} | |
public void onBtnReadContact(Button btn) { | |
Log.d("A", "B"); | |
} | |
private void readContacts() { | |
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); | |
if (cursor != null) { | |
while (cursor.moveToNext()) { | |
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); | |
String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); | |
contacts.add(name + ": " + phoneNumber); | |
} | |
cursor.close(); | |
} | |
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, contacts); | |
contactsListView.setAdapter(adapter); | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | |
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | |
if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS) { | |
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
readContacts(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment