Cactus icon indicating copy to clipboard operation
Cactus copied to clipboard

Not allowed to delete channel with a foreground service

Open kaiwmtnk opened this issue 3 years ago • 6 comments

报错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

kaiwmtnk avatar Jul 16 '21 08:07 kaiwmtnk

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

nukix avatar Jul 17 '21 08:07 nukix

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

BaiPeiHe avatar Oct 11 '21 07:10 BaiPeiHe

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

nukix avatar Oct 11 '21 07:10 nukix

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

该怎么解决?

github-gsjb avatar Oct 28 '21 06:10 github-gsjb

一起讨论下

cjecky avatar Dec 08 '21 00:12 cjecky

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>

完美解决了。

adustdu2015 avatar May 19 '23 03:05 adustdu2015