-
Notifications
You must be signed in to change notification settings - Fork 234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not allowed to delete channel with a foreground service #58
Comments
一样, 2021年6月1日更新系统的 Android 11 都出现这种情况。 |
这样就可以了,不会报错了 Cactus.getInstance().hideNotification(false) |
@BaiPeiHe 这样会显示常驻通知栏的, 很多用户对于常驻通知栏很反感 |
该怎么解决? |
一起讨论下 |
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> 完美解决了。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
报错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
The text was updated successfully, but these errors were encountered: