Android 讲解:通知栏

Android 通知栏是很多主流软件使用的功能,使用非常简单,4行代码就可以完成一个简单的通知栏。

简单的通知栏

MainActivity.java

package cn.met0.android.chapter3;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);




        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        NotificationManager manage = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        Notification notification = 
          new Notification(R.drawable.ic_launcher, "you have new message", System.currentTimeMillis());

        notification.setLatestEventInfo(this, "this is title", "this is text", null);

        manage.notify( 1, notification);

    }

}

通知栏代码都在public void onClick(View v)方法里面。

(NotificationManager) getSystemService(NOTIFICATION_SERVICE); 获取一个系统通知栏管理器,Android 的所有程序通知栏的显示和消失都由这个对象管理。

new Notification(R.drawable.ic_launcher, "you have new message", System.currentTimeMillis()); 创建一个通知栏对象。

ParamInfo
R.drawable.ic_launcher通知栏显示图标
“you have new message”通知栏的消息提醒信息
System.currentTimeMillis()默认

notification.setLatestEventInfo(this, "this is title", "this is text", null); 设置通知栏的下拉显示的信息。

ParamInfo
this本Activity
“this is title”通知栏的消息提醒标题
null通知栏的点击后的Intent 。只是提醒信息就为空。

manage.notify( 1, notification); 显示通知栏

ParamInfo
1通知栏的标示id,调用manager.cancel(1); 就能把这个通知栏给消除了
notification通知栏对象
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值