"Capacitor android plugin example" Code Answer's
You're definitely familiar with the best coding language Whatever that developers use to develop their projects and they get all their queries like "Capacitor android plugin example" answered properly. Developers are finding an appropriate answer about Capacitor android plugin example related to the Whatever coding language. By visiting this online portal developers get answers concerning Whatever codes question like Capacitor android plugin example. Enter your desired code related query in the search bar and get every piece of information about Whatever code related question on Capacitor android plugin 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<Map> contactList = new ArrayList<>();
ContentResolver cr = this.getContext().getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
Map<String,String> map = new HashMap<String, String>();
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
map.put("firstName", name);
map.put("lastName", "");
String contactNumber = "";
if (cur.getInt(cur.getColumnIndex( ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
pCur.moveToFirst();
contactNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i("phoneNUmber", "The phone number is "+ contactNumber);
}
map.put("telephone", contactNumber);
contactList.add(map);
}
}
if (cur != null) {
cur.close();
}
JSONArray jsonArray = new JSONArray(contactList);
JSObject ret = new JSObject();
ret.put("results", jsonArray);
call.success(ret);
}
}
Source: devdactic.com
All those coders who are working on the Whatever based application and are stuck on Capacitor android plugin example can get a collection of related answers to their query. Programmers need to enter their query on Capacitor android plugin example related to Whatever code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about Capacitor android plugin example for the programmers working on Whatever code while coding their module. Coders are also allowed to rectify already present answers of Capacitor android plugin example while working on the Whatever language code. Developers can add up suggestions if they deem fit any other answer relating to "Capacitor android plugin example". Visit this developer's friendly online web community, CodeProZone, and get your queries like Capacitor android plugin example resolved professionally and stay updated to the latest Whatever updates.