Notification的用法

来源:互联网 发布:一首歌火了网络歌手 编辑:程序博客网 时间:2024/05/21 16:14

Notification是通过NotificationManger来管理的发送通知
要记住一个服务System.NOTIFICATION_SERVICE
java代码

package com.phone.hty.myapplication;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;public class MainActivity extends AppCompatActivity {    private NotificationManager mNm;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mNm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);    }    public void next(View view) {        Intent intent = new Intent(this, TabActivity.class);        PendingIntent pi = PendingIntent.getActivities(this, 0, new Intent[]{intent}, 0);        Notification notification = new Notification.Builder(this)                .setAutoCancel(true)                .setTicker("小马哥来了")                .setSmallIcon(R.drawable.aa)                .setContentTitle("大好人")                .setContentText("总打时手有点累,还好腰不酸,头不痛")                .setWhen(System.currentTimeMillis())                .setContentIntent(pi)                .build();        mNm.notify(0x123, notification);    }    public void cencel(View view) {        mNm.cancel(0x123);    }}
0 0
原创粉丝点击