Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.apis.app;
     18 
     19 import com.example.android.apis.R;
     20 
     21 import android.app.Activity;
     22 import android.app.Notification;
     23 import android.app.NotificationManager;
     24 import android.app.PendingIntent;
     25 import android.content.Context;
     26 import android.content.Intent;
     27 import android.graphics.Bitmap;
     28 import android.graphics.BitmapFactory;
     29 import android.os.Bundle;
     30 import android.view.LayoutInflater;
     31 import android.view.View;
     32 import android.widget.Button;
     33 import android.widget.TextView;
     34 
     35 public class IncomingMessage extends Activity {
     36     @Override
     37     protected void onCreate(Bundle savedInstanceState) {
     38         super.onCreate(savedInstanceState);
     39 
     40         setContentView(R.layout.incoming_message);
     41 
     42         Button button = (Button) findViewById(R.id.notify);
     43         button.setOnClickListener(new Button.OnClickListener() {
     44                 public void onClick(View v) {
     45                     showNotification();
     46                 }
     47             });
     48     }
     49 
     50     private View inflateView(int resource) {
     51         LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     52         return vi.inflate(resource, null);
     53     }
     54 
     55     /**
     56      * The notification is the icon and associated expanded entry in the
     57      * status bar.
     58      */
     59     protected void showNotification() {
     60         // look up the notification manager service
     61         NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
     62 
     63         // The details of our fake message
     64         CharSequence from = "Joe";
     65         CharSequence message = "kthx. meet u for dinner. cul8r";
     66 
     67         // The PendingIntent to launch our activity if the user selects this notification
     68 //BEGIN_INCLUDE(pending_intent)
     69         PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
     70                 new Intent(this, IncomingMessageView.class), 0);
     71 //END_INCLUDE(pending_intent)
     72 
     73         // The ticker text, this uses a formatted string so our message could be localized
     74         String tickerText = getString(R.string.imcoming_message_ticker_text, message);
     75 
     76         // construct the Notification object.
     77         Notification notif = new Notification(R.drawable.stat_sample, tickerText,
     78                 System.currentTimeMillis());
     79 
     80         // Set the info for the views that show in the notification panel.
     81         notif.setLatestEventInfo(this, from, message, contentIntent);
     82 
     83         /*
     84         // On tablets, the ticker shows the sender, the first line of the message,
     85         // the photo of the person and the app icon.  For our sample, we just show
     86         // the same icon twice.  If there is no sender, just pass an array of 1 Bitmap.
     87         notif.tickerTitle = from;
     88         notif.tickerSubtitle = message;
     89         notif.tickerIcons = new Bitmap[2];
     90         notif.tickerIcons[0] = getIconBitmap();;
     91         notif.tickerIcons[1] = getIconBitmap();;
     92         */
     93 
     94         // after a 0ms delay, vibrate for 250ms, pause for 100 ms and
     95         // then vibrate for 500ms.
     96         notif.vibrate = new long[] { 0, 250, 100, 500};
     97 
     98         // Note that we use R.layout.incoming_message_panel as the ID for
     99         // the notification.  It could be any integer you want, but we use
    100         // the convention of using a resource id for a string related to
    101         // the notification.  It will always be a unique number within your
    102         // application.
    103         nm.notify(R.string.imcoming_message_ticker_text, notif);
    104     }
    105 
    106     private Bitmap getIconBitmap() {
    107         BitmapFactory f = new BitmapFactory();
    108         return f.decodeResource(getResources(), R.drawable.app_sample_code);
    109     }
    110 }
    111 
    112