capacitor android install code example
Example: Capacitor android plugin example
package com.devdactic.contacts;
import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.util.Log;
import com.getcapacitor.JSObject;
import com.getcapacitor.NativePlugin;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import org.json.JSONArray;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@NativePlugin(
requestCodes={ContactsPlugin.REQUEST_CONTACTS}
)
public class ContactsPlugin extends Plugin {
protected static final int REQUEST_CONTACTS = 12345; // Unique request code
@PluginMethod()
public void getContacts(PluginCall call) {
String value = call.getString("filter");
// Filter based on the value if want
saveCall(call);
pluginRequestPermission(Manifest.permission.READ_CONTACTS, REQUEST_CONTACTS);
}
@Override
protected void handleRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.handleRequestPermissionsResult(requestCode, permissions, grantResults);
PluginCall savedCall = getSavedCall();
if (savedCall == null) {
Log.d("Test", "No stored plugin call for permissions request result");
return;
}
for(int result : grantResults) {
if (result == PackageManager.PERMISSION_DENIED) {
Log.d("Test", "User denied permission");
return;
}
}
if (requestCode == REQUEST_CONTACTS) {
// We got the permission!
loadContacts(savedCall);
}
}
void loadContacts(PluginCall call) {
ArrayList