Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)
CustomNotifications / src / com.example.android.customnotifications /

MainActivity.java

       
        1
       
       
        /*
       
       
        2
       
       
        * Copyright (C) 2013 The Android Open Source Project
       
       
        3
       
       
        * Licensed under the Apache License, Version 2.0 (the "License");
       
       
        4
       
       
        * you may not use this file except in compliance with the License.
       
       
        5
       
       
        * You may obtain a copy of the License at
       
       
        6
       
       
        *
       
       
        7
       
       
        *       http://www.apache.org/licenses/LICENSE-2.0
       
       
        8
       
       
        *
       
       
        9
       
       
        * Unless required by applicable law or agreed to in writing, software
       
       
        10
       
       
        * distributed under the License is distributed on an "AS IS" BASIS,
       
       
        11
       
       
        * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
       
        12
       
       
        * See the License for the specific language governing permissions and
       
       
        13
       
       
        * limitations under the License.
       
       
        14
       
       
        */
       
       
        15
       
       
       
       
        16
       
       
        package com.example.android.customnotifications;
       
       
        17
       
       
       
       
        18
       
       
        import android.app.Activity;
       
       
        19
       
       
        import android.app.Notification;
       
       
        20
       
       
        import android.app.NotificationManager;
       
       
        21
       
       
        import android.app.PendingIntent;
       
       
        22
       
       
        import android.content.Intent;
       
       
        23
       
       
        import android.os.Build;
       
       
        24
       
       
        import android.os.Bundle;
       
       
        25
       
       
        import android.support.v4.app.NotificationCompat;
       
       
        26
       
       
        import android.view.View;
       
       
        27
       
       
        import android.widget.RemoteViews;
       
       
        28
       
       
       
       
        29
       
       
        import java.text.DateFormat;
       
       
        30
       
       
        import java.util.Date;
       
       
        31
       
       
       
       
        32
       
       
        public class MainActivity extends Activity {
       
       
        33
       
       
        /**
       
       
        34
       
       
        * This sample demonstrates notifications with custom content views.
       
       
        35
       
       
        *
       
       
        36
       
       
        * <p>On API level 16 and above a big content view is also defined that is used for the
       
       
        37
       
       
        * 'expanded' notification. The notification is created by the NotificationCompat.Builder.
       
       
        38
       
       
        * The expanded content view is set directly on the {@link android.app.Notification} once it has been build.
       
       
        39
       
       
        * (See {@link android.app.Notification#bigContentView}.) </p>
       
       
        40
       
       
        *
       
       
        41
       
       
        * <p>The content views are inflated as {@link android.widget.RemoteViews} directly from their XML layout
       
       
        42
       
       
        * definitions using {@link android.widget.RemoteViews#RemoteViews(String, int)}.</p>
       
       
        43
       
       
        */
       
       
        44
       
       
        private void createNotification() {
       
       
        46
       
       
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
       
       
        48
       
       
       
       
        50
       
       
        //Create Intent to launch this Activity again if the notification is clicked.
       
       
        51
       
       
        Intent i = new Intent(this, MainActivity.class);
       
       
        52
       
       
        i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
       
       
        53
       
       
        PendingIntent intent = PendingIntent.getActivity(this, 0, i,
       
       
        54
       
       
        PendingIntent.FLAG_UPDATE_CURRENT);
       
       
        55
       
       
        builder.setContentIntent(intent);
       
       
        57
       
       
       
       
        59
       
       
        // Sets the ticker text
       
       
        60
       
       
        builder.setTicker(getResources().getString(R.string.custom_notification));
       
       
        61
       
       
       
       
        62
       
       
        // Sets the small icon for the ticker
       
       
        63
       
       
        builder.setSmallIcon(R.drawable.ic_stat_custom);
       
       
        65
       
       
       
       
        67
       
       
        // Cancel the notification when clicked
       
       
        68
       
       
        builder.setAutoCancel(true);
       
       
        69
       
       
       
       
        70
       
       
        // Build the notification
       
       
        71
       
       
        Notification notification = builder.build();
       
       
        73
       
       
       
       
        75
       
       
        // Inflate the notification layout as RemoteViews
       
       
        76
       
       
        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);
       
       
        77
       
       
       
       
        78
       
       
        // Set text on a TextView in the RemoteViews programmatically.
       
       
        79
       
       
        final String time = DateFormat.getTimeInstance().format(new Date()).toString();
       
       
        80
       
       
        final String text = getResources().getString(R.string.collapsed, time);
       
       
        81
       
       
        contentView.setTextViewText(R.id.textView, text);
       
       
        82
       
       
       
       
        83
       
       
        /* Workaround: Need to set the content view here directly on the notification.
       
       
        84
       
       
        * NotificationCompatBuilder contains a bug that prevents this from working on platform
       
       
        85
       
       
        * versions HoneyComb.
       
       
        86
       
       
        * See https://code.google.com/p/android/issues/detail?id=30495
       
       
        87
       
       
        */
       
       
        88
       
       
        notification.contentView = contentView;
       
       
        89
       
       
       
       
        90
       
       
        // Add a big content view to the notification if supported.
       
       
        91
       
       
        // Support for expanded notifications was added in API level 16.
       
       
        92
       
       
        // (The normal contentView is shown when the notification is collapsed, when expanded the
       
       
        93
       
       
        // big content view set here is displayed.)
       
       
        94
       
       
        if (Build.VERSION.SDK_INT >= 16) {
       
       
        95
       
       
        // Inflate and set the layout for the expanded notification view
       
       
        96
       
       
        RemoteViews expandedView =
       
       
        97
       
       
        new RemoteViews(getPackageName(), R.layout.notification_expanded);
       
       
        98
       
       
        notification.bigContentView = expandedView;
       
       
        99
       
       
        }
       
       
        101
       
       
       
       
        102
       
       
        // START_INCLUDE(notify)
       
       
        103
       
       
        // Use the NotificationManager to show the notification
       
       
        104
       
       
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
       
       
        105
       
       
        nm.notify(0, notification);
       
       
        107
       
       
        }
       
       
        108
       
       
       
       
        109
       
       
        @Override
       
       
        110
       
       
        protected void onCreate(Bundle savedInstanceState) {
       
       
        111
       
       
        super.onCreate(savedInstanceState);
       
       
        112
       
       
        setContentView(R.layout.sample_main);
       
       
        113
       
       
        }
       
       
        114
       
       
       
       
        115
       
       
        /**
       
       
        116
       
       
        * Create and show a notification with a custom layout.
       
       
        117
       
       
        * This callback is defined through the 'onClick' attribute of the
       
       
        118
       
       
        * 'Show Notification' button in the XML layout.
       
       
        119
       
       
        *
       
       
        120
       
       
        * @param v
       
       
        121
       
       
        */
       
       
        122
       
       
        public void showNotificationClicked(View v) {
       
       
        123
       
       
        createNotification();
       
       
        124
       
       
        }
       
       
        125
       
       
        }