Ringtone Randomizer Example

It is always a good idea to modify the behavior of android device’s in a new and innovative ways. The Android platform gives it and developers a lot of freedom to build such apps so that its users would be able to make such modification. Today, in this tutorial we will learn how to create an app that randomizes the ringtone of an Android phone every time it receives a call.

Make sure you have the latest version of Android Studio installed. You can get it from the Android Developer website. By updating to latest version I just wanted to make sure you will get all the features provided by android team.

Assuming you are already familiar with the basics of Android development and have created one or more Android apps. Follow the steps below to create ringtone randomizer:

Step1: Create a new Android project in studio or your Android environment.

Step2: Write following into your manifest file:

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ringtonerandomizerexample" >

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".RingtoneReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>

</manifest>

 

Step3: Create and write the following into your RingtoneHelper.java:

 

public class RingtoneHelper {

public static List<Ringtone> fetchAvailableRingtones(Context context){

List<Ringtone> ringtones = new ArrayList<>();
RingtoneManager mgr = new RingtoneManager(context);
mgr.setType(RingtoneManager.TYPE_RINGTONE);

int n = mgr.getCursor().getCount();
for(int i=0;i<n;i++){
ringtones.add(mgr.getRingtone(i));
}

return ringtones;
}

public static void changeRingtone(Context context){

SharedPreferences preferences = context.getSharedPreferences("randomizer", Context.MODE_PRIVATE);
if(!preferences.getBoolean("active", false))
return;

RingtoneManager mgr = new RingtoneManager(context);
Random random = new Random(System.currentTimeMillis());

int n = random.nextInt(mgr.getCursor().getCount());

RingtoneManager.setActualDefaultRingtoneUri(context,
RingtoneManager.TYPE_RINGTONE, mgr.getRingtoneUri(n));
}
}

 

Step3: Write below into strings.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">Ringtone Randomizer</string>

<string name="activate">Activate Ringtone Randomizer</string>
<string name="deactivate">Deactivate Ringtone Randomizer</string>

<string name="list_of_ringtones">Ringtones available on this device:</string>

</resources>

 

Step4: Below is your main.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
>

<ToggleButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textOff="@string/activate"
android:textOn="@string/deactivate"
android:id="@+id/toggle"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/list_of_ringtones"
android:textStyle="bold"
/>

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_of_ringtones"
/>

</LinearLayout>

 

Step5: Create a receiver class and write the following into it:

 

public class RingtoneReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

if(intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
String callState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (callState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
RingtoneHelper.changeRingtone(context);
}
}
}
}

 

 

Step6: Write the following into main activity:

 

public class MainActivity extends Activity {

private ListView listOfRingtones;
private ToggleButton toggleRandomizer;
private List<Ringtone> ringtones;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
listOfRingtones = (ListView)findViewById(R.id.list_of_ringtones);
toggleRandomizer = (ToggleButton)findViewById(R.id.toggle);

ringtones = RingtoneHelper.fetchAvailableRingtones(this);
initializeList();
initializeToggle();
}

private void initializeToggle(){
final SharedPreferences preferences = getSharedPreferences("randomizer", Context.MODE_PRIVATE);
boolean active = preferences.getBoolean("active", false);
toggleRandomizer.setChecked(active);

toggleRandomizer.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
preferences.edit().putBoolean("active", isChecked).commit();
}
});
}

private void initializeList(){
ArrayAdapter<Ringtone> adapter = new ArrayAdapter<Ringtone>(this,
android.R.layout.simple_list_item_1, ringtones) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView item = (TextView)super.getView(position, convertView, parent);
item.setText(ringtones.get(position).getTitle(MainActivity.this));
return item;
}
};

listOfRingtones.setAdapter(adapter);
}
}

 

Step7: Run for the output below:

ringtone1

ringtone2

Check my previous blog on RoboGuice Library

Leave a Comment: