FileDocCategorySizeDatePackage
IncomingMessage.javaAPI DocAndroid 1.5 API4110Wed May 06 22:41:08 BST 2009com.example.android.apis.app

IncomingMessage

public class IncomingMessage extends android.app.Activity

Fields Summary
Constructors Summary
Methods Summary
private android.view.ViewinflateView(int resource)

        LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        return vi.inflate(resource, null);
    
protected voidonCreate(android.os.Bundle savedInstanceState)

        super.onCreate(savedInstanceState);

        setContentView(R.layout.incoming_message);

        Button button = (Button) findViewById(R.id.notify);
        button.setOnClickListener(new Button.OnClickListener() {
                public void onClick(View v) {
                    showToast();
                    showNotification();
                }
            });
    
protected voidshowNotification()
The notification is the icon and associated expanded entry in the status bar.

        // 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";

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, IncomingMessageView.class), 0);

        // The ticker text, this uses a formatted string so our message could be localized
        String tickerText = getString(R.string.imcoming_message_ticker_text, message);

        // construct the Notification object.
        Notification notif = new Notification(R.drawable.stat_sample, tickerText,
                System.currentTimeMillis());

        // Set the info for the views that show in the notification panel.
        notif.setLatestEventInfo(this, from, message, contentIntent);

        // 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);
    
protected voidshowToast()
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.

        // 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();