GeekHub - Android Season 3 (2013/2014) | Stage 2
Notifications
Notifications in the notification area
Notifications in the notification drawer
Notification Display Elements Normal view
1. 2. 3. 4. 5. 6.
Big view (4.1+)
Content title Large icon Content text Content info Small icon Time that the notification was issued. You can set an explicit value with setWhen() if you don't it defaults to the time that the system received the notification.
Creating a Notification Required notification contents ● A small icon, set by setSmallIcon() ● A title, set by setContentTitle() ● Detail text, set by setContentText() All other notification settings and contents are optional and listed here: NotificationCompat.Builder
Create a Notification Builder 1. 2. 3. 4. 5.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!");
Define the Notification's Action 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.
Intent resultIntent = new Intent(this, ResultActivity.class); ... // Because clicking the notification opens a new ("special") activity, there's // no need to create an artificial back stack. PendingIntent resultPendingIntent = PendingIntent.getActivity( this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT );
Set the Notification's Click Behavior 1. 2. 3.
PendingIntent resultPendingIntent; ... mBuilder.setContentIntent(resultPendingIntent);
Issue the Notification 1. 2. 3. 4. 5. 6. 7. 8. 9.
NotificationCompat.Builder mBuilder; ... // Sets an ID for the notification int mNotificationId = 001; // Gets an instance of the NotificationManager service NotificationManager mNotifyMgr = (NotificationManager) getSystemService (NOTIFICATION_SERVICE); // Builds the notification and issues it. mNotifyMgr.notify(mNotificationId, mBuilder.build());
Preserving Navigation when Starting an Activity 1. Regular activity You're starting an Activity that's part of the application's normal workflow. (With back stack) 2. Special activity The user only sees this Activity if it's started from a notification. (Without back stack)
Setting up a regular activity PendingIntent 1. Define your application's Activity hierarchy in the manifest. a. Add support for Android 4.0.3 and earlier. To do this, specify the parent of the Activity you're starting by adding a <meta-data> element as the child of the . b. Also add support for Android 4.1 and later. To do this, add the android:parentActivityName attribute to the element of the Activity you're starting.
Setting up a regular activity PendingIntent 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15.
<meta-data android:name ="android.support.PARENT_ACTIVITY" android:value =".MainActivity" />
Setting up a regular activity PendingIntent 2. Create a back stack based on the Intent that starts the Activity: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.
... Intent resultIntent = new Intent(this, ResultActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create (this); // Adds the back stack stackBuilder.addParentStack(ResultActivity.class); // Adds the Intent to the top of the stack stackBuilder.addNextIntent(resultIntent); // Gets a PendingIntent containing the entire back stack PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent. FLAG_UPDATE_CURRENT); ...
Setting up a special activity PendingIntent 1. In your manifest, add the following attributes to the element for the Activity: android:name="activityclass" The activity's fully-qualified class name. android:taskAffinity="" Combined with the FLAG_ACTIVITY_NEW_TASK flag that you set in code, this ensures that this Activity doesn't go into the application's default task. Any existing tasks that have the application's default affinity are not affected. android:excludeFromRecents="true" Excludes the new task from Recents, so that the user can't accidentally navigate back to it.
Setting up a special activity PendingIntent 1. 2. 3. 4. 5. 6. 7. 8.
...
Setting up a special activity PendingIntent 2. Build and issue the notification: 1. 2. 3. 4. 5. 6. 7. 8. 9.
// Instantiate a Builder object. NotificationCompat.Builder builder = new NotificationCompat. Builder(this); // Creates an Intent for the Activity Intent notifyIntent = new Intent(new ComponentName(this, ResultActivity. class)); // Sets the Activity to start in a new, empty task notifyIntent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK); // Creates the PendingIntent ...
Services - This corresponds to calls to Context.startService(), which ask the system to schedule work for the service, to be run until the service or someone else explicitly stop it. - A facility for an application to expose some of its functionality to other applications. This corresponds to calls to Context.bindService(), which allows a long-standing connection to be made to the service in order to interact with it.
Services public class LocalService extends Service { public class LocalBinder extends Binder { LocalService getService() { return LocalService.this; } } @Override public void onCreate() { mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); // Display a notification about us starting. We put an icon in the status bar. showNotification(); }
Services @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i("LocalService", "Received start id " + startId + ": " + intent); // We want this service to continue running until it is explicitly // stopped, so return sticky. return START_STICKY; } @Override public void onDestroy() { // Cancel the persistent notification. mNM.cancel(NOTIFICATION); // Tell the user we stopped. Toast.makeText(this, R.string.local_service_stopped, Toast.LENGTH_SHORT).show(); }
Services @Override public IBinder onBind(Intent intent) { return mBinder; } // This is the object that receives interactions from clients. See // RemoteService for a more complete example. private final IBinder mBinder = new LocalBinder(); public class LocalBinder extends Binder { LocalService getService() { return LocalService.this; } }
http://developer.android.com/reference/android/app/Service.html
Hometask Написать сервис который будет проверять новые новости в вашей ленте через заданое время. Сервис запускается при старте приложения и отображает нотификацию о том, что он (сервис) запущен. При появлении новых новостей апдейтить нотификейшн.