问题描述
我尝试了使用前台服务在后台运行服务的所有方法,并且该设备可在除中国设备(如vivo,oppo和oneplus)之外的所有设备上运行。一旦这些应用从最近的列表中被杀死,这些设备就会终止该服务。
即使前台通知也不粘滞。它是可滑动的。但是,我想在清除通知后立即再次显示通知,有什么办法吗?
例如,当我下载任何文件时,在IDM(互联网下载管理器)之类的应用中看到的通知仍然可见。即使清除,通知也会再次弹出。
如何获得这种功能?
我的代码:
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startService(View v) {
Intent serviceIntent = new Intent(this,ExampleService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(serviceIntent);
} else startService(serviceIntent);
}
public void stopService(View v) {
Intent serviceIntent = new Intent(this,ExampleService.class);
stopService(serviceIntent);
}
}
ExampleService.java
final Handler worker = new Handler();
@Override
public void onCreate() {
super.onCreate();
Log.d("Foreground Service","*********** Service Created ***********");
}
@Override
public void onDestroy() {
super.onDestroy();
worker.removeCallbacksAndMessages(null);
Log.d("Foreground Service","*********** Service Destroyed ***********");
}
@Override
public int onStartCommand(final Intent intent,int flags,int startId) {
Intent notificationIntent = new Intent(this,MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
final NotificationCompat.Builder notification = new NotificationCompat.Builder(this,App.NOTIFICATION_CHANNEL_ID)
.setContentTitle("Example Service")
.setContentText("Working in background")
.setSmallIcon(android.R.drawable.sym_def_app_icon)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setAutoCancel(false)
.setongoing(true)
.setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notification.setCategory(Notification.CATEGORY_PROGRESS);
}
startForeground(1,notification.build());
worker.post(new Runnable() {
@Override
public void run() {
Log.i("Info",Thread.currentThread().getName());
Toast toast = Toast.makeText(getApplicationContext(),"Service Ping",Toast.LENGTH_SHORT);
toast.show();
Log.d("Foreground Service","*********** Service working ***********");
worker.postDelayed(this,5000);
}
});
return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)