Tuesday, June 23, 2015

Android example using ACTION_RINGTONE_PICKER

This example show how to start another activity of RingTone Picker using ACTION_RINGTONE_PICKER intent, further works on last exercise "Generate Alarm, Notification and Ringtone using RingtoneManager".


com.example.androidringtone.MainActivity.java
package com.example.androidringtone;

import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    RadioButton optAlarm, optNotification, optRingtone;
    RadioButton optRingTonePicker;
    Button btnStart, btnStop;
    Ringtone ringTone;

    Uri uriAlarm, uriNotification, uriRingtone;

    final int RQS_RINGTONEPICKER = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        optAlarm = (RadioButton)findViewById(R.id.optAlarm);
        optNotification = (RadioButton)findViewById(R.id.optNotification);
        optRingtone = (RadioButton)findViewById(R.id.optRingtone);
        optRingTonePicker = (RadioButton)findViewById(R.id.optPicker);
        btnStart = (Button)findViewById(R.id.start);
        btnStop = (Button)findViewById(R.id.stop);

        uriAlarm = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        uriNotification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        uriRingtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
        optAlarm.setText("uriAlarm: " + uriAlarm);
        optNotification.setText("uriNotification: " + uriNotification);
        optRingtone.setText("uriRingtone: " + uriRingtone);

        btnStart.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ringTone != null) {
                    //previous ringTone is playing,
                    //stop it first
                    ringTone.stop();
                }

                if (optAlarm.isChecked()) {
                    ringTone = RingtoneManager
                            .getRingtone(getApplicationContext(), uriAlarm);
                } else if (optNotification.isChecked()) {
                    ringTone = RingtoneManager
                            .getRingtone(getApplicationContext(), uriNotification);
                } else if (optRingtone.isChecked()) {
                    ringTone = RingtoneManager
                            .getRingtone(getApplicationContext(), uriRingtone);
                } else if (optRingTonePicker.isChecked()) {
                    startRingTonePicker();
                    return; //skip Toast to display RingTone title
                }

                Toast.makeText(MainActivity.this,
                        ringTone.getTitle(MainActivity.this),
                        Toast.LENGTH_LONG).show();
                ringTone.play();
            }
        });

        btnStop.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                if(ringTone!=null){
                    ringTone.stop();
                    ringTone = null;
                }
            }
        });
    }

    private void startRingTonePicker(){
        Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
        startActivityForResult(intent, RQS_RINGTONEPICKER);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == RQS_RINGTONEPICKER && resultCode == RESULT_OK){
            Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
            ringTone = RingtoneManager.getRingtone(getApplicationContext(), uri);
            Toast.makeText(MainActivity.this,
                    ringTone.getTitle(MainActivity.this),
                    Toast.LENGTH_LONG).show();
            ringTone.play();
        }
    }
}


layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/optAlarm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Alarm"
            android:checked="true" />

        <RadioButton
            android:id="@+id/optNotification"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Notification" />

        <RadioButton
            android:id="@+id/optRingtone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Ringtone" />

        <RadioButton
            android:id="@+id/optPicker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ACTION_RINGTONE_PICKER" />

    </RadioGroup>

    <Button
        android:id="@+id/start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start"/>

    <Button
        android:id="@+id/stop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Stop"/>

</LinearLayout>



1 comment:

prabhu krish said...

I want to add the event in mainactivity.java with particular date, time and event name. post some code for this. please...

Thanks in advance...