Android 讲解:通知栏

来源:互联网 发布:通过网络社会治理案例 编辑:程序博客网 时间:2024/05/22 10:52

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()); 创建一个通知栏对象。

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

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

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

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

Param Info 1 通知栏的标示id,调用manager.cancel(1); 就能把这个通知栏给消除了 notification 通知栏对象
0 0
原创粉丝点击