I would like to get attachment from unread MMS messages, but the codes I have doesn't allow me to do so. How do I go about doing that?
Codes modified from here:
private void checkMMSMessages(){
// Create string arrays to store the queries later on
String[] columns = null;
String[] values = null;
// Calls the ContentResolver to query for columns with URI "content:mms"
Cursor curPdu = getContentResolver().query(Uri.parse("content://mms"), null, null, null, null);
if(curPdu.moveToNext()){
//String read = curRead.getString(curRead.getColumnIndex("read"));
// Gets ID of message
String id = curPdu.getString(curPdu.getColumnIndex("_id"));
// Gets thread ID of message
String thread_id = curPdu.getString(curPdu.getColumnIndex("thread_id"));
// Gets subject of message (if any)
String subject = curPdu.getString(curPdu.getColumnIndex("sub"));
// Gets date of message
String date = curPdu.getString(curPdu.getColumnIndex("date"));
String selectionAddr = new String ("msg_id = '" + id + "'");
Uri uriAddr = Uri.parse ("content://mms/" + id + "/addr");
Cursor curAddr = getContentResolver().query(uriAddr, null, null, null, null);
if(curAddr.moveToNext()){
String contact_id = curAddr.getString (curAddr.getColumnIndex ("contact_id"));
String address = curAddr.getString (curAddr.getColumnIndex ("address"));
String selectionPart = new String ("mid = '" + id + "'");
Cursor curPart = getContentResolver ().query(Uri.parse ("content://mms/part"), null, null, null, null);
//Cursor curPart = context.getContentResolver ().query(Uri.parse ("content://mms/" + id + "/part"), null, null, null, null);
while(curPart.moveToNext())
{
columns = curPart.getColumnNames();
if(values == null)
values = new String[columns.length];
for(int i=0; i< curPart.getColumnCount(); i++){
values[i] = curPart.getString(i);
}
String contact_idd = curPart.getString(0);
if(values[3].equals("image/jpeg") || values[3].equals("image/bmp") ||
values[3].equals("image/gif") || values[3].equals("image/jpg") ||
values[3].equals("image/png"))
{
GetMmsAttachment(values[0],values[12]);
//Toast.makeText(getApplicationContext(), "Retrieved MMS attachment", Toast.LENGTH_LONG);
}
}
}
}
}
private void GetMmsAttachment(String _id, String _data)
{
Uri partURI = Uri.parse("content://mms/part/" + _id );
String filePath = "/sdcard/photo.jpg";
InputStream is = null;
OutputStream picFile = null;
Bitmap bitmap = null;
try {
is = getContentResolver().openInputStream(partURI);
bitmap = BitmapFactory.decodeStream(is);
picFile = new FileOutputStream(filePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, picFile);
picFile.flush();
picFile.close();
}
catch (Exception e)
{
e.printStackTrace();
//throw new MmsException(e);
}
}
Figured out myself, the codes are as follows:
private void checkMMSMessages() {
String[] columns = null;
String[] values = null;
String read = "read = 0";
Cursor curPdu = getContentResolver().query(Uri.parse("content://mms"), null, read, null, null);
if(curPdu.moveToNext()){
String id = curPdu.getString(curPdu.getColumnIndex("_id"));
Cursor curPart = getContentResolver().query(Uri.parse ("content://mms/" + id + "/part"), null, null, null, null);
while(curPart.moveToNext())
{
columns = curPart.getColumnNames();
if(values == null)
values = new String[columns.length];
for(int i=0; i< curPart.getColumnCount(); i++){
values[i] = curPart.getString(i);
}
if(values[3].equals("image/jpeg") || values[3].equals("image/bmp") ||
values[3].equals("image/gif") || values[3].equals("image/jpg") ||
values[3].equals("image/png"))
{
GetMmsAttachment(values[0],values[12]);
}
}
}
}
private void GetMmsAttachment(String _id, String _data)
{
Uri partURI = Uri.parse("content://mms/part/" + _id );
String filePath = "/sdcard/photo.jpg";
InputStream is = null;
OutputStream picFile = null;
Bitmap bitmap = null;
try {
is = getContentResolver().openInputStream(partURI);
bitmap = BitmapFactory.decodeStream(is);
picFile = new FileOutputStream(filePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, picFile);
picFile.flush();
picFile.close();
}
catch (Exception e)
{
e.printStackTrace();
//throw new MmsException(e);
}
}
I think he asked how to retrieve the attachment from the server, as it is written UNREAD mms... If you have the column ct_l how to get the data from that internet address?
Related
The problem which i'm facing is I am not able to display the fetched value, as I have used a list view to display the values are not reaching it. This is my code snippet which fetches multiple contacts from phone book. Hope any1 help me solve it.
private void chooseContact() {
Intent phonebookIntent = new Intent("intent.action.INTERACTION_TOPMENU");
phonebookIntent.putExtra("additional", "phone-multi");
phonebookIntent.putExtra("maxRecipientCount", MAX_PICK_CONTACT);
phonebookIntent.putExtra("FromMMS", true);
startActivityForResult(phonebookIntent, REQUEST_CODE_PICK_CONTACT);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == RESULT_PICK_CONTACT) {
Uri contractData = data.getData();
ContactRetriever cr = new ContactRetriever(getApplicationContext(), contractData);
Person p = cr.getPerson();
if (p == null)
Toast.makeText(this, "Phone number not found!", Toast.LENGTH_SHORT).show();
else {
PersonManager.savePerson(p, getApplicationContext());
listContacts.setAdapter(new PersonAdapter(this, PersonManager.getSavedPersons(this)));
}
}
}
}}
And the Uri value is called and used in next below class
public class ContactRetriever {
private final String TAG = "ContactRetriever";
private ContentResolver cr;
private Context context;
private Uri contractData;
private String id;
public ContactRetriever(Context context,Uri contractData ) {
this.context = context;
this.cr = context.getContentResolver();
this.contractData = contractData;
}
public Person getPerson() {
String name = getName();
String num = getNumber();
if (name != null && num != null)
return new Person(getNumber(), getName());
else return null;
}
private String getNumber() {
String ret = null;
Cursor cId = cr.query(contractData, new String[]{ContactsContract.Contacts._ID}, null, null, null);
if (cId.moveToFirst()){
id = cId.getString(cId.getColumnIndex(ContactsContract.Contacts._ID));
Log.i(TAG + " IDs: ", id);
}
cId.close();
Cursor cNum = cr.query(contractData, null, null, null, null);
if (cNum.moveToNext()){
ret = cNum.getString(cNum.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i(ret, TAG + " NUMBERS: ");
}
return ret;
}
private String getName() {
String ret = null;
Cursor c = cr.query(contractData, null, null, null, null);
if (c.moveToFirst())
ret = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
c.close();
Log.i(TAG + "NAMES: ", ret);
return ret;
}
}
Here is my working code for mine
//enter code here
private void getAllContacts() {
List<InviteContactInfo> inviteContactList = new ArrayList();
InviteContactInfo inviteContactInfo;
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
inviteContactInfo = new InviteContactInfo();
inviteContactInfo.setContactName(name);
Cursor phoneCursor = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id},
null);
if (phoneCursor.moveToNext()) {
String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
inviteContactInfo.setContactNumber(phoneNumber);
}
phoneCursor.close();
Cursor emailCursor = contentResolver.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCursor.moveToNext()) {
String emailId = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
Bitmap photo =null;
try {
InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(),
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(id)));
if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
inviteContactInfo.setContactImage(photo);
}
else
{
photo =BitmapFactory.decodeResource(getResources(), R.drawable.profilepic);;
inviteContactInfo.setContactImage(photo);
}
assert inputStream != null;
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
inviteContactList.add(inviteContactInfo);
}
}
InviteContactAdapter contactAdapter = new InviteContactAdapter(getApplicationContext(), inviteContactList);
recList.setLayoutManager(new LinearLayoutManager(this));
recList.setAdapter(contactAdapter);
}
}
I am trying to get all the file name having audio files in it I have used Mediastore to get the mediastore audio,album,playlist and audio DATA also but how I can get the file or folder titles which contains the audio file .Here is the code that I have tried but it is not correct as I am not able to set the External_Content_uri.
This is the code I have tried.
private void External() {
try {
String[] proj = {MediaStore.Files.FileColumns.TITLE,
MediaStore.Files.FileColumns._ID,
MediaStore.Files.FileColumns.PARENT,
MediaStore.Files.FileColumns.DATA
};// Can include more data for more details and check it.
String selection =MediaStore.Files.FileColumns.MEDIA_TYPE+"=?";
String[] selectionArgs = {"MediaStore.Files.FileColumns.MEDIA_TYPE_AUDIO"};
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
Cursor audioCursor = getContentResolver().query(MediaStore.Files.getContentUri("\"external\""), proj, selection, selectionArgs, sortOrder);
if (audioCursor != null) {
if (audioCursor.moveToFirst()) {
do {
int filetitle = audioCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.TITLE);
int file_id = audioCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID);
int fileparent = audioCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.PARENT);
int filedata = audioCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA);
Mediafileinfo info = new Mediafileinfo();
info.setData(new File(new File(audioCursor.getString(filedata)).getParent()).getName());
info.setTitle(audioCursor.getString(filetitle));
info.set_id(audioCursor.getString(file_id));
info.setParent(audioCursor.getString(fileparent));
// info.setData(audioCursor.getString(filedata));
audioList.add(info);
} while (audioCursor.moveToNext());
}
}
assert audioCursor != null;
audioCursor.close();
} catch (Exception e) {
e.printStackTrace();
}
}
I tried this and this example but I am not able to get the solution.
Writing a fresh answer, since all you want is the folder names of the audio files.
So the best thing to use here is MediaStore.Audio.Media instead of using MediaStore.File. Get the folder names using the below on the audio file path.
new File(new File(audioCursor.getString(filedata)).getParent()).getName()
private void External() {
try {
Uri externalUri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
projection=new String[]{MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,;
String selection =null;
String[] selectionArgs = null;
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
Cursor audioCursor = getContentResolver().query(externalUri, proj, selection, selectionArgs, sortOrder);
if (audioCursor != null) {
if (audioCursor.moveToFirst()) {
do {
int filetitle = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
int file_id = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
int filePath = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
Mediafileinfo info = new Mediafileinfo();
info.setData(new File(new File(audioCursor.getString(filedata)).getParent()).getName());
info.setTitle(audioCursor.getString(filetitle));
info.set_id(audioCursor.getString(file_id));
audioList.add(info);
} while (audioCursor.moveToNext());
}
}
assert audioCursor != null;
audioCursor.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Hi query media file like audio,images and video using Android Media Store and Android Content Resolver, check this tutorial
http://www.androiddevelopersolutions.com/2015/12/android-media-store-tutorial-list-all.html
For Listing All images:
private void parseAllImages() {
try {
String[] projection = {MediaStore.Images.Media.DATA};
cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
null);
int size = cursor.getCount();
/******* If size is 0, there are no images on the SD Card. *****/
if (size == 0) {
} else {
int thumbID = 0;
while (cursor.moveToNext()) {
int file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
/**************** Captured image details ************/
/***** Used to show image on view in LoadImagesFromSDCard class ******/
String path = cursor.getString(file_ColumnIndex);
String fileName = path.substring(path.lastIndexOf("/") + 1, path.length());
MediaFileInfo mediaFileInfo = new MediaFileInfo();
mediaFileInfo.setFilePath(path);
mediaFileInfo.setFileName(fileName);
mediaFileInfo.setFileType(type);
mediaList.add(mediaFileInfo);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
For Listing all Video(.mp4)
private void parseAllVideo() {
try {
String name = null;
String[] thumbColumns = {MediaStore.Video.Thumbnails.DATA,
MediaStore.Video.Thumbnails.VIDEO_ID};
int video_column_index;
String[] proj = {MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE};
Cursor videocursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
proj, null, null, null);
int count = videocursor.getCount();
Log.d("No of video", "" + count);
for (int i = 0; i < count; i++) {
MediaFileInfo mediaFileInfo = new MediaFileInfo();
video_column_index = videocursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
videocursor.moveToPosition(i);
name = videocursor.getString(video_column_index);
mediaFileInfo.setFileName(name);
int column_index = videocursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
videocursor.moveToPosition(i);
String filepath = videocursor.getString(column_index);
mediaFileInfo.setFilePath(filepath);
mediaFileInfo.setFileType(type);
mediaList.add(mediaFileInfo);
// id += " Size(KB):" +
// videocursor.getString(video_column_index);
}
videocursor.close();
} catch (Exception e) {
e.printStackTrace();
}
}
For Listing All Audio
private void parseAllAudio() {
try {
String TAG = "Audio";
Cursor cur = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
null);
if (cur == null) {
// Query failed...
Log.e(TAG, "Failed to retrieve music: cursor is null :-(");
}
else if (!cur.moveToFirst()) {
// Nothing to query. There is no music on the device. How boring.
Log.e(TAG, "Failed to move cursor to first row (no query results).");
}else {
Log.i(TAG, "Listing...");
// retrieve the indices of the columns where the ID, title, etc. of the song are
// add each song to mItems
do {
int artistColumn = cur.getColumnIndex(MediaStore.Audio.Media.ARTIST);
int titleColumn = cur.getColumnIndex(MediaStore.Audio.Media.TITLE);
int albumColumn = cur.getColumnIndex(MediaStore.Audio.Media.ALBUM);
int durationColumn = cur.getColumnIndex(MediaStore.Audio.Media.DURATION);
int idColumn = cur.getColumnIndex(MediaStore.Audio.Media._ID);
int filePathIndex = cur.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
Log.i(TAG, "Title column index: " + String.valueOf(titleColumn));
Log.i(TAG, "ID column index: " + String.valueOf(titleColumn));
Log.i("Final ", "ID: " + cur.getString(idColumn) + " Title: " + cur.getString(titleColumn) + "Path: " + cur.getString(filePathIndex));
MediaFileInfo audio = new MediaFileInfo();
audio.setFileName(cur.getString(titleColumn));
audio.setFilePath(cur.getString(filePathIndex));
audio.setFileType(type);
mediaList.add(audio);
} while (cur.moveToNext());
}
} catch (Exception e) {
e.printStackTrace();
}
}
Background
I've found that there is a way to open a specific contact conversation screen on WhatsApp, here .
Not only that, but I've found that an app called "Drupe" does the same, and maybe even more :
https://lh3.googleusercontent.com/EQrs1jplMlP8SkOTdpqT4NzmgzGa5Wz2qageG1Pkjc6rKg0HBb-rwlOVW07_G7bAWgo=h900
The problem
I can't find any official API of opening it this way, so I'm not sure how safe it is.
I've found SDKs, but not intents instructions.
The questions
I'd like to know more about what's available for various social-networks and chatting apps :
WhatsApp
Facebook Messenger
Viber
Line
Telegram
Hangouts
Possible features may be:
open the conversation of a contact, when input is his phone number
have a new text that will be ready to be sent in the new screen
for Facebook, maybe also be able to open using the Facebook-ID of the person (meaning this is the input), instead of a phone number.
Are such features available for each of those social networks and chatting apps?
For Facebook-messenger, I've found this (from https://developers.facebook.com/docs/messenger-platform/discovery/m-me-links#format):
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.me/" + facebookId));
It works, but I wonder if there is another way to access it (using phone number, for example).
For WhatsApp, I've found this (from here) :
final String formattedPhoneNumber = getFormattedPhoneNumber(this, phone);
final String contactId = getContactIdFromPhoneNumber(phone);
final String contactMimeTypeDataId = getContactMimeTypeDataId(contactId, "vnd.android.cursor.item/vnd.com.whatsapp.profile");
if (contactMimeTypeDataId != null) {
intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + formattedPhoneNumber));
intent.setPackage("com.whatsapp");
} else
Toast.makeText(this, "cannot find this contact on whatsapp", Toast.LENGTH_SHORT).show();
public static String getFormattedPhoneNumber(Context context, String input) {
final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
String normalizedPhone = input.replaceAll("[^0-9+]", "");
try {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String countryCode = tm.getSimCountryIso();
final PhoneNumber phoneNumber = phoneNumberUtil.parse(normalizedPhone, countryCode.toUpperCase());
final String formattedPhoneNumber = phoneNumberUtil.format(phoneNumber, PhoneNumberFormat.E164).replaceAll("[^0-9]", "");
return formattedPhoneNumber;
} catch (NumberParseException e) {
e.printStackTrace();
}
return null;
}
private String getContactIdFromPhoneNumber(String phone) {
if (TextUtils.isEmpty(phone))
return null;
final Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));
final ContentResolver contentResolver = getContentResolver();
final Cursor phoneQueryCursor = contentResolver.query(uri, new String[]{PhoneLookup._ID}, null, null, null);
if (phoneQueryCursor != null) {
if (phoneQueryCursor.moveToFirst()) {
String result = phoneQueryCursor.getString(phoneQueryCursor.getColumnIndex(PhoneLookup._ID));
phoneQueryCursor.close();
return result;
}
phoneQueryCursor.close();
}
return null;
}
public String getContactMimeTypeDataId(#NonNull Context context, String contactId, #NonNull String mimeType) {
if (TextUtils.isEmpty(mimeType))
return null;
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI, new String[]{Data._ID}, Data.MIMETYPE + "= ? AND "
+ ContactsContract.Data.CONTACT_ID + "= ?", new String[]{mimeType, contactId}, null);
if (cursor == null)
return null;
if (!cursor.moveToFirst()) {
cursor.close();
return null;
}
String result = cursor.getString(cursor.getColumnIndex(Data._ID));
cursor.close();
return result;
}
It works, but it doesn't add the message. It also might say the contact doesn't have WhatsApp.
It's also possible to just use the phone number, as I wrote here.
For Viber, I've found this (from here) :
final String contactId = getContactIdFromPhoneNumber(phone);
final String contactMimeTypeDataId = getContactMimeTypeDataId(contactId, "vnd.android.cursor.item/vnd.com.viber.voip.viber_number_message");
if (contactMimeTypeDataId != null) {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/data/" + contactMimeTypeDataId));
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
intent.setPackage("com.viber.voip");
} else {
intent = new Intent("android.intent.action.VIEW", Uri.parse("tel:" + Uri.encode(formattedPhoneNumber)));
intent.setClassName("com.viber.voip", "com.viber.voip.WelcomeActivity");
}
private String getContactIdFromPhoneNumber(String phone) {
final Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));
final ContentResolver contentResolver = getContentResolver();
final Cursor phoneQueryCursor = contentResolver.query(uri, new String[]{PhoneLookup._ID}, null, null, null);
if (phoneQueryCursor != null) {
if (phoneQueryCursor.moveToFirst()) {
String result = phoneQueryCursor.getString(phoneQueryCursor.getColumnIndex(PhoneLookup._ID));
phoneQueryCursor.close();
return result;
}
phoneQueryCursor.close();
}
return null;
}
For Hangouts, it seems it's similar to Viber, but with this mimetype: "vnd.android.cursor.item/vnd.googleplus.profile.comm". Yet, it doesn't work as it probably needs additional steps (setting G+ to keep contacts updated and have the contacts in the G+ circles). However, I've somehow succeeded to open the video chat of a person:
intent =new Intent(Intent.ACTION_VIEW,Uri.parse("content://com.android.contacts/data/"+contactMimeTypeDataId));
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT |Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET |Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
For Telegram, someone (here) suggested using the next code, but it doesn't work:
intent = new Intent(android.content.Intent.ACTION_SENDUri.parse("http://telegram.me/"+profile)));
intent.setPackage("org.telegram.messenger");
It's also possible to just use the phone number, as I wrote here.
For Line, I've found these (based on here and here), but none work:
Intent intent = new Intent("jp.naver.line.android.intent.action.LINESHORTCUT");
intent.putExtra("shortcutType", "chatmid");
intent.putExtra("shortcutTargetId", target);
intent.putExtra("shortcutTargetName", "");
intent.putExtra("shortcutFromOS", false);
startActivity(intent);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("line://msg/text/" + getMongon()));
skype: this one works (found from various links, here, for example):
final String skypeUserName = getSkypeUserName(phone);
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("skype:" + skypeUserName + "?chat"));
public String getSkypeUserName(String phoneNumber) {
if (TextUtils.isEmpty(phoneNumber))
return null;
ContentResolver cr = getContentResolver();
final Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = cr.query(uri, new String[]{PhoneLookup.LOOKUP_KEY}, null, null, null);
if (cursor == null)
return null;
final Set<String> contactKeys = new HashSet<>();
// get contact keys
{
final int contactKeyIdx = cursor.getColumnIndex(PhoneLookup.LOOKUP_KEY);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
String contactKey = cursor.getString(contactKeyIdx);
contactKeys.add(contactKey);
}
cursor.close();
}
if (contactKeys.isEmpty())
return null;
//get raw ids
final Set<String> contactRawIdsSet = new HashSet<>();
{
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < contactKeys.size(); ++i)
sb.append(sb.length() == 0 ? "?" : ",?");
String inParameters = sb.toString();
final String[] selectionArgs = contactKeys.toArray(new String[contactKeys.size()]);
cursor = cr.query(ContactsContract.Data.CONTENT_URI, new String[]{ContactsContract.Data.RAW_CONTACT_ID}, ContactsContract.Data.LOOKUP_KEY + " IN (" + inParameters + ")", selectionArgs, null);
if (cursor == null)
return null;
final int rawContactColIdx = cursor.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
String rawContactId = cursor.getString(rawContactColIdx);
contactRawIdsSet.add(rawContactId);
}
cursor.close();
}
if (contactRawIdsSet.isEmpty())
return null;
//find the skype name
//TODO think of a better way to query, as it looks weird to search within a set of ids...
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < contactRawIdsSet.size(); ++i)
sb.append(sb.length() == 0 ? "?" : ",?");
String inParameters = sb.toString();
final String[] selectionArgs = new String[2 + contactRawIdsSet.size()];
selectionArgs[0] = "com.skype.contacts.sync";
selectionArgs[1] = "vnd.android.cursor.item/name";
int i = 2;
for (String rawId : contactRawIdsSet)
selectionArgs[i++] = rawId;
cursor = cr.query(ContactsContract.Data.CONTENT_URI, new String[]{RawContacts.SOURCE_ID}, ContactsContract.RawContacts.ACCOUNT_TYPE + " = ? AND " + Data.MIMETYPE + " = ? AND " +
ContactsContract.Data.CONTACT_ID + " IN (" + inParameters + ")", selectionArgs, null);
if (cursor == null)
return null;
if (!cursor.moveToFirst()) {
cursor.close();
return null;
}
String result = cursor.getString(cursor.getColumnIndex(RawContacts.SOURCE_ID));
cursor.close();
return result;
}
it works for me
try {
String toNumber = "+91 8*******36"; // contains spaces.
toNumber = toNumber.replace("+", "").replace(" ", "");
Intent sendIntent = new Intent(Intent.ACTION_SENDTO,Uri.parse("smsto:" + "" + toNumber + "?body=" + ""));
sendIntent.putExtra(Intent.EXTRA_TEXT, "hello");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
}
catch (Exception e){
Toast.makeText(getActivity(),"it may be you dont have whats app",Toast.LENGTH_LONG).show();
}
Other posts here have good information. I wanted to add for LINE because the information is lacking in many places.
String userId = findUserId();
String sendText = "line://ti/p/~" + userId;
Intent intent = null;
try {
intent = Intent.parseUri(sendText, Intent.URI_INTENT_SCHEME);
} catch (URISyntaxException e) {
e.printStackTrace();
}
startActivity(intent);
I have an assynctask that will read all the content in the conversation box and it works well
protected String doInBackground(String... params) {
Uri inboxURI = Uri.parse("content://sms/conversations");
ContentResolver cr = getContentResolver();
Cursor c = cr.query(inboxURI, null, null, null, null);
c.moveToLast();
al = new ArrayList<MessageInfo>();
try{
for (int i = c.getCount(); i > 0; i--) {
MessageInfo mMessageInfo = new MessageInfo();
mMessageInfo.MessageText = c.getString(c.getColumnIndexOrThrow("snippet"));
mMessageInfo.ThreadId = c.getInt(c.getColumnIndex("thread_id"));
Uri uri = Uri.parse("content://sms/inbox");
String where = "thread_id=" + mMessageInfo.ThreadId;
Cursor Cursor = getContentResolver().query(uri, null, where,null, null);
startManagingCursor(Cursor);
//Cursor.moveToFirst();
String number = "";
String name = "";
if (Cursor.moveToFirst()) {
number = Cursor.getString(Cursor.getColumnIndexOrThrow("address")).toString();
name = GetNameAndNumber(Cursor, number, MessageBox.this);
Log.i("checkPoint","checkPoint-1 "+name);
}
else {
uri = Uri.parse("content://sms/sent");
where = "thread_id=" + mMessageInfo.ThreadId;
Cursor = GetCursor(uri, where, MessageBox.this);
if (Cursor.moveToFirst()) {
number = Cursor.getString(Cursor.getColumnIndexOrThrow("address")).toString();
name = GetNameAndNumber(Cursor, number, MessageBox.this);
Log.i("checkPoint","checkPoint-2 "+name);
}Cursor.close();
}
if (name.length() > 0) {
mMessageInfo.Name = name;
} else {
mMessageInfo.Name = number;
}
mMessageInfo.Number = number;
Log.i("name",name);
Log.i("number",number);
Log.i("mMessageInfo.ThreadId ",""+mMessageInfo.ThreadId );
al.add(mMessageInfo);
c.moveToPrevious();
}c.close();
}catch(Exception x){x.printStackTrace();}
return "Executed";
}
But if my app writes to content://sms/sent the above code gives null exception but the stock messaging app can read it well.
This is the method how I update my sent items
void sentBox(){
ContentValues values = new ContentValues();
values.put("address", number);
values.put("body", msgBox.getText().toString());
getContentResolver().insert(Uri.parse("content://sms/sent"), values);
finish();
}
To be specific I cannot get the address of the message in the sent items.. below returns null exception.. I think there is something to do with my URI.
if (Cursor.moveToFirst())
number = Cursor.getString(Cursor.getColumnIndexOrThrow("address")).toString();
Can someone give me hint why it can't read if I write/update my sent box that way?
I've Manage to fix the problem. It was another method which is writing to sent box using the message ID where there is another method writing to sent box using the address..
protected String doInBackground(String... params) {
al = new ArrayList<MessageInfo>();
String number = "";
String name = "";
Uri conversationURI = Uri.parse("content://sms/conversations");
ContentResolver cr = getContentResolver();
Cursor c = cr.query(conversationURI, null, null, null, null);
c.moveToLast();
for (int i = c.getCount(); i > 0; i--) {
MessageInfo mMessageInfo = new MessageInfo();
mMessageInfo.MessageText = c.getString(c.getColumnIndexOrThrow("snippet"));
mMessageInfo.ThreadId = c.getInt(c.getColumnIndex("thread_id"));
try{
Uri message = Uri.parse("content://sms/inbox");
ContentResolver cr_ = getContentResolver();
String where_ = "thread_id=" + mMessageInfo.ThreadId;
Cursor c_ = cr_.query(message, null, where_, null, null);
startManagingCursor(c_);
if (c_.moveToFirst() && c_ != null) {
number = c_.getString(c_.getColumnIndexOrThrow("address"));
name = GetNameAndNumber(c_, number, MessageBox.this);
}else{
Uri _message_ = Uri.parse("content://sms/sent");
ContentResolver _cr_ = getContentResolver();
String _where_ = "thread_id=" + mMessageInfo.ThreadId;
Cursor _c_ = _cr_.query(_message_, null, _where_, null, null);
if (_c_.moveToFirst() && _c_ != null) {
number = _c_.getString(_c_.getColumnIndexOrThrow("address"));
name = GetNameAndNumber(_c_, reformatNumber(number), MessageBox.this);
}
}
}catch(Exception x){x.printStackTrace();}
if (name.length() > 0) {
mMessageInfo.Name = name;
} else {
mMessageInfo.Name = number;
}
mMessageInfo.Number = number;
al.add(mMessageInfo);
c.moveToPrevious();
}c.close();
return null;
}
Changed this FROM:
void sentBox(){
ContentValues values = new ContentValues();
values.put("thread_id", getIntent().getExtras().getInt("ID"));
values.put("body", mMessageEditText.getText().toString());
getContentResolver().insert(Uri.parse("content://sms/sent"), values);
}
TO:
void sentBox(){
ContentValues values = new ContentValues();
values.put("address", getIntent().getExtras().getString("number"));
values.put("body", mMessageEditText.getText().toString());
}
EDIT:A list of what I consider important contact details:
1.NAME
2.PHONE NUMBER
3.EMAIL ADDRESS
4.WEBSITE
5.PHYSICAL ADDRESS
I would prefer to do this using a pre-fetched contactId...using only one cursor to get all of the data specified.I,preferably would like to find the right query to do this:
I would like to get all of the important details of a Contact at once,I am using the following code to do this:
public void getAllDataByContactId(int contactId)
{
Log.d(TAG, "Seriously scared it might not work");
String phoneNo="Phone disconnected";
String email="Email could not be delivered";
String website="Website 404";
String address="Number 13,Dark Street,Area 51,Bermuda Trianlge";
String name="Clint Eastwood";
int hasPhoneNumber;
String selection=ContactsContract.Data.CONTACT_ID+"=?";
String[] selectionArgs={String.valueOf(contactId)};
Cursor c=context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null,selection, selectionArgs,ContactsContract.Data.TIMES_CONTACTED);
if(c!=null && c.getCount()>0)
{
while(c.moveToNext())
{
phoneNo=c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.d(TAG, "Phone number: "+phoneNo);
email=c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
Log.d(TAG, "Email: "+email);
website=c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL));
Log.d(TAG, "Website :"+website);
address=c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
name=c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
Log.d(TAG, "Name :"+name);
}
}
}
However,although this does not throw an error it shows many rows consisting of an empty string interspresed with the actual values.How do I write a query that cuts out the noise?
I have tried this and this gets me all the values:
String selection=ContactsContract.Data.CONTACT_ID+"=? AND "+ContactsContract.Data.MIMETYPE+"=? OR "+ContactsContract.Data.MIMETYPE+"=? OR "+ContactsContract.Data.MIMETYPE+"=? OR "+ContactsContract.Data.MIMETYPE+"=? OR "+ContactsContract.Data.MIMETYPE+"=?";
String[] selectionArgs={String.valueOf(contactId),ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE,ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE,ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
Too late to answer, but maybe it can help someone in the future.
My solution for this question with only one while cycle and query:
private void fetchContacts(ContentResolver contentResolver) {
if (contentResolver == null) return;
Cursor cursor = contentResolver.query(ContactsContract.Data.CONTENT_URI,
null, null, null, null);
if (cursor == null || cursor.getCount() <= 0) {
return;
}
String prevId = "";
String contactId = "";
PersonContact personContact = null;
while (cursor.moveToNext()) {
String company = "";
String columnName = cursor.getString(cursor.getColumnIndex("mimetype"));
if (columnName.equals(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)) {
company = cursor.getString(cursor.getColumnIndex("data1"));
}
String email = "";
if (columnName.equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
email = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
String phone = "";
if (columnName.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
phone = cursor.getString(cursor.getColumnIndex("data1"));
}
String first = "";
String last = "";
if (columnName.equals(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)) {
first = cursor.getString(cursor.getColumnIndex("data2"));
last = cursor.getString(cursor.getColumnIndex("data3"));
}
if (!prevId.equals(contactId)) {
if (!TextUtils.isEmpty(prevId)) {
addFilteredList(personContact);
allContacts.put(prevId, personContact);
}
prevId = contactId;
personContact = new PersonContact();
} else {
if (personContact != null) {
personContact.id = prevId;
if (TextUtils.isEmpty(personContact.company)) personContact.company = company;
if (TextUtils.isEmpty(personContact.firstName)) personContact.firstName = first;
if (TextUtils.isEmpty(personContact.lastName)) personContact.lastName = last;
if (!TextUtils.isEmpty(email) && personContact.emails.size() == 0) {
personContact.emails.add(email);
}
if (!TextUtils.isEmpty(phone) && personContact.phoneNumbers.size() == 0) {
personContact.phoneNumbers.add(phone);
}
}
}
contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
}
cursor.close();
}
As you can see, I used the prevId field, because cursor.moveToNext performs several times for one contact (once for first and last names, one for phone, etc.). After each iteration, I check the previous contact identifier with the current identifier and, if it is false, I update the fields in the personContact model.
May not be the best solution. But this is how I achieved it.
ArrayList<String> fnameList = new ArrayList<>();
ArrayList<String> lnameList = new ArrayList<>();
ArrayList<String> mnumList = new ArrayList<>();
ArrayList<String> hnumList = new ArrayList<>();
ArrayList<String> wnumList = new ArrayList<>();
ArrayList<String> mailList = new ArrayList<>();
final DynamoDBMapper dynamoDBMapper = AWSMobileClient.defaultMobileClient().getDynamoDBMapper();
final ContactsDO firstItem = new ContactsDO(); // Initialize the Notes Object
firstItem.setUserId(AWSMobileClient.defaultMobileClient().getIdentityManager().getCachedUserID());
String email = null;
Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
String _ID = ContactsContract.Contacts._ID;
String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
Uri EmailCONTENT_URI = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
String EmailCONTACT_ID = ContactsContract.CommonDataKinds.Email.CONTACT_ID;
String DATA = ContactsContract.CommonDataKinds.Email.DATA;
StringBuffer output = new StringBuffer();
ContentResolver contentResolver = this.getContentResolver();
Cursor cursor = contentResolver.query(CONTENT_URI, null, null, null, null);
// Loop for every contact in the phone
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
String contact_id = cursor.getString(cursor.getColumnIndex(_ID));
// Query and loop for every phone number of the contact
Cursor pCur = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{contact_id}, ContactsContract.CommonDataKinds.Phone.NUMBER);
int flag = 0;
assert pCur != null;
while (pCur.moveToNext()) {
String mobileNum = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
if (flag == 0) {
if(mobileNum!=null){
mnumList.add(mobileNum);}
} else if (flag == 1) {
if(mobileNum!=null){
hnumList.add(mobileNum);}
} else if (flag == 2) {
if(mobileNum!=null){
wnumList.add(mobileNum);}
}
flag++;
}
if(flag==1){
hnumList.add("");
wnumList.add("");
Log.e("Set","Both added");
}
if(flag==2){
wnumList.add("");
Log.e("Set","W added");
}
pCur.close();
}
}
}
cursor.close();
String MIME = ContactsContract.Data.MIMETYPE + "=?";
String[] params = new String[]{ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};
final Cursor nameCur = contentResolver.query(
ContactsContract.Data.CONTENT_URI,
null,
MIME,
params,
ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME);
assert nameCur != null;
int i = 0;
while (nameCur.moveToNext()){
String fname = "";
String lname = "";
fname = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
lname = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
Log.e("In While","All the time");
if(fname!=null){
fnameList.add(fname);
Log.e("Put","Value Fname "+fname);}
if(lname!=null) {
lnameList.add(lname);
Log.e("Put","Value Lname "+lname);
}
if(fname==null){
fnameList.add(" ");
}
if(lname==null){
lnameList.add(" ");
}
i++;
}
nameCur.close();
Cursor cursorB = contentResolver.query(CONTENT_URI, null, null, null, null);
// Loop for every contact in the phone
if (cursorB.getCount() > 0) {
while (cursorB.moveToNext()) {
// Query and loop for every email of the contact
String[] paramEmail = new String[]{ContactsContract.CommonDataKinds.Email.CONTENT_TYPE};
Cursor emailCursor = contentResolver.query(EmailCONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", paramEmail, ContactsContract.CommonDataKinds.Email.DISPLAY_NAME);
int j=0;
while (emailCursor.moveToNext()) {
email = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
mailList.add(email);
Log.e("Email",email);
j++;
}
if(j==0){
mailList.add("");
Log.e("Email","Dummy Added");
}
emailCursor.close();
output.append("\n");
}
}cursorB.close();
Cursor cursorD = contentResolver.query(CONTENT_URI, null, null, null, null);
// Loop for every contact in the phone
if (cursorD.getCount() > 0) {
while (cursorD.moveToNext()) {
String contact_id = cursorD.getString(cursorD.getColumnIndex(_ID));
//for url
String newNoteUrl = "";
String whereName3 = ContactsContract.Data.MIMETYPE + " = ?";
String[] whereNameParams3 = new String[]{ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE};
ContentResolver contentResolverUrl = this.getContentResolver();
try {
Cursor cursorUrl = contentResolverUrl.query(ContactsContract.Data.CONTENT_URI, null, whereName3, new String[]{contact_id}, ContactsContract.CommonDataKinds.Website.URL);
while (cursorUrl.moveToNext()) {
newNoteUrl = cursorUrl.getString(cursorUrl.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL));
Log.e("URL",newNoteUrl);
}
Log.e("URL","Not Getting");
output.append("\nurl " + newNoteUrl);
firstItem.setUrl(newNoteUrl);
cursorUrl.close();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}cursorD.close();
Log.e("#######","##########################");
for(int m=0;m<fnameList.size();m++){
Log.e("Contact Val ",fnameList.get(m)+" , "+lnameList.get(m)+" , "+mnumList.get(m)+" , "+hnumList.get(m)+" , "+wnumList.get(m)+" , "+mailList.get(m));
ContactsDO item = new ContactsDO();
item.setUserId(AWSMobileClient.defaultMobileClient().getIdentityManager().getCachedUserID());
item.setFirstName(fnameList.get(m));
item.setLastName(lnameList.get(m));
item.setMobileNumber(mnumList.get(m));
item.setHomeNumber(hnumList.get(m));
item.setWorkNumber(wnumList.get(m));
item.setEmail(mailList.get(m));
try {
//saving to the database
dynamoDBMapper.save(item);
} catch (final AmazonClientException ex) {
Log.e(TAG, "Failed saving item : " + ex.getMessage(), ex);
}
}