消息提示

前台代码1:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:text="发送消息"
        android:onClick="fasong"></Button>

    <Button
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:text="发送进度条"
        android:onClick="jindu"></Button>

    <Button
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:text="发送自定义"
        android:onClick="zidingyi"></Button>

</LinearLayout>

后台代码1:

package com.bw.day03;


import androidx.appcompat.app.AppCompatActivity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void fasong(View view) {
        //加入消息跳转的页面
        Intent intent = new Intent(this, MainActivity2.class);
        PendingIntent activity = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        //初始化消息构建对象
        Notification notification = new Notification.Builder(this)
                .setContentTitle("测试消息")
                .setSmallIcon(R.drawable.xiaoyi)
                .setContentText("发送一条测试消息")
                .setContentIntent(activity)
                .build();

        //获取消息管理对象
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //发送消息
        manager.notify(1,notification);


    }
    int process = 0;
    public void jindu(View view) {
        Notification.Builder builder = new Notification.Builder(this).setContentTitle("下载文件")
                .setContentText("正在下载中...")
                .setSmallIcon(R.drawable.xiaoyi)
                .setProgress(100,process,false);
        Notification notification = builder.build();
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //发送消息
        manager.notify(2,notification);
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                process+=10;
                builder.setProgress(100,process,false);
                manager.notify(2,builder.build());
                if (process == 100){
                    timer.cancel();
                }
            }
        },0,1000);
    }

    public void zidingyi(View view) {
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notice);
        Notification notification = new Notification.Builder(this)
                .setContentTitle("自定义消息")
                .setSmallIcon(R.drawable.xiaoyi)
                .setCustomContentView(remoteViews)
                .build();
        //获取消息管理对象
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //发送消息
        manager.notify(3,notification);
    }
}

前台代码2:

<?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=".MainActivity2">

</androidx.constraintlayout.widget.ConstraintLayout>

后台代码2:

package com.bw.day03;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    }
}

里面有一个部分需要跳转

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值