前台服务
Android 13及以上系统需要动态获取通知权限。
//android 13及以上系统动态获取通知权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {checkPostNotificationPermission();
}
private void checkPostNotificationPermission() {if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS)!= PackageManager.PERMISSION_GRANTED) {ActivityCompat.requestPermissions((Activity) this, new String[]{Manifest.permission.POST_NOTIFICATIONS}, 200);} else if (manager != null) {//通知ID设置(您可以使用任何您想要的ID,相同ID通知只会显示一个)。manager.notify(2, builder.build());}
}@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {super.onRequestPermissionsResult(requestCode, permissions, grantResults);if (requestCode == 200) {if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {//允许了通知权限} else {Toast.makeText(this, "您拒绝了通知权限", Toast.LENGTH_SHORT).show();}}
}
别忘记添加通知权限
<!--发布通知权限-->
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
开启前台服务需要添加前台服务权限
<!--前台服务权限-->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
通知震动需要添加震动权限
<!--振动器权限-->
<uses-permission android:name="android.permission.VIBRATE" />
前台服务代码
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.IBinder;import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;import com.example.javatest.MainActivity;
import com.example.javatest.R;/*** created by cwj on 2023-10-19* Description: 前台服务*/
public class ForegroundService extends Service {@Overridepublic void onCreate() {super.onCreate();if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {CharSequence name = getString(R.string.app_name);int importance = NotificationManager.IMPORTANCE_DEFAULT;NotificationChannel channel = new NotificationChannel("CHANNEL_ID", name, importance);channel.setDescription("前台服务守护进程");NotificationManager notificationManager = getSystemService(NotificationManager.class);notificationManager.createNotificationChannel(channel);}//设置服务跳转Intent intent = new Intent(this, MainActivity.class);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "CHANNEL_ID")// 设置通知的小图标。.setSmallIcon(R.mipmap.ic_launcher)//设置通知的标题。.setContentTitle(getString(R.string.app_name))//设置通知的内容.setContentText("进程守护中")//设置通知的优先级。.setPriority(NotificationCompat.PRIORITY_DEFAULT)//震动模式的通知在Android O及以上。.setVibrate(new long[]{0, 1000, 500, 1000})//在Android O及以上的系统上使用LED光色和微信号。.setLights(Color.RED, 1000, 1000)//设置Android O及以上版本的通知声音。.setSound(Uri.parse("android.resource://" + this.getPackageName() + "/raw/notification_sound"))//如果需要,在Android O及以上版本设置较大的通知标题。// .setLargeTitle("Large Title Text")//如果需要,在Android O及以上版本设置通知的子文本。// .setSubText("Sub Text")//如果需要,在Android O及以上版本设置大字体通知。// .setStyle(new NotificationCompat.BigTextStyle().bigText("Big Text"))//如果需要,在Android O及以上版本设置通知摘要文本(当有多个具有相同优先级的通知时显示摘要文本)。// .setSummaryText("Summary Text")//如果需要,在通知中添加动作按钮(可用于启动活动或发送广播)。// .addAction(R.drawable.ic_action, "Action Title", PendingIntent)//设置可点击跳转.setContentIntent(pendingIntent);// 将通知设置为前台服务startForeground(1, builder.build());}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// 在这里执行需要在前台运行的任务// 返回START_STICKY表示服务在被杀死后会自动重启return START_STICKY;}@Nullable@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onDestroy() {super.onDestroy();// 停止前台服务stopForeground(true);stopSelf();}
}
application中添加服务
<serviceandroid:name=".service.ForegroundService"android:enabled="true"android:exported="false" />
activity中启动服务
Intent intent = new Intent(this, ForegroundService.class);
// Android 8.0使用startForegroundService在前台启动新服务
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {startForegroundService(intent);
} else {startService(intent);
创建通知
模拟发送通知
binding.iv.setOnClickListener(v ->showNotification(getString(R.string.app_name), dateToString(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss") + "您收到一条消息")
);
创建通知及通知点击取消通知
private NotificationCompat.Builder builder;
private NotificationManagerCompat manager;private void showNotification(String title, String content) {// 创建通知频道,如果用户没有创建,则会自动创建。String CHANNEL_ID = "message";if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {if (getSystemService(NotificationManager.class).getNotificationChannel(CHANNEL_ID) == null) {NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "消息", NotificationManager.IMPORTANCE_DEFAULT);//通知的振动模式。channel.setVibrationPattern(new long[]{0, 1000, 500, 1000});//为通知通道启用振动。channel.enableVibration(true);//设置通道的颜色。channel.setLightColor(Color.RED);//设置通道的锁屏可见性。channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);getSystemService(NotificationManager.class).createNotificationChannel(channel);}}//设置服务跳转Intent intent = new Intent(this, MainActivity.class);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);PendingIntent pendingIntent = PendingIntent.getActivity(this, 2, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);// 创建并分发通知。builder = new NotificationCompat.Builder(this, CHANNEL_ID)// 设置通知的小图标。.setSmallIcon(R.mipmap.ic_launcher)//设置通知的标题。.setContentTitle(title)//设置通知的内容.setContentText(content)//设置通知的优先级。.setPriority(NotificationCompat.PRIORITY_DEFAULT)//震动模式的通知在Android O及以上。.setVibrate(new long[]{0, 1000, 500, 1000})//在Android O及以上的系统上使用LED光色和微信号。.setLights(Color.RED, 1000, 1000)//设置Android O及以上版本的通知声音。.setSound(Uri.parse("android.resource://" + this.getPackageName() + "/raw/notification_sound"))//如果需要,在Android O及以上版本设置较大的通知标题。// .setLargeTitle("Large Title Text")//如果需要,在Android O及以上版本设置通知的子文本。// .setSubText("Sub Text")//如果需要,在Android O及以上版本设置大字体通知。// .setStyle(new NotificationCompat.BigTextStyle().bigText("Big Text"))//如果需要,在Android O及以上版本设置通知摘要文本(当有多个具有相同优先级的通知时显示摘要文本)。// .setSummaryText("Summary Text")//如果需要,在通知中添加动作按钮(可用于启动活动或发送广播)。// .addAction(R.drawable.ic_action, "Action Title", PendingIntent)//设置可点击跳转.setContentIntent(pendingIntent)//开启点按通知后自动移除通知.setAutoCancel(true);manager = NotificationManagerCompat.from(this);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {checkPostNotificationPermission();} else {// 在 Android 10 或更早版本中,不需要请求此权限。//ID要和前台服务ID区分开,相同ID只能显示一条通知。manager.notify(2, builder.build());}
activity完整代码
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;import android.Manifest;
import android.app.Activity;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;import com.example.javatest.databinding.ActivityMainBinding;
import com.example.javatest.service.ForegroundService;import java.text.SimpleDateFormat;
import java.util.Date;public class MainActivity extends AppCompatActivity {private ActivityMainBinding binding;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);binding = ActivityMainBinding.inflate(getLayoutInflater());setContentView(binding.getRoot());initView();initData();}private void initView() {//android 13及以上系统动态获取通知权限if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {checkPostNotificationPermission();}Intent intent = new Intent(this, ForegroundService.class);// Android 8.0使用startForegroundService在前台启动新服务if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {startForegroundService(intent);} else {startService(intent);}}private void initData() {binding.iv.setOnClickListener(v ->showNotification(getString(R.string.app_name), dateToString(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss") + "您收到一条消息"));}private String dateToString(long time, String format) {Date date = new Date(time);SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);return simpleDateFormat.format(date);}private NotificationCompat.Builder builder;private NotificationManagerCompat manager;private void showNotification(String title, String content) {// 创建通知频道,如果用户没有创建,则会自动创建。String CHANNEL_ID = "message";if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {if (getSystemService(NotificationManager.class).getNotificationChannel(CHANNEL_ID) == null) {NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "消息", NotificationManager.IMPORTANCE_DEFAULT);//通知的振动模式。channel.setVibrationPattern(new long[]{0, 1000, 500, 1000});//为通知通道启用振动。channel.enableVibration(true);//设置通道的颜色。channel.setLightColor(Color.RED);//设置通道的锁屏可见性。channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);getSystemService(NotificationManager.class).createNotificationChannel(channel);}}//设置服务跳转Intent intent = new Intent(this, MainActivity.class);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);PendingIntent pendingIntent = PendingIntent.getActivity(this, 2, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);// 创建并分发通知。builder = new NotificationCompat.Builder(this, CHANNEL_ID)// 设置通知的小图标。.setSmallIcon(R.mipmap.ic_launcher)//设置通知的标题。.setContentTitle(title)//设置通知的内容.setContentText(content)//设置通知的优先级。.setPriority(NotificationCompat.PRIORITY_DEFAULT)//震动模式的通知在Android O及以上。.setVibrate(new long[]{0, 1000, 500, 1000})//在Android O及以上的系统上使用LED光色和微信号。.setLights(Color.RED, 1000, 1000)//设置Android O及以上版本的通知声音。.setSound(Uri.parse("android.resource://" + this.getPackageName() + "/raw/notification_sound"))//如果需要,在Android O及以上版本设置较大的通知标题。// .setLargeTitle("Large Title Text")//如果需要,在Android O及以上版本设置通知的子文本。// .setSubText("Sub Text")//如果需要,在Android O及以上版本设置大字体通知。// .setStyle(new NotificationCompat.BigTextStyle().bigText("Big Text"))//如果需要,在Android O及以上版本设置通知摘要文本(当有多个具有相同优先级的通知时显示摘要文本)。// .setSummaryText("Summary Text")//如果需要,在通知中添加动作按钮(可用于启动活动或发送广播)。// .addAction(R.drawable.ic_action, "Action Title", PendingIntent)//设置可点击跳转.setContentIntent(pendingIntent)//开启点按通知后自动移除通知.setAutoCancel(true);manager = NotificationManagerCompat.from(this);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {checkPostNotificationPermission();} else {// 在 Android 10 或更早版本中,不需要请求此权限。//显示ID为1的通知(您可以使用任何您想要的ID)。manager.notify(2, builder.build());}}private void checkPostNotificationPermission() {if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS)!= PackageManager.PERMISSION_GRANTED) {ActivityCompat.requestPermissions((Activity) this, new String[]{Manifest.permission.POST_NOTIFICATIONS}, 200);} else if (manager != null) {//显示ID为1的通知(您可以使用任何您想要的ID)。manager.notify(2, builder.build());}}@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {super.onRequestPermissionsResult(requestCode, permissions, grantResults);if (requestCode == 200) {if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {//允许了通知权限} else {Toast.makeText(this, "您拒绝了通知权限", Toast.LENGTH_SHORT).show();}}}
}
完整配置文件AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"><!--网络权限--><uses-permission android:name="android.permission.INTERNET" /><!--前台服务权限--><uses-permission android:name="android.permission.FOREGROUND_SERVICE" /><!--振动器权限--><uses-permission android:name="android.permission.VIBRATE" /><!--全屏意图权限--><uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" /><!--发布通知权限--><uses-permission android:name="android.permission.POST_NOTIFICATIONS" /><applicationandroid:allowBackup="true"android:dataExtractionRules="@xml/data_extraction_rules"android:fullBackupContent="@xml/backup_rules"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.JavaTest"tools:targetApi="31"><activityandroid:name=".MainActivity"android:exported="true"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><serviceandroid:name=".service.ForegroundService"android:enabled="true"android:exported="false" /></application></manifest>
build.gradle.kts文件中添加viewBinding支持
buildFeatures{viewBinding = true
}