Android 通知的基本用法

Android 通知的基本用法

在第一行的代码第二部的基础上,学习通知已经不够用了,需要对通知进行新的学习,主要是为了对不同机型的适配进行处理。

1.修改xml布局如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/send_notice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="send message"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

代码很简单,就是一个简单Button按钮。

2.修改MainActivity代码如下:
功能实现分为以下几下部分:
(1)实例化Button和 NotificationManager,为Button设立点击事件
(2)判断Android sdk的版本
(3) NotificationCompat.Builder建立一个通知
(4)为通知添加标题,内容,图标,点击事件等属性
这里新建了一个otherActivity,利用PendingIntent为通知设置点击事件,当点击通知之后就会跳转到otherActivity去,由于注释比较详细就不多做解释。

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    NotificationManager manager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button send =(Button)findViewById(R.id.send_notice);
        send.setOnClickListener(this);
        manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.send_notice:
                /**
                 *点击通知后跳转到otherActivity界面
                 */
                Intent intent = new Intent(this,otherActivity.class);
                PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);
                //Android sdk解决兼容问题
                // 高版本需要渠道
                if(Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
                    //只在Android O之上需要渠道
                    NotificationChannel notificationChannel = new NotificationChannel("channelid1",
                            "channelname",NotificationManager.IMPORTANCE_HIGH);
                    //如果这里用IMPORTANCE_NOENE就需要在系统的设置里面开启渠道,通知才能正常弹出
                    manager.createNotificationChannel(notificationChannel);
                }
                //新版的Notifiction 需要channelld
                NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this,"channelid1");
                builder.setSmallIcon(R.drawable.music)
                        .setContentTitle("网抑云向你发来多人运动邀请!")
                    .setContentText("")
                    .setStyle(new NotificationCompat.BigTextStyle().bigText("hello every one, 今天" +
                            "你有网抑云吗?come some music!"))//长文本
                    .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(
                            getResources(),R.drawable.music)))//放大图,利用BitmapFactory解析成大图
                    .setAutoCancel(true)//点击之后消失
                    .setVibrate(new long[]{0,1000,1000,1000})//震动
                    .setLights(Color.GREEN,1000,1000)//绿灯闪闪
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)//重要程度
                    .setCategory(NotificationCompat.CATEGORY_MESSAGE)//紧急消息,
                    .setContentIntent(pi);//响应跳转界面

                manager.notify(1,builder.build());//每个通知需要一个独特的ID
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值