Android 里的各种通信方式(三)

四:notifation 通知

  Notifation通知栏会在屏幕上方向用户提示信息 但是不会打断用户正在阅读的内容,除非用户手动将 Notifation通知栏拉下。 Notifation的好处就是在于不会影响用户的操作,比如用户正在阅读非常重要的信息这时候帮他直接打开一个activity会非常不合适 因为直接影响到了他当时的操作行为 所以Notifation就出来了。建议大家在开发中遇到可能打断用户使用的情况下都去使用Notifation通知栏。

public class NotificationActivity extends Activity {    

    NotificationManager mManager = null;    

    Notification notification =null;    

    @Override    

    protected void onCreate(Bundle savedInstanceState) {    

    setContentView(R.layout.notification);    

    // 得到通知消息的管理器对象,负责管理 Notification 的发送与清除消息等    

    mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // 创建Notification对象 参数分别代表 通知栏 中显示的图标 显示的标题 显示的时间    

    notification = new Notification(R.drawable.jay,    

    "Android专业开发群", System.currentTimeMillis());    

    // 设置在通知栏中点击后Notification自动消失    

    notification.flags = Notification.FLAG_AUTO_CANCEL;    

    //设置点击后转跳的新activity    

    Intent intent = new Intent(this, MyShowActivity.class);    

    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);    

    //通过bundle可以带一些数据过去 这里将字符串传递了过去    

    Bundle bundle = new Bundle();    

    bundle.putString("name", "从Notification转跳过来的");    

    intent.putExtras(bundle);       

    //设置通知栏中显示的内容    

    PendingIntent contentIntent = PendingIntent.getActivity(this,    

        R.string.app_name, intent, PendingIntent.FLAG_UPDATE_CURRENT);    

    notification.setLatestEventInfo(this, "Android专业开发群",    

     "QQ群号 164257885", contentIntent);    

    Button button0 = (Button)findViewById(R.id.button0);    

    button0.setOnClickListener(new OnClickListener() {        

        @Override    

        public void onClick(View arg0) {    

        //打开这个Notification通知    

        mManager.notify(0, notification);    

        }    

    });     

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

    button1.setOnClickListener(new OnClickListener() {      

        @Override    

        public void onClick(View arg0) {    

        //关闭这个Notification通知    

        mManager.cancelAll();    

        }    

    });      

    super.onCreate(savedInstanceState);    

    }   

}    

 

 

转载于:https://my.oschina.net/u/2971691/blog/798167

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值