android开发 -- Notification 状态栏 消息推送

这里推荐下另一片文章:    Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)
http://blog.csdn.net/vipzjyno1/article/details/25248021/



Notification用于在状态栏显示信息。这些信息一般来源于app的消息推送,或应用的一些功能控制(如播放器)

Notification的两种视图


普通视图

借用官方的图片说明一下Notification视图中包括的内容

1. 内容标题

2. 大图标(Bitmap)

3. 正文内容

4. 附加信息

5. 小图标

6. Notification的推送时间

大视图

除了上图中标出的第7点外,其他的基本和普通视图中的一样。

其中第7点可显示为一下几种内容

1. 显示大图片

      在详情区域可以显示一张最大为256dp的图片

2. 显示长文本

      在详情区域显示长文本

3. inbox style(因为不知道中文怎样才能说得准确,这里用英文表达)

        inbox style在详情区域显示多行文本.

4. 大内容标题

        允许用户为扩展视图定义另一个标题。

5. 摘要文本

        允许用户在详情区域添加一行额外的文本

创建标准的Notification


以下三项式创建一个标准的Notification的先决条件

1. 小图标, 通过setSmallIcon()方法设置

2. 标题, 通过setContentTitle()方法设置

3. 内容文本, 通过setContentText()方法设置

我们这里不通过调用Notification构造方法的方式创建Notification,因为这种方法已经不推荐使用。

我们使用通过NotificationCompat.Builder类创建Notification。

程序的主布局文本中只有一个id为btnShow的按钮,点击这个按钮显示Notification,下面为简单的示例demo;

import android.app.Notification;
import android.app.NotificationManager;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

    private Notification mNotification;
    private NotificationManager mNotificationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button btnShow = (Button) findViewById(R.id.btnShow);

        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        mNotification = new NotificationCompat.Builder(this)
                // 设置小图标
                .setSmallIcon(R.drawable.ic_launcher)
                // 设置标题
                .setContentTitle("you have a meeting")
                // 设置内容
                .setContentText("you have a meeting at 3:00 this afternoon")
                .build();

        btnShow.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mNotificationManager.notify(0, mNotification);
            }
        });
    }
}



完整的Notification实例


上面代码中的例子并不完整,只是演示了一个可以正常运行的实例

说它不完整是因为Notification到来的时候状态栏只有图标,没有文字,也没有声音提示,用户体验并不好。

当我们不设置LargeIcon的时候,系统就会使用当前设置的小图标作为大图标使用,小图标位置(位置5)则置空。

下面的代码是比较完整的Notification示例

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.RingtoneManager;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends BarActivity {

    private Notification mNotification;
    private NotificationManager mNotificationManager;
    private PendingIntent mResultIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnShow = (Button) findViewById(R.id.btnShow);

        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        /*
         * 取得PendingIntent, 并设置跳转到的Activity,
         */
        Intent intent = new Intent(this, UpdateActivity.class);
        mResultIntent = PendingIntent.getActivity(this, 1, intent,
                Intent.FLAG_ACTIVITY_NEW_TASK);

        btnShow.setOnClickListener(new View.OnClickListener() {

            @SuppressLint("NewApi")
            @Override
            public void onClick(View v) {

                Bitmap largeIcon = BitmapFactory.decodeResource(
                        MainActivity.this.getResources(), R.drawable.luffy);

                mNotification = new NotificationCompat.Builder(getBaseContext())

                // 设置大图标
                        .setLargeIcon(largeIcon)

                        // 设置小图标
                        .setSmallIcon(R.drawable.ic_launcher)

                        // 设置小图标旁的文本信息
                        .setContentInfo("1")

                        // 设置状态栏文本标题
                        .setTicker("你的系统有更新")

                        // 设置标题
                        .setContentTitle("系统更新")

                        // 设置内容
                        .setContentText("发现系统更新,点击查看详情")

                        // 设置ContentIntent
                        .setContentIntent(mResultIntent)

                        // 设置Notification提示铃声为系统默认铃声
                        .setSound(
                                RingtoneManager.getActualDefaultRingtoneUri(
                                        getBaseContext(),
                                        RingtoneManager.TYPE_NOTIFICATION))

                        // 点击Notification的时候使它自动消失
                        .setAutoCancel(true).build();

                mNotificationManager.notify(0, mNotification);
            }
        });

    }
}


使用自定义布局统一Notification的显示效果


布局文件代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:attr/colorBackground">
    
    <ImageView
        android:id="@+id/largeIcon"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:src="@drawable/luffy"
        android:contentDescription="largeIcon" />

    <TextView
        android:id="@+id/contentTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/largeIcon"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:text="系统更新"/>

    <TextView
        android:id="@+id/contentText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/contentTitle"
        android:layout_below="@+id/contentTitle"
        android:layout_marginTop="5dp"
        android:text="发现系统更新,点击查看详情"
        android:textSize="12sp"/>
    
    <TextView
        android:id="@+id/when"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="5dp"
        android:text="11:00"/>

    <ImageView
        android:id="@+id/smallIcon"
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:layout_alignRight="@+id/when"
        android:layout_alignTop="@+id/contentText"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/contentInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/smallIcon"
        android:layout_toLeftOf="@+id/smallIcon"
        android:text="信息"
        android:textSize="12sp" />

</RelativeLayout>


程序代码

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.media.RingtoneManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;

public class MainActivity extends Activity {

    private Notification mNotification;
    private NotificationManager mNotificationManager;
    private PendingIntent mResultIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnShow = (Button) findViewById(R.id.btnShow);

        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        /*
         * 取得PendingIntent, 并设置跳转到的Activity,
         */
        Intent intent = new Intent(this, UpdateActivity.class);
        mResultIntent = PendingIntent.getActivity(this, 1, intent,
                Intent.FLAG_ACTIVITY_NEW_TASK);

        btnShow.setOnClickListener(new View.OnClickListener() {

            @SuppressLint("NewApi")
            @Override
            public void onClick(View v) {

                RemoteViews customView = new RemoteViews(getPackageName(),
                        R.layout.customerviews);

                mNotification = new NotificationCompat.Builder(getBaseContext())

                         // 设置小图标
                        .setSmallIcon(R.drawable.ic_launcher)

                        // 设置状态栏文本标题
                        .setTicker("你的系统有更新")

                        //设置自定义布局
                        .setContent(customView)

                        // 设置ContentIntent
                        .setContentIntent(mResultIntent)

                        // 设置Notification提示铃声为系统默认铃声
                        .setSound(
                                RingtoneManager.getActualDefaultRingtoneUri(
                                        getBaseContext(),
                                        RingtoneManager.TYPE_NOTIFICATION))

                        // 点击Notification的时候自动移除
                        .setAutoCancel(true).build();

                /*
                 * 当API level 低于14的时候使用setContent()方法是没有用的
                 * 需要对contentView字段直接赋值才能生效
                 */
                if (Build.VERSION.SDK_INT < 14) {
                    mNotification.contentView = customView;
                }

                mNotificationManager.notify(0, mNotification);
            }
        });

    }
}

上面的布局文件中的内容是硬编码的,也可以通过RemoteViews类中的setXXX方法动态设定内容。

这样程序便更加灵活


更新Notification的内容


因为我们创建的Notification是通过一个Id分别的,因此只要在使用NotificationManager的notify()方法的时候使用

相同的Id就可以更新这个Notification的内容

当然在使用notify()方法之前我们还是要创建一个Notification实例。



【转】http://www.bkjia.com/Androidjc/961705.html



猜你喜欢

转载自blog.csdn.net/qq_22078107/article/details/53448535