[控件]NotificationManager提示通知

在main2中直接finish不做操作
package a.b;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class main extends Activity implements OnClickListener {
    Button btn1, btn2, btn3;
    NotificationManager myManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // 初始化对象
        myManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.btn1:
                notify(1);
                break;
            case R.id.btn2:
                notify(2);
                break;
            case R.id.btn3:
                notify(3);
                break;
        }
    }

    void notify(int type) {
        // 创建新的intent作为单击notification留言条
        Intent notifyIntent = new Intent(this, main2.class);
        // 创建一个新的栈
        notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        // 创建一个pendingintent
        PendingIntent appintent = PendingIntent.getActivity(main.this, 0,
                notifyIntent, 0);
        // 创建Notification
        Notification mynoti = new Notification();
        // 设置图片
        mynoti.icon = R.drawable.icon;
        // 设置文字
        mynoti.tickerText = "标题栏通知文字" + type;
        // 设置发出默认声音
        mynoti.defaults = Notification.DEFAULT_SOUND;
        // 设置Notification留言条参数
        mynoti.setLatestEventInfo(main.this, "通知图片旁的文字", "下方提示文字", appintent);
        //Notification.FLAG_NO_CLEAR;不能被点击清除或选中消失
        //Notification.FLAG_AUTO_CANCEL;点击清除或选中消失
        //Notification.FLAG_ONGOING_EVENT将通知放置在"正在进行的"中  
        //Notification.FLAG_INSISTENT声音一直播放直到通知被取消
        //其中ledARGB 表示灯光颜色、 ledOnMS 亮持续时间、ledOffMS 暗的时间。
        //Notification.FLAG_SHOW_LIGHTS LED闪动
        //第一个为id,如果id相同则会覆盖上一个通知        myManager.notify(0, mynoti);
        //使用 myManager.cancel(id);来清除通知    
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值