14.01.2013 Views

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

}<br />

Chapter 9: Peer-to-Peer Communication<br />

editor.putBoolean(getString(R.string.autoRespondPref), autoRespond);<br />

editor.putString(getString(R.string.responseTextPref), respondText);<br />

editor.putBoolean(getString(R.string.includeLocationPref), includeLoc );<br />

editor.putInt(getString(R.string.respondForPref), respondForIndex );<br />

editor.commit();<br />

// Set the alarm to turn off the autoresponder<br />

setAlarm(respondForIndex);<br />

private void setAlarm(int respondForIndex) {}<br />

8. The setAlarm stub from Step 8 is used to create a new Alarm that fi res an Intent that should<br />

result in the AutoResponder being disabled.<br />

You’ll need to create a new Alarm object and a BroadcastReceiver that listens for it before<br />

disabling the auto-responder accordingly.<br />

8.1. Start by creating the action String that will represent the Alarm Intent.<br />

public static final String alarmAction =<br />

“com.paad.emergencyresponder.AUTO_RESPONSE_EXPIRED”;<br />

8.2. Then create a new Broadcast Receiver instance that listens for an Intent that includes<br />

the action specifi ed in Step 7. When this Intent is received, it should modify the autoresponder<br />

settings to disable the automatic response.<br />

private BroadcastReceiver stopAutoResponderReceiver = new BroadcastReceiver() {<br />

@Override<br />

public void onReceive(Context context, Intent intent) {<br />

if (intent.getAction().equals(alarmAction)) {<br />

String preferenceName = getString(R.string.user_preferences);<br />

SharedPreferences sp = getSharedPreferences(preferenceName, 0);<br />

}<br />

};<br />

}<br />

Editor editor = sp.edit();<br />

editor.putBoolean(getString(R.string.autoRespondPref), false);<br />

editor.commit();<br />

8.3. Finally, complete the setAlarm method. It should cancel the existing alarm if the autoresponder<br />

is turned off; otherwise, it should update it with the latest expiry time.<br />

PendingIntent intentToFire;<br />

private void setAlarm(int respondForIndex) {<br />

// Create the alarm and register the alarm intent receiver.<br />

AlarmManager alarms = (AlarmManager)getSystemService(ALARM_SERVICE);<br />

if (intentToFire == null) {<br />

Intent intent = new Intent(alarmAction);<br />

311

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!