《第一行代码》第八章——多媒体

Notification(通知)功能是Android系统中当应用不在前台运行的时候通过状态栏向用户推送信息的一种方式。

通知的创建倾向于在广播接收器和服务中,一般在前台的应用是不会在活动当中使用通知的(因为已经在显示屏上直接提示就可以了,效果会比通知更好吧……)

NotificationManager类用来管理通知 通过调用Context中的getSystemService( )来获取,该方法获取一个字符串作为参数来确定调用哪一个系统服务。这里传入Context.NOTIFICATION_SERVICE就可以了。

为了解决各个版本的兼容问题,我们可以使用support-v4库中的NotificationCompact类来创建对象。

其中基本设置大致包含5个方法:

设置标题
.setContentTitle("This is a content title")
设置内容
.setContentText("This is a content text")
设置通知显示时间
.setWhen(System.currentTimeMillis())
设置通知栏小图标
.setSmallIcon(R.mipmap.ic_launcher)
设置通知栏大图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))

书本例子上把大小图标都设置成同一个了,这种做法仅供学习实验的时候使用,实际项目中应当分别设置特定分辨率的图片才好。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button sendNotice = (Button) findViewById(R.id.send_notice);
//        将 MainActivity 继承为 OnClickListener 的子类
        sendNotice.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.send_notice:
//                获得系统通知服务
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//                 设置 notification 的各项基本属性
                Notification notification = new NotificationCompat.Builder(this)
//                        设置标题
                        .setContentTitle("This is a content title")
//                        设置内容
                        .setContentText("This is a content text")
//                        设置通知显示时间
                        .setWhen(System.currentTimeMillis())
//                        设置通知栏小图标
                        .setSmallIcon(R.mipmap.ic_launcher)
//                        设置通知栏大图标
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                        .build();
                manager.notify(1, notification);//id = 1 这里要求所有 id 不同,不能重复。
                break;
            default:
                break;
        }
    }
}

接下来还有一个问题就是“如何使这个通知可以被点击?”

为了实现点击触发,首先需要新建一个activity,然后在notification的设置中调用setContentIntent( Intent pendingIntent)方法传入已经预先创建好的intent来启动自己的活动。

//代码片段
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.send_notice:
                Intent intent=new Intent(this,NotificationActivity.class);
                PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);
//                获得系统通知服务
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//                 设置 notification 的各项基本属性
                Notification notification = new NotificationCompat.Builder(this)
//                        设置标题
                        .setContentTitle("这是一个测试")
//                        设置内容
                        .setContentText("点击以获取详细信息。")
//                        设置通知显示时间
                        .setWhen(System.currentTimeMillis())
//                        设置通知栏小图标
                        .setSmallIcon(R.mipmap.ic_launcher)
//                        将 pendingIntent 活动设置为点击启动活动
                        .setContentIntent(pendingIntent)
//                        设置通知栏大图标
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                        .build();
                manager.notify(1, notification);//id = 1 这里要求所有 id 不同,不能重复。
                break;
            default:
                break;
        }
    }

在 " case R.id.send_notice" 行下面创建了一个PendingIntent 类对象

在 " .sewtContentIntent( ) " 行下面将该对象设置为ContentIntent,实现了在点击notification之后会创建一个新的Activity

更多关于本章的内容将在下次更新。

你可能感兴趣的:(读书笔记)