Android中通知的简单使用

本文介绍了在Android 8.0及以上和以下版本如何使用通知,包括在Android 8.0中引入的通知渠道概念,强调了通知渠道对用户体验的改善和管理。同时提供了兼容不同Android版本的通知代码示例。
摘要由CSDN通过智能技术生成

由于在Android 8.0之后,对通知进行过重大的调整和加入了一些新的功能,所以本篇文章将分别介绍,在Android 8.0中和在Android 8.0以下的版本中怎么使用通知,以及怎么在Android 8.0和Android 8.0以下的版本中兼容使用通知。

在Android 8.0以下的版本中使用通知,代码如下:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
   
    private Button mBtn_Send_Notice;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBtn_Send_Notice = findViewById(R.id.btn_send_notification);
        mBtn_Send_Notice.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
   
        switch (v.getId()) {
   
            case R.id.btn_send_notification:
                // 调用getSystemService()方法得到一个NotificationManager的实例
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

                Intent intent = new Intent(MainActivity.this, DisplayActivity.class);
                // 设置一个待定意图,因为我们是在活动中跳转,所以使用getActivity()方法得到实例,在广播中和服务中应使用getBroadcast()和getService来得到实例
                PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);

                NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值