diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2014-03-03 23:33:25 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2016-11-10 17:40:55 +0100 |
commit | 1e33c4940ea43093a80077fbb7249b013e35a1f1 (patch) | |
tree | c908396564aeca07472c016c9f91faac127c9e71 | |
parent | e55eeb311c01775c123eea7bd2f6e9c82fa067b4 (diff) | |
download | coursera-1e33c4940ea43093a80077fbb7249b013e35a1f1.zip coursera-1e33c4940ea43093a80077fbb7249b013e35a1f1.tar.gz |
Android : 06-Notifications: implement
2 files changed, 20 insertions, 34 deletions
diff --git a/Android/06-Notifications/Notifications/src/course/labs/notificationslab/DownloaderTask.java b/Android/06-Notifications/Notifications/src/course/labs/notificationslab/DownloaderTask.java index b792b38..655d93a 100644 --- a/Android/06-Notifications/Notifications/src/course/labs/notificationslab/DownloaderTask.java +++ b/Android/06-Notifications/Notifications/src/course/labs/notificationslab/DownloaderTask.java @@ -147,15 +147,9 @@ public class DownloaderTask extends AsyncTask<String, Void, String[]> { log("Entered result receiver's onReceive() method"); - // TODO: Check whether the result code is RESULT_OK + if (getResultCode() == Activity.RESULT_OK) { - if (/*change this*/ true) { - - // TODO: If so, create a PendingIntent using the - // restartMainActivityIntent and set its flags - // to FLAG_UPDATE_CURRENT - - final PendingIntent pendingIntent = null; + final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); // Uses R.layout.custom_notification for the // layout of the notification View. The xml @@ -164,19 +158,13 @@ public class DownloaderTask extends AsyncTask<String, Void, String[]> { mApplicationContext.getPackageName(), R.layout.custom_notification); - // TODO: Set the notification View's text to - // reflect whether or the download completed - // successfully - - // TODO: Use the Notification.Builder class to - // create the Notification. You will have to set - // several pieces of information. You can use - // android.R.drawable.stat_sys_warning - // for the small icon. You should also setAutoCancel(true). - - Notification.Builder notificationBuilder = null; + mContentView.setTextViewText(R.id.text, successMsg); - // TODO: Send the notification + Notification.Builder notificationBuilder = new Notification.Builder(context); + notificationBuilder.setContentIntent(pendingIntent); + notificationBuilder.setContent(mContentView); + notificationBuilder.setSmallIcon(android.R.drawable.stat_sys_warning); + notificationBuilder.setAutoCancel(true); log("Notification Area Notification sent"); } diff --git a/Android/06-Notifications/Notifications/src/course/labs/notificationslab/MainActivity.java b/Android/06-Notifications/Notifications/src/course/labs/notificationslab/MainActivity.java index eebf924..e74daa9 100644 --- a/Android/06-Notifications/Notifications/src/course/labs/notificationslab/MainActivity.java +++ b/Android/06-Notifications/Notifications/src/course/labs/notificationslab/MainActivity.java @@ -17,6 +17,7 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.widget.Toast; import android.os.Bundle; import android.util.Log; @@ -70,13 +71,10 @@ public class MainActivity extends Activity implements SelectionListener { log("In ensureData(), mIsFresh:" + mIsFresh); if (!mIsFresh) { - // TODO: - // Show a Toast Notification to inform user that - // the app is "Downloading Tweets from Network" log ("Issuing Toast Message"); + Toast.makeText(MainActivity.this, "Downloading Tweets from Network", Toast.LENGTH_SHORT).show(); - // TODO: - // Start new AsyncTask to download Tweets from network + new DownloaderTask(MainActivity.this).execute(URL_LGAGA, URL_RBLACK, URL_TSWIFT); // Set up a BroadcastReceiver to receive an Intent when download // finishes. @@ -84,10 +82,9 @@ public class MainActivity extends Activity implements SelectionListener { @Override public void onReceive(Context context, Intent intent) { log("BroadcastIntent received in MainActivity"); - // TODO: - // Check to make sure this is an ordered broadcast - // Let sender know that the Intent was received - // by setting result code to RESULT_OK + if (isOrderedBroadcast()) { + setResultCode(RESULT_OK); + } } }; } else { @@ -145,15 +142,16 @@ public class MainActivity extends Activity implements SelectionListener { @Override protected void onResume() { super.onResume(); - // TODO: - // Register the BroadcastReceiver to receive a - // DATA_REFRESHED_ACTION broadcast + if(mRefreshReceiver != null) { + registerReceiver(mRefreshReceiver, new IntentFilter(DATA_REFRESHED_ACTION)); + } } @Override protected void onPause() { - // TODO: - // Unregister the BroadcastReceiver + if(mRefreshReceiver != null) { + unregisterReceiver(mRefreshReceiver); + } super.onPause(); } |