Notification通知栏的用法

来源:互联网 发布:下载识谱软件 编辑:程序博客网 时间:2024/06/07 15:44
public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Button sendNotice=(Button)findViewById(R.id.send_notice);        sendNotice.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                switch (view.getId()){                    case R.id.send_notice:                        NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);//获取NotificationManager                        NotificationCompat.Builder builder=new NotificationCompat.Builder(MainActivity.this);//创建NotificationCompat.Builder                        builder.setSmallIcon(R.mipmap.ic_launcher);//图标                        builder.setTicker("You have a  new message");//瞬时一过的信息                        builder.setWhen(System.currentTimeMillis());//瞬时显示时间                        builder.setContentTitle("This is title");//标题                        builder.setContentText("This is text");//内容                        builder.setAutoCancel(true);//点击消失                        Notification notification=builder.build();//使用Builder创建通知                        manager.notify(1,notification);//显示一个通知                        break;                    default:                        break;                }            }        });    }}
0 0
原创粉丝点击