天天看點

Android Api Demos登頂之路(五十一)Notification-->Status Bar

這個demo示範了notification的不同的顯示方式,以及自定義消息的視圖、設定預設的狀态

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Icons only" />
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/icon_only"
        android:text="Icon Only"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Icons and marquee"
        android:layout_marginTop="10dp" />
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/icon_marquee"
        android:text="Icon And Marquee"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Use remote views in balloon"
        android:layout_marginTop="10dp" />
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/icon_remoteview"
        android:text="Use RemoteView"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Use default values where applicable"
        android:layout_marginTop="10dp" />
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/default_sound"
        android:text="Sound"/>
        <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/default_vibrate"
        android:text="Vibrate"/>
        <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/default_all"
        android:text="All"/>
    </LinearLayout>
    <Button 
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/clear"
        android:text="Clear Notification"/>

</LinearLayout>
           

RemoteView的布局檔案:notification_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <ImageView 
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:id="@+id/icon"/>
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text"
        android:layout_gravity="center_vertical"
        android:text="hello"
        android:layout_marginLeft="10dp"/>
</LinearLayout>
           

MainActivity

public class MainActivity extends Activity implements OnClickListener {
    private NotificationManager nm;
    private static final int NOTIFICATION_ID = ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button icon_only = (Button) findViewById(R.id.icon_only);
        Button icon_marquee = (Button) findViewById(R.id.icon_marquee);
        Button icon_remoteview = (Button) findViewById(R.id.icon_remoteview);
        Button default_sound = (Button) findViewById(R.id.default_sound);
        Button default_vibrate = (Button) findViewById(R.id.default_vibrate);
        Button default_all = (Button) findViewById(R.id.default_all);
        Button clear = (Button) findViewById(R.id.clear);
        icon_only.setOnClickListener(this);
        icon_marquee.setOnClickListener(this);
        icon_remoteview.setOnClickListener(this);
        default_sound.setOnClickListener(this);
        default_vibrate.setOnClickListener(this);
        default_all.setOnClickListener(this);
        clear.setOnClickListener(this);

        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.icon_only:
            showNotification(R.drawable.ic_launcher, "摘要資訊", false);
            break;
        case R.id.icon_marquee:
            showNotification(R.drawable.ic_launcher, "摘要資訊", true);
            break;
        case R.id.icon_remoteview:
            showViewNotification(R.drawable.icon48x48_2, "摘要資訊");
            break;
        case R.id.default_sound:
            showNotificationDefalut(Notification.DEFAULT_SOUND);
            break;
        case R.id.default_vibrate:
            showNotificationDefalut(Notification.DEFAULT_VIBRATE);
            break;
        case R.id.default_all:
            showNotificationDefalut(Notification.DEFAULT_ALL);
            break;
        case R.id.clear:
            nm.cancel(NOTIFICATION_ID);
            break;
        }
    }

    private void showNotification(int iconId, String text, boolean showTricker) {
        PendingIntent contentIntent = PendingIntent.getActivity(this, , new Intent(
                this, NotificationDisplay.class), PendingIntent.FLAG_CANCEL_CURRENT);
        String trickerText=showTricker ? "顯示提示資訊":null;
        Notification.Builder builder=new Builder(this);
        builder.setTicker(trickerText)
               .setSmallIcon(iconId)
               .setContentIntent(contentIntent)
               .setContentTitle("Notification")
               .setContentText(text)
               .setWhen(System.currentTimeMillis());
        Notification noti=builder.build();
        nm.notify(NOTIFICATION_ID, noti);
    }

    private void showViewNotification(int iconId, String text) {
        PendingIntent contentIntent = PendingIntent.getActivity(this, , new Intent(
                this, NotificationDisplay.class), PendingIntent.FLAG_CANCEL_CURRENT);

        /*RemoteViews描述一個跨程序顯示的View在對 notification的View進行自定義時就必須
         * 使用這個類。因為notification的view程序與顯示notification的程序是兩個不同的程序。
         * 使用RemoteViews必須首先定義一個layout的布局檔案*/
        RemoteViews rv=new RemoteViews(getPackageName(), R.layout.notification_view);
        //對RemoteViews中的控件進行簡單的指派
        rv.setImageViewResource(R.id.icon, iconId);
        rv.setTextViewText(R.id.text, text);

        Notification.Builder builder=new Builder(this);
        builder.setTicker(text)
               .setSmallIcon(R.drawable.ic_launcher)
               .setContentIntent(contentIntent)
               .setContent(rv);
        Notification noti=builder.build();
        nm.notify(NOTIFICATION_ID, noti);
    }

    private void showNotificationDefalut(int defaluts) {
        PendingIntent contentIntent = PendingIntent.getActivity(this, , new Intent(
                this, NotificationDisplay.class), PendingIntent.FLAG_CANCEL_CURRENT);
        String trickerText= "顯示提示資訊";
        Notification.Builder builder=new Builder(this);
        builder.setTicker(trickerText)
               .setSmallIcon(R.drawable.ic_launcher)
               .setContentIntent(contentIntent)
               .setContentTitle("Notification")
               .setContentText("Summary info")
               .setWhen(System.currentTimeMillis())
               .setDefaults(defaluts);
        Notification noti=builder.build();
        nm.notify(NOTIFICATION_ID, noti);
    }

}
           

在本例中隻是使用了一個最簡單的textView Activity,這裡不再貼代碼。

配置檔案中配置這個Activity。

繼續閱讀