Android Boot Camp for Developers Using Java: A Guide to Creating Your First Andr
ID: 3671801 • Letter: A
Question
Android Boot Camp for Developers Using Java: A Guide to Creating Your First Android Apps, 3rd Edition
Case Project 6–5: Ring Tones App***
Requirements Document
Application title:
Ring Tones App
Purpose:
The Ring Tones app allows you to listen to three different ring tones using RadioButton controls for selection.
Algorithms:
1. Create an app that opens with a picture of a mobile phone and a title for three seconds.
2. The second screen shows three RadioButton controls that display different ring tone titles and a description of each ring tone.
Conditions:
1. Select your own images and free ring tones available by searching the Web.
2. When a ring tone is playing, the other buttons should not be displayed. Each ring tone can play and pause on the user’s selection.
Author: Corinne Hoisington VBID: 9781305986060Explanation / Answer
first xml permissions
<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"
next the activity of the app
activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
broadcast reciever
<receiver
android:name=".RingReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
edit strings in 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>
activity functions
<?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>
create helper class
{
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;
}
}
changer
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));
}
create Broad Cast reciever
public class RingReceiver 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);
}
}
}
}
create 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);
}