How do I play the "Positive" alarm sound? I know how to play the default one...
Ringtone ringtone = RingtoneManager.getRingtone(getApplicationContext(), RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM));
if (ringtone != null) {
ringtone.play();
}
EDIT:
Tried the below but the cursor just returns the same uri???
RingtoneManager rm = new RingtoneManager(getApplicationContext());
rm.setType(RingtoneManager.TYPE_ALARM);
Cursor c = rm.getCursor();
c.moveToFirst();
if (!c.isAfterLast()) {
do {
int uriIndex = c.getInt(RingtoneManager.URI_COLUMN_INDEX);
Uri ring = rm.getRingtoneUri(uriIndex);
Log.d("TC", ring.toString());
} while (c.moveToNext());
}
The RingtoneManager.getCursor() function provides a cursor that will let you iterate over all the ringtones and you can check the ringtone title for a matching name. The uri's returned depend on where the ringtone was stored but once the right one is found you can save the uri as a preference. Code I have (using a preference) doesn't do anything very fancy:
Uri alert;
String alarmname = mPrefs.getString(getString(R.string.pref_sound_key, null);
if (alarmname != null && !alarmname.equals(""))
alert = Uri.parse(alarmname);
elsewhere you can use the ringtone preferences to save a user choice and should be able to arrange the default to be "Positive" once you work out what the uri looks like.
RingtoneManager.URI_COLUMN_INDEX is a TEXT column.
RingtoneManager rm = new RingtoneManager(getApplicationContext());
rm.setType(RingtoneManager.TYPE_ALARM);
Cursor c = rm.getCursor();
c.moveToFirst();
if (!c.isAfterLast()) {
do {
int uriString = c.getString(RingtoneManager.URI_COLUMN_INDEX);
Log.d("TC", uriString);
} while (c.moveToNext());
}
Related
I am trying to use DocumentsContract to traverse a directory recursively using the following method.
void traverseDirectoryEntries(Uri rootUri) {
ContentResolver contentResolver = activityMain.getContentResolver();
Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(
rootUri, DocumentsContract.getTreeDocumentId(rootUri));
List<Uri> dirNodes = new LinkedList<>();
dirNodes.add(childrenUri);
while (!dirNodes.isEmpty()) {
childrenUri = dirNodes.remove(0); // get the item from top
try (Cursor cursor = contentResolver.query(childrenUri, new String[]{
DocumentsContract.Document.COLUMN_DOCUMENT_ID,
DocumentsContract.Document.COLUMN_DISPLAY_NAME,
DocumentsContract.Document.COLUMN_MIME_TYPE},
null, null, null)) {
if (cursor != null) {
while (cursor.moveToNext()) {
final String docId = cursor.getString(0);
final String name = cursor.getString(1);
final String mime = cursor.getString(2);
if (isDirectory(mime)) {
final Uri newNode = DocumentsContract.buildChildDocumentsUriUsingTree(
rootUri, docId);
traverseDirectoryEntries(newNode);
}
}
}
}
}
}
// Util method to check if the mime type is a directory
private static boolean isDirectory(String mimeType) {
return DocumentsContract.Document.MIME_TYPE_DIR.equals(mimeType);
}
Unfortunately, this does not work. It goes through the same root directory over and over despite the directory passed in being a child of the root. The line final Uri newNode = DocumentsContract.buildChildDocumentsUriUsingTree(rootUri, docId); returns a Uri with the child directory attached at the end. That works fine. The problem is the follwing line: Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(rootUri, DocumentsContract.getTreeDocumentId(rootUri)); where rootUri is the newNode. I get the same childrenUri; the one I got from rootUri. Is this a bug? Did I make a mistake? Is the docId wrong? I tried getDocumentId(rootUri), but that throws an error because the id it returns is not a tree. It is almost like getTreeDocumentId(rootUri) is returning what getRootId(rootUri) should be returning. Is there a way to fix this?
The rootUri was obtained via ACTION_OPEN_DOCUMENT_TREE. I am running this on Android Pie. I left some code out where I am building a tree structure from the data.
You are abusing this well known void traverseDirectoryEntries(Uri rootUri) function.
You think it is recursive but it is not.
Remove the recursive call and instead add 'newNode' to the list.
// traverseDirectoryEntries(newNode);
dirNodes.add(newNode);
Then you make use of the list.
Then original code is restored.
Who changed it without telling so?
Of course you can adapt this function to make it recursiv.
Nice exercise ;-).
I am developing an android app which backs up and restores the messages/conversations from device. It backup the messages, export file in the form of xml, and then later restore it. The only problem I am facing is the date/times of conversations. It is changed to current time at the time of restoration, but when I open any conversation, there time is correct. Have a look at photos.
Before backup:
After backup:
Code I am using for backup:
Uri uri = Uri.parse("content://sms/inbox");
//Uri uri = Uri.parse("content://mms-sms/conversations/");
ContentResolver contentResolver = getContentResolver();
final String[] projection = new String[]{"*"};
Cursor SMSL = contentResolver.query(Telephony.Sms.Inbox.CONTENT_URI, projection, null, null, null);
int msgscount = SMSL.getCount();
if (msgscount>0) {
msgs = new String[SMSL.getCount()][5];
int i = 0;
while (SMSL.moveToNext()) {
address = SMSL.getString(SMSL.getColumnIndex("address"));
body = SMSL.getString(SMSL.getColumnIndex("body"));
read = SMSL.getString(SMSL.getColumnIndex("read"));
date = SMSL.getString(SMSL.getColumnIndex("date"));
type = SMSL.getString(SMSL.getColumnIndex("type"));
msgs[i][0] = address;
msgs[i][1] = body;
msgs[i][2] = date;
msgs[i][3] = read;
msgs[i][4] = type;
Log.i("Date: ", String.valueOf(SMSL.getLong(SMSL.getColumnIndex("date"))));
i++;
}
SMSL.close();
}else{
msgs = new String[0][0];
Toast.makeText(getApplicationContext(),"No messages found!",Toast.LENGTH_LONG).show();
}
Code for restoring:
ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.parse("content://sms/inbox");
//Uri uri = Uri.parse("content://mms-sms/conversations/");
ContentValues values = new ContentValues();
for (int i = 0; i < readMsgsFromFile.length; i++) {
values.put("address",readMsgsFromFile[i][0]);
values.put("body",readMsgsFromFile[i][1]);
values.put("date",readMsgsFromFile[i][2]);
values.put("read",readMsgsFromFile[i][3]);
values.put("type",readMsgsFromFile[i][4]);
contentResolver.insert(Telephony.Sms.Inbox.CONTENT_URI, values);
Log.i("Restoring: ",readMsgsFromFile[i][2]);
}
Thanks Mike M. I did find a solution and you are right, the conversation table is updated whenever a new message is received or sent by a user and the time of conversation is same as that message's (whether received or sent) time. But in case of writing messages through contentresolver query it does not work and the conversation time is current time at the time of writing. So what I did is add a temporary message in all of the conversations, right after messages are restored. And after that delete all the temporary messages, this will update the conversations time to last message time.
HashSet hs = new HashSet();
hs.addAll(list);
//list is the ArrayList<String> which contains the addressess of all the messages and
//through hashset we remove all the duplicates to get only the addressess once and hence we know the number of conversations and their addressess.
list.clear();
list.addAll(hs);
//Add some dummy message to each conversation
ContentValues values2 = new ContentValues();
for (int i = 0; i < list.size(); i++) {
values2.put("address",list.get(i));
values2.put("date_sent",readMsgsFromFile[0][1]);
values2.put("date",readMsgsFromFile[0][2]);
values2.put("type",readMsgsFromFile[0][3]);
values2.put("body","temp"); //this should be more unique
values2.put("read",readMsgsFromFile[0][5]);
values2.put("service_center","01010101");
contentResolver.insert(Telephony.Sms.CONTENT_URI, values2);
}
//Now deleting that message with body 'temp' from each conversation
Cursor c = contentResolver.query(Telephony.Sms.CONTENT_URI,null,null,null,null);
while (c.moveToNext()){
String body = c.getString(c.getColumnIndex("body"));
String mid = c.getString(0);
if (body.equals("temp")){
Log.i("Deleting ",mid);
getContentResolver().delete(Uri.parse(Telephony.Sms.CONTENT_URI+"/"+mid),null,null);
}
}
c.close();
This word 'temp' could be and should be more unique so that it is not mixed with actual message.
I am trying to permanently remove a Android Contact Group and have used the Sync parameter and it always appears the record is simply marked as deleted and not physically removed. Can anyone explain how/when, if ever, the Contract group row is deleted permanently or show a snippet of code demonstrating how to do this? The records I am trying to remove are ones that I added, so they are not Read-Only.
Linked back to https://stackoverflow.com/a/21376905/5398898
My Delete Code:
private void RemoveGroup()
{
TextView tv = (TextView) this.findViewById(R.id.helloworld);
int[] startId = {10};//{6, 7, 8, 9, 10, 11};
String groupName = "My New Contacts";
Uri mUri = ContactsContract.Groups.CONTENT_URI;
mUri.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build();
for (int n = 0; n < startId.length; n++) {
groupCount = startId[n];
ContentValues values = new ContentValues();
values.put(ContactsContract.Groups._ID, groupCount);
try {
getContentResolver().delete(mUri, values.toString(),null);
} catch (Exception ex) {
tv.setText(ex.getMessage());
}
}
}
Result when reading the groups:
Image can be found here http://i.stack.imgur.com/5OOfc.png
You are building the correct Uri but not using it, try like this
Uri mUri = ContactsContract.Groups.CONTENT_URI;
mUri = mUri.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build();
Big picture: GUI shows user a list of their playlists. User picks one. Program passes chosen playlist to next activity which displays the songs in that playlist.
Problem: I can display the playlists and register the users choice, but I can't seem to display the songs of that play list.
Yes, I've see the following questions:
How to query for songs in playlists on Android SDK?
Given an Android music playlist name, how can one find the songs in the playlist?
What is the String 'volumeName' argument of MediaStore.Audio.Playlists.Members.getContentUri referring to?
As you can see in my code, I've done my best to implement those solutions, but to no avail.
Things to keep in mind: I'm testing this on a Galaxy Nexus, so no SDcard. Just internal storage and music in the cloud. I need it to work in any scenario (internal, external, or cloud). It currently works in none of those.
//#SuppressWarnings ("serial)")
public class CreationActivity extends Activity {
private final String [] STAR= {"*"};
//reads in all songs to an array
#Override
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//set layout view and assign to variable
setContentView(R.layout.creation);
TableLayout myLayout = (TableLayout)findViewById(R.id.creationLayout);
try {
Bundle extras = getIntent().getExtras();
if (extras!=null){
//get the desired playlist and ID
String playlist = extras.getString("playlist");
Long playlistID = extras.getLong("playlistID");
ArrayList<song> songs = new ArrayList<song>();
//read in the songs from the playlist
String[] proj = {MediaStore.Audio.Playlists.Members.TITLE,
MediaStore.Audio.Playlists.Members.ARTIST,
MediaStore.Audio.Playlists.Members.DURATION};
//method 1
Cursor songCursor = getContentResolver().query(MediaStore.Audio.Playlists.Members.getContentUri(null,playlistID),
proj,
null,
null,
null);
//method 2
/*
Cursor songCursor = getContentResolver().query(Uri.parse("content://com.google.android.music.MusicContent/playlists/members"),
proj,
null,
null,
null);
*/
//method 3
/*
Uri membersUri = MediaStore.Audio.Playlists.Members.getContentUri("internal", playlistID);
Cursor membersCursor = managedQuery(membersUri, STAR, null, null, null);
*/
//then this part with methods 1 and 2
/*
if (songCursor.getCount() > 0) {
songCursor.moveToFirst();
do {
song currSong = new song();
currSong.title = songCursor.getString(0);
currSong.artist = songCursor.getString(1);
songs.add(currSong);
} while (songCursor.moveToNext());
}
songCursor.close();
*/
//or this part with method 3
/*
membersCursor.moveToFirst();
for(int s= 0; s<membersCursor.getCount(); s++,
membersCursor.moveToNext()){
song currSong = new song();
currSong.title = songCursor.getString(0);
currSong.artist = songCursor.getString(1);
songs.add(currSong);
}
membersCursor.close();
*/
}else{
Toast.makeText(getBaseContext(), "No songs",Toast.LENGTH_LONG).show();
}
} catch (NumberFormatException e){
}
}
}
No errors during compiling. But "Unfortunately Music App has unexpectedly quit." every time.
Thanks for the help!
I figured it out. The key was to use the playlist ID as a string immediately within the URI. See code below.
This is the part that will get the playlist names and IDs:
String[] proj = {MediaStore.Audio.Playlists.NAME, MediaStore.Audio.Playlists._ID};
Uri playlistUri = Uri.parse("content://com.google.android.music.MusicContent/playlists");
Cursor playlistCursor = getContentResolver().query(playlistUri, proj, null, null, null);
if (playlistCursor.getCount() > 0) {
playlistCursor.moveToFirst();
do {
nameList.add(playlistCursor.getString(0));
idList.add(playlistCursor.getLong(1));
} while (playlistCursor.moveToNext());
}
Then once you have the playlist ID you can query for the songs in the playlist. This is the part of code that actually queries for the info and puts it all in an array list. NOTE: "song" is a class I have defined elsewhere, where readSong is a method that assigns values to various values (title, artist, etc).
ArrayList<song> songs = new ArrayList<song>();
//read songs into library from the correct playlist
String[] proj = {MediaStore.Audio.Playlists.Members.TITLE, MediaStore.Audio.Playlists.Members.ARTIST, MediaStore.Audio.Playlists.Members.DURATION, MediaStore.Audio.Playlists.Members._ID};
Uri songUri = Uri.parse("content://com.google.android.music.MusicContent/playlists/" + playlistID + "/members");
Cursor songCursor = getContentResolver().query(songUri, proj, null, null, null);
if (songCursor.getCount() > 0) {
songCursor.moveToFirst();
do {
//create dummy song
song currSong = new song();
//read info to dummy var
currSong.readSong(songCursor);
//add instance to collection
songs.add(currSong);
} while (songCursor.moveToNext());
}
songCursor.close();
I hope this helps anybody else who was struggling with this!! Let me know if you have any comments on my method or ways to make it better!
I've seen plenty of examples of how to set a default ringtone, but what I'm more interested in is being able populate a drop down box list filled with the available ringtones on the phone. So the list that people see when they change their ringtone in the android settings, I want to be able to list all of those.
The closest thing I've found is here, but again this is just for setting the default ringtone. Any ideas anyone? It can be in or out of ringtonemanager.
This will return you the title and uri of all the ringtones available. Do with them what you wish!
public Map<String, String> getNotifications() {
RingtoneManager manager = new RingtoneManager(this);
manager.setType(RingtoneManager.TYPE_RINGTONE);
Cursor cursor = manager.getCursor();
Map<String, String> list = new HashMap<>();
while (cursor.moveToNext()) {
String notificationTitle = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
String notificationUri = cursor.getString(RingtoneManager.URI_COLUMN_INDEX) + "/" + cursor.getString(RingtoneManager.ID_COLUMN_INDEX);
list.put(notificationTitle, notificationUri);
}
return list;
}
RingtoneManager is what you are looking for. You just need to use setType to set TYPE_RINGTONE and then iterate over the Cursor provided by getCursor.
This is a working example of an hypothetical method that returns an array of URIs, with the only slight difference that it's looking for alarms instead of ringtones:
RingtoneManager ringtoneMgr = new RingtoneManager(this);
ringtoneMgr.setType(RingtoneManager.TYPE_ALARM);
Cursor alarmsCursor = ringtoneMgr.getCursor();
int alarmsCount = alarmsCursor.getCount();
if (alarmsCount == 0 && !alarmsCursor.moveToFirst()) {
return null;
}
Uri[] alarms = new Uri[alarmsCount];
while(!alarmsCursor.isAfterLast() && alarmsCursor.moveToNext()) {
int currentPosition = alarmsCursor.getPosition();
alarms[currentPosition] = ringtoneMgr.getRingtoneUri(currentPosition);
}
alarmsCursor.close();
return alarms;