• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.os.Bundle;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.widget.Button;
31 import android.widget.TextView;
32 import android.widget.Toast;
33 
34 public class IncomingMessage extends Activity {
35     @Override
onCreate(Bundle savedInstanceState)36     protected void onCreate(Bundle savedInstanceState) {
37         super.onCreate(savedInstanceState);
38 
39         setContentView(R.layout.incoming_message);
40 
41         Button button = (Button) findViewById(R.id.notify);
42         button.setOnClickListener(new Button.OnClickListener() {
43                 public void onClick(View v) {
44                     showToast();
45                     showNotification();
46                 }
47             });
48     }
49 
50     /**
51      * The toast pops up a quick message to the user showing what could be
52      * the text of an incoming message.  It uses a custom view to do so.
53      */
showToast()54     protected void showToast() {
55         // create the view
56         View view = inflateView(R.layout.incoming_message_panel);
57 
58         // set the text in the view
59         TextView tv = (TextView)view.findViewById(R.id.message);
60         tv.setText("khtx. meet u for dinner. cul8r");
61 
62         // show the toast
63         Toast toast = new Toast(this);
64         toast.setView(view);
65         toast.setDuration(Toast.LENGTH_LONG);
66         toast.show();
67     }
68 
inflateView(int resource)69     private View inflateView(int resource) {
70         LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
71         return vi.inflate(resource, null);
72     }
73 
74     /**
75      * The notification is the icon and associated expanded entry in the
76      * status bar.
77      */
showNotification()78     protected void showNotification() {
79         // look up the notification manager service
80         NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
81 
82         // The details of our fake message
83         CharSequence from = "Joe";
84         CharSequence message = "kthx. meet u for dinner. cul8r";
85 
86         // The PendingIntent to launch our activity if the user selects this notification
87         PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
88                 new Intent(this, IncomingMessageView.class), 0);
89 
90         // The ticker text, this uses a formatted string so our message could be localized
91         String tickerText = getString(R.string.imcoming_message_ticker_text, message);
92 
93         // construct the Notification object.
94         Notification notif = new Notification(R.drawable.stat_sample, tickerText,
95                 System.currentTimeMillis());
96 
97         // Set the info for the views that show in the notification panel.
98         notif.setLatestEventInfo(this, from, message, contentIntent);
99 
100         // after a 100ms delay, vibrate for 250ms, pause for 100 ms and
101         // then vibrate for 500ms.
102         notif.vibrate = new long[] { 100, 250, 100, 500};
103 
104         // Note that we use R.layout.incoming_message_panel as the ID for
105         // the notification.  It could be any integer you want, but we use
106         // the convention of using a resource id for a string related to
107         // the notification.  It will always be a unique number within your
108         // application.
109         nm.notify(R.string.imcoming_message_ticker_text, notif);
110     }
111 }
112