粘性前台服务不是粘性的

时间:2019-05-18 12:19:37

标签: android

我的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

2 个答案:

答案 0 :(得分:1)

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

从Andorid Q开始,您必须添加权限:

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

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

科特琳:

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;
    }
}

答案 1 :(得分:0)

var randomAssets: Set<Int> = Set(0...18) @IBAction func Button(_ sender: Any) { guard let random = randomAssets.randomElement() else { return } randomAssets.remove(random) Smallbug.image = UIImage(named: "Bug\(random)") } 与通知无关。

START_STICKY添加到您的setOngoing(true)