IncomingMessage.java
samples/ApiDemos/src/com/google/android/samples/app/
Original IncomingMessage.java
/*
* Copyright (C) 2007 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.samples.app;
import com.google.android.samples.R;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Telephony.Sms;
import android.view.View;
import android.view.ViewInflate;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class IncomingMessage extends Activity {
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.incoming_message);
Button button = (Button) findViewById(R.id.notify);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
showToast();
showNotification();
}
});
}
/**
* The toast pops up a quick message to the user showing what could be
* the text of an incoming message. It uses a custom view to do so.
*/
protected void showToast() {
// create the view
View view = inflateView(R.layout.incoming_message_panel);
// set the text in the view
TextView tv = (TextView)view.findViewById(R.id.message);
tv.setText("khtx. meet u for dinner. cul8r");
// show the toast
Toast toast = new Toast(this);
toast.setView(view);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}
private View inflateView(int resource) {
ViewInflate vi = (ViewInflate)getSystemService(Context.INFLATE_SERVICE);
return vi.inflate(resource, null, null);
}
/**
* The notification is the icon and associated expanded entry in the
* status bar.
*/
protected void showNotification() {
// look up the notification manager service
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// The details of our fake message
CharSequence from = "Joe";
CharSequence message = "kthx. meet u for dinner. cul8r";
// What happens when we click the notification in the expanded status bar. In this
// case, we launch the IncomingMessageView activity.
Intent contentIntent = new Intent(this, IncomingMessageView.class);
// The ticker text, this uses a formatted string so our message could be localized
String tickerText = getString(R.string.imcoming_message_ticker_text, message);
// What happens when we click the app icon -- we'll just launch the SMS Inbox for
// the purposes of this demo
Intent appIntent = new Intent(Intent.VIEW_ACTION, Sms.Inbox.CONTENT_URI);
// construct the Notification object.
Notification notif = new Notification(
this, // our context
R.drawable.stat_sample, // the icon for the status bar
tickerText, // the text to display in the ticker
System.currentTimeMillis(), // the timestamp for the notification
from, // the title for the notification
message, // the details to display in the notification
contentIntent, // the contentIntent (see above)
R.drawable.app_sample_code, // the app icon
getText(R.string.activity_sample_code), // the name of the app
appIntent);
// after a 100ms delay, vibrate for 250ms, pause for 100 ms and
// then vibrate for 500ms.
notif.vibrate = new long[] { 100, 250, 100, 500};
// Note that we use R.layout.incoming_message_panel as the ID for
// the notification. It could be any integer you want, but we use
// the convention of using a resource id for a string related to
// the notification. It will always be a unique number within your
// application.
nm.notify(R.string.imcoming_message_ticker_text, notif);
}
}