Android get list of active alarms

前端 未结 6 1029
广开言路
广开言路 2021-01-01 10:04

Is there any way to get a list of all the active alarms in the android device programmatically in our application programmatically.Just point me out to some links that can b

6条回答
  •  温柔的废话
    2021-01-01 10:21

    For getting active alarms, there is no work-around yet, but to get the list of alarms - use the RingtoneManager, which has access to alarm tones, ringtones & notification sounds.

    1. To launch the default intent of user selecting an alarm, you can launch an implicit intent like so:

    private void selectAlarmTone(){
        Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_ALARM); //Incase you want the user to set a notification use => RingtoneManager.TYPE_NOTIFICATION, ringtone use => RingtoneManager.TYPE_RINGTONE
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select an alarm tone");
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
        startActivityForResult(intent, 1);
    }
    

    and to get the alarm that the user has selected use from onActivityResult:

    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final
    Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && requestCode == 1) {
            Uri selectedUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
    
            Ringtone ringtone = RingtoneManager.getRingtone(this, selectedUri);
            String title = ringtone.getTitle(this);
            Log.i("ALARM_SELECTED: ", title);
        }
    }
    

    2. Incase you want to get the list of the tones programmatically, use:

    public void listAlarmtones() {
        RingtoneManager ringtoneManager = new RingtoneManager(this);
        ringtoneManager.setType(RingtoneManager.TYPE_ALARM);
        Cursor cursor = ringtoneManager.getCursor();
    
          while (cursor.moveToNext()) {
            String title = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
            String uri = String.valueOf(ringtoneManager.getRingtoneUri(cursor.getPosition()));
    
            String alarmTone = cursor.getString(cursor.getColumnIndex("title"));
    
            Log.e("ALARM_TONE: ", title + "=>" + uri + "=>" + alarmTone);
          }
    }
    

提交回复
热议问题