现在的位置: 首页 > 综合 > 正文

[控件]NotificationManager提示通知

2013年10月23日 ⁄ 综合 ⁄ 共 1847字 ⁄ 字号 评论关闭

在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);来清除通知    
    }
}

抱歉!评论已关闭.