gpt4 book ai didi

android - 粘性前台服务不粘

转载 作者:行者123 更新时间:2023-11-29 23:09:08 27 4
gpt4 key购买 nike

我的 onStartCommand 函数:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

Intent notifIntent = new Intent(this,MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0,notifIntent,0);
notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
contentView = new RemoteViews(getPackageName(), R.layout.download_notification_bar);
contentView.setImageViewResource(R.id.image, R.mipmap.ic_launcher);
contentView.setTextViewText(R.id.title, "Custom notification");


notification = new NotificationCompat.Builder(this,CHANEL_ID)
.setContentTitle("test")
.setContentText("test Againg")
.setContent(contentView)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentIntent(pendingIntent)
.build();

startForeground(1,notification);

return START_STICKY;

}

但是可以删除显示在屏幕顶部的通知(向外滑动):

enter image description here enter image description here

最佳答案

从 Android O 开始,您应该从 startForegroundService() 开始,但更好的方法是使用 ContextCompat.startForegroundService()

Andorid Q开始需要添加权限:

<!-- Android Q requirement -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

以下是跨 Android 版本的前台服务的最小设置:

Kotlin :

class ForegroundServiceSample : Service() {

companion object {
@JvmStatic
fun start(context: Context) {
ContextCompat.startForegroundService(context, Intent(context, ForegroundServiceSample::class.java))
}

@JvmStatic
fun stop(context: Context) {
context.stopService(Intent(context, ForegroundServiceSample::class.java))
}
}

// Foreground service notification =========

private val foregroundNotificationId: Int = (System.currentTimeMillis() % 10000).toInt()
private val foregroundNotification by lazy {
NotificationCompat.Builder(this, foregroundNotificationChannelId)
.setSmallIcon(R.drawable.ic_sample_service)
.setPriority(NotificationCompat.PRIORITY_MIN)
.setSound(null)
.build()
}
private val foregroundNotificationChannelName by lazy {
getString(R.string.sample_service_name)
}
private val foregroundNotificationChannelDescription by lazy {
getString(R.string.sample_service_description)
}
private val foregroundNotificationChannelId by lazy {
"ForegroundServiceSample.NotificationChannel".also {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
(getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).apply {
if (getNotificationChannel(it) == null) {
createNotificationChannel(NotificationChannel(
it,
foregroundNotificationChannelName,
NotificationManager.IMPORTANCE_MIN
).also {
it.description = foregroundNotificationChannelDescription
it.lockscreenVisibility = NotificationCompat.VISIBILITY_PRIVATE
it.vibrationPattern = null
it.setSound(null, null)
it.setShowBadge(false)
})
}
}
}
}
}


// Lifecycle ===============================

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(foregroundNotificationId, foregroundNotification)
}
return START_STICKY
}

override fun onBind(intent: Intent?): IBinder? {
return null
}

}

Java:

public class ForegroundServiceSample extends Service {

public static void start(Context context) {
ContextCompat.startForegroundService(context, new Intent(context, ForegroundServiceSample.class));
}

public static void stop(Context context) {
context.stopService(new Intent(context, ForegroundServiceSample.class));
}


// Foreground service notification =========

private final static int foregroundNotificationId = (int) (System.currentTimeMillis() % 10000);

// Notification
private static Notification foregroundNotification = null;
public Notification getForegroundNotification() {
if (foregroundNotification == null) {
foregroundNotification = new NotificationCompat.Builder(getApplicationContext(), getForegroundNotificationChannelId())
.setSmallIcon(R.drawable.ic_sample_service)
.setPriority(NotificationCompat.PRIORITY_MIN)
.setSound(null)
.build();
}
return foregroundNotification;
}

// Notification channel name
private static String foregroundNotificationChannelName = null;
public String getForegroundNotificationChannelName() {
if (foregroundNotificationChannelName == null) {
foregroundNotificationChannelName = getString(R.string.sample_service_name);
}
return foregroundNotificationChannelName;
}


// Notification channel description
private static String foregroundNotificationChannelDescription = null;
public String getForegroundNotificationChannelDescription() {
if (foregroundNotificationChannelDescription == null) {
foregroundNotificationChannelDescription = getString(R.string.sample_service_description);
}
return foregroundNotificationChannelDescription;
}

// Notification channel id
private String foregroundNotificationChannelId = null;
public String getForegroundNotificationChannelId() {
if (foregroundNotificationChannelId == null) {
foregroundNotificationChannelId = "ForegroundServiceSample.NotificationChannel";
// Android O+ channel is a requirement
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Not exists so we create it at first time
if (manager.getNotificationChannel(foregroundNotificationChannelId) == null) {
NotificationChannel nc = new NotificationChannel(
getForegroundNotificationChannelId(),
getForegroundNotificationChannelName(),
NotificationManager.IMPORTANCE_MIN
);
// Discrete notification setup
manager.createNotificationChannel(nc);
nc.setDescription(getForegroundNotificationChannelDescription());
nc.setLockscreenVisibility(NotificationCompat.VISIBILITY_PRIVATE);
nc.setVibrationPattern(null);
nc.setSound(null, null);
nc.setShowBadge(false);
}
}
}
return foregroundNotificationChannelId;
}


// Lifecycle ===============================

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(foregroundNotificationId, getForegroundNotification());
}
return START_STICKY;
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}

关于android - 粘性前台服务不粘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56198875/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com