android 顶部消息通知和Toast顶部弹窗消息提醒

1.顶部通知栏的消息通知

 Intent intent = new Intent(mContext, 跳转到哪个Activity.class); //点击了之后进入的一个Actity
        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        Notification noti = new Notification.Builder(mContext)
                .setTicker("名称")
                .setContentTitle("标题")
                .setContentText("时间")
                .setSmallIcon(R.drawable.app_icon)//设置图标
                .setAutoCancel(true)//点击之后消失
                .setDefaults(Notification.DEFAULT_SOUND)//设置声音
                .setContentIntent(pendingIntent)//点击之后的页面
                .build();
        NotificationManager manager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(1,noti);//id控制第几条,相同的id会替换掉上一次的消息通知

2.toast弹窗通知

1.主代码
        View layoutView =  LayoutInflater.from(context).inflate(R.layout.toast_top, null);
        //设置文本的参数 设置加载文本文件的参数,必须通过LayoutView获取。
        TextView timeView = layoutView.findViewById(R.id.tv_assist_toast_time);
        TextView contentView = layoutView.findViewById(R.id.tv_assist_toast_content);
        TextView titletView = layoutView.findViewById(R.id.tv_assist_toast_title);
        timeView.setText("时间");
        titletView.setText("头部提示");
        contentView.setText("消息内容");
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        //获得屏幕的宽度
        int width = wm.getDefaultDisplay().getWidth();
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置TextView的宽度为 屏幕宽度
        layoutView.setLayoutParams(layoutParams);
        //获得屏幕的宽度
        //创建toast对象,
        Toast toast = new Toast(context);
        //把要Toast的布局文件放到toast的对象中
        toast.setView(layoutView);
        toast.setDuration(toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP, 0, 0);
        toast.getView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);//设置Toast可以布局到系统状态栏的下面
        toast.show();
2.通知页面的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_margin="10dp"
        android:background="@drawable/app_icon"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_assist_toast_title"
                android:layout_width="wrap_content"
                android:layout_height="25dp"
                android:textStyle="bold"
                android:text="远程协助申请"
                android:layout_gravity="center"
                android:textSize="18dp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <TextView
                android:id="@+id/tv_assist_toast_time"
                android:layout_width="wrap_content"
                android:layout_height="25dp"
                android:layout_marginRight="10dp"
                android:layout_gravity="center"
                android:textStyle="bold"/>

        </LinearLayout>

        <TextView
            android:id="@+id/tv_assist_toast_content"
            android:layout_width="match_parent"
            android:layout_height="20dp"/>

    </LinearLayout>



</LinearLayout>

猜你喜欢

转载自blog.csdn.net/zhaohan___/article/details/82587675