前台代码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); } }
里面有一个部分需要跳转