Comments (6)
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>
完美解决了。
from cactus.
一样, 2021年6月1日更新系统的 Android 11 都出现这种情况。
from cactus.
这样就可以了,不会报错了 Cactus.getInstance().hideNotification(false)
from cactus.
@BaiPeiHe 这样会显示常驻通知栏的, 很多用户对于常驻通知栏很反感
from cactus.
@BaiPeiHe 这样会显示常驻通知栏的, 很多用户对于常驻通知栏很反感
该怎么解决?
from cactus.
一起讨论下
from cactus.
Related Issues (20)
- android 10 能搞吗?
- ANR
- 小米手机无法做到保活
- 有些手机通知栏需要权限,影不影响这个库? HOT 1
- Caused by: java.lang.RuntimeException HOT 1
- 无声音乐播放问题 HOT 2
- 服务问题 HOT 9
- CacutsException
- 如果我app主动杀掉进程 可以监听到嘛 就是说 服务断掉??
- bug: vivo 8.1版本手机上会出现unregisterReceiver()的问题
- android.app.RemoteServiceException: Bad notification for startForeground HOT 1
- android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
- 大佬有方法解决一下这个carsh吗?8.1.0奔溃了100% HOT 1
- 后台能杀死 HOT 1
- 魔法保活失效
- App开启自启动后,前台服务会自启
- 导致ANR有人帮忙看一下吗
- 通知栏问题
- implementation 'com.gyf.cactus:cactus:1.1.3-beta13' 失败,有漏洞下载失败?? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cactus.