gyf-dev / Cactus

Android Keep Alive(安卓保活),Cactus 集成双进程前台服务,JobScheduler,onePix(一像素),WorkManager,无声音乐
Apache License 2.0
1.54k stars 229 forks source link

Not allowed to delete channel with a foreground service #58

Open kaiwmtnk opened 3 years ago

kaiwmtnk commented 3 years ago

报错Not allowed to delete channel $app name$ with a foreground service

com.gyf.cactus.ext.NotificationExtKt$setNotification$$inlined$apply$lambda$1.run(NotificationExt.kt:71)

机型SM-G9750 版本Android 11,level 30

nukix commented 3 years ago

一样, 2021年6月1日更新系统的 Android 11 都出现这种情况。

BaiPeiHe commented 2 years ago

这样就可以了,不会报错了 Cactus.getInstance().hideNotification(false)

nukix commented 2 years ago

@BaiPeiHe 这样会显示常驻通知栏的, 很多用户对于常驻通知栏很反感

github-gsjb commented 2 years ago

@BaiPeiHe 这样会显示常驻通知栏的, 很多用户对于常驻通知栏很反感

该怎么解决?

cjecky commented 2 years ago

一起讨论下

adustdu2015 commented 1 year ago

Android10以后,启动前台服务,必须显示通知的处理方案

我们都知道,目前启动服务在Android O 之后,必须使用startForeground 。并且会显示一个通知栏的东西。

//调用
Intent intent = new Intent(MainActivity.this, MyService.class);
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    startForegroundService(intent);
  } else {
    startService(intent);
  }

我们完全可以用bindService(intent,conn,flags)替代。

bindService用于绑定一个服务。这样当bindService(intent,conn,flags)后,就会绑定一个服务。这样做可以获得这个服务对象本身;

废话不多说,直接上代码:

 /**
 *1:调用者
 */
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;

/**
 * 此例的目的就是拿到MyService的引用,从而可以引用其内部的方法和变量
 * 
 * @author Administrator
 * 
 */
public class MainActivity extends AppCompatActivity {
    //这里创建一个静态对象,就可以在其他类中进行调用啦。
    public static MyService myService;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent intent = new Intent(this, MyService.class);
        bindService(intent, connection, Context.BIND_AUTO_CREATE);
    }

    private ServiceConnection connection = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            myService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myService = ((MyService.MyBinder) service).getService();
            System.out.println("Service连接成功");
            // 执行Service内部自己的方法
            myService.excute();
        }
    };

    protected void onDestroy() {
        super.onDestroy();
        unbindService(connection);
    };
}

 /*
 *2:服务者
 */
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class MyService extends Service {
    private final IBinder binder = new MyBinder();

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

    public class MyBinder extends Binder {
        public MyService getService() {
            return MyService.this;
        }
    }

    public void excute() {
        System.out.println("通过Binder得到Service的引用来调用Service内部的方法");
    }

    @Override
    public void onDestroy() {
        // 当调用者退出(即使没有调用unbindService)或者主动停止服务时会调用
        super.onDestroy();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        // 当调用者退出(即使没有调用unbindService)或者主动停止服务时会调用
        System.out.println("调用者退出了");
        return super.onUnbind(intent);
    }
}

3.AndroidManifest.xml添加服务
 <application ...>
    <service android:name=".MyService" />
 </application>

完美解决了。