• android 顶部的通知栏


    package com.notificationTest;

    import android.app.Activity;

    import android.app.Notification;

    import android.app.NotificationManager;

    import android.app.PendingIntent;

    import android.content.Intent;

    import android.os.Bundle;

    import android.view.View;

    import android.widget.Button;

    public class NotificationtestActivity extends Activity {

        private static final int ID = 1213;

        private static final String KEY_COUNT="keyCount";

        private int count;

    /** Called when the activity is first created. */

        @Override

        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.main);

            Intent intent=this.getIntent();

            count=intent.getIntExtra(KEY_COUNT,0);        

           

            this.setTitle("这是第"+Integer.toString(count)+"个");

            

            Button btn=(Button) this.findViewById(R.id.button1);

            btn.setOnClickListener(new View.OnClickListener() {

    @Override

    public void onClick(View v) {

    AddNotification();

    NotificationtestActivity.this.finish();

    }

    });

        }

        /**

        * 添加顶部通知

        * @author liuzhao

        */

        public void AddNotification(){

        count++;

        //添加通知到顶部任务栏

        //获得NotificationManager实例

        String service = NOTIFICATION_SERVICE;

        NotificationManager nm = (NotificationManager)getSystemService(service);

        //实例化Notification

        Notification n = new Notification();

        //设置显示图标

        int icon = R.drawable.icon;

        //设置提示信息

        String tickerText ="我的程序";

        //显示时间

        long when = System.currentTimeMillis();

         

        n.icon = icon;

        n.tickerText = tickerText;

        n.when = when;

        //显示在“正在进行中”

      //  n.flags = Notification.FLAG_ONGOING_EVENT;

        n.flags|=Notification.FLAG_AUTO_CANCEL; //自动终止

        //实例化Intent

        Intent it = new Intent(this,NotificationtestActivity.class);

        it.putExtra(KEY_COUNT, count);

        /*********************

         *获得PendingIntent  

         *FLAG_CANCEL_CURRENT:

         * 如果当前系统中已经存在一个相同的PendingIntent对象,

         * 那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。 

    *FLAG_NO_CREATE:

    * 如果当前系统中不存在相同的PendingIntent对象,

    * 系统将不会创建该PendingIntent对象而是直接返回null。 

         *FLAG_ONE_SHOT:

         * 该PendingIntent只作用一次,

         * 如果该PendingIntent对象已经触发过一次,

         * 那么下次再获取该PendingIntent并且再触发时,

         * 系统将会返回一个SendIntentException,在使用这个标志的时候一定要注意哦。 

    *FLAG_UPDATE_CURRENT:

    * 如果系统中已存在该PendingIntent对象,

    * 那么系统将保留该PendingIntent对象,

    * 但是会使用新的Intent来更新之前PendingIntent中的Intent对象数据,

    * 例如更新Intent中的Extras。这个非常有用,

    * 例如之前提到的,我们需要在每次更新之后更新Intent中的Extras数据,

    * 达到在不同时机传递给MainActivity不同的参数,实现不同的效果。 

         *********************/

         

        PendingIntent pi = PendingIntent.getActivity(this, 0, it, PendingIntent.FLAG_UPDATE_CURRENT);

        

        //设置事件信息,显示在拉开的里面

        n.setLatestEventInfo(NotificationtestActivity.this,"我的软件"+Integer.toString(count), "我的软件正在运行……", pi);

        //发出通知

        nm.notify(ID,n);

        }

    }

  • 相关阅读:
    每天一个linux命令(9):touch
    每天一个linux命令(8):rm
    每天一个linux命令(7):mv
    每天一个linux命令(6):cp
    每天一个linux命令(5):rmdir
    每天一个linux命令(4):mkdir
    区块链技术与应用——BTC挖矿难度
    区块链技术与应用——BTC网络工作原理
    区块链技术与应用——BTC系统实现
    区块链技术与应用——BTC的共识协议
  • 原文地址:https://www.cnblogs.com/meieiem/p/2178842.html
Copyright © 2020-2023  润新知