Notification通知栏的用法

    xiaoxiao2021-03-25  72

    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; } } }); } }
    转载请注明原文地址: https://ju.6miu.com/read-34282.html

    最新回复(0)