android studio启动了一个前台服务,但是在手机上查看正在运行的服务只显示1个进程和0个服务,我在服务中开启了线程循环打印能够打印,但是我不知道为什么会显示0个服务,正常难道不是应该显示1个进程和1个服务吗,这是为什么
下边是代码
Intent intent = new Intent(this, ForegroundSrv.class);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
this.startForegroundService(intent);
}else {
this.startService(intent);
}
package com.example.testc;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import java.util.ArrayList;
import java.util.List;
public class ForegroundSrv extends Service{
private final String TAG = "ForegroundSrv";
int count = 0;
private List threads = new ArrayList<>();
NotificationCompat.Builder builder;
NotificationManager manager;
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG,"onCreate");
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String CHANNEL_ID = "my_channel_01";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ //Android 8.0适配
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);//如果这里用IMPORTANCE_NOENE就需要在系统的设置里面开启渠道, //通知才能正常弹出
manager.createNotificationChannel(channel);
}
builder = new NotificationCompat.Builder(this,String.valueOf(CHANNEL_ID));
builder.setContentTitle("前台服务") //指定通知栏的标题内容
.setContentText("前台服务正在运行") //通知的正文内容
.setWhen(System.currentTimeMillis()) //通知创建的时间
.setSmallIcon(R.drawable.ic_launcher_background); //通知显示的小图标,只能用alpha图层的图片进行设置
Notification notification = builder.build() ;
startForeground(2, notification);
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
builder.setContentText("前台服务正在运行 count="+count);
manager.notify(2,builder.build());
}
};
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//flags = START_STICKY;
Log.i(TAG,"onStartCommand,flags="+flags+" ,startId="+startId);
Thread thread = new Thread(){
@Override
public void run() {
super.run();
while (true){
Log.i(TAG,getName()+"count="+count);
try {
sleep(2000);
} catch (InterruptedException e) {
return;
}
setCount();
mHandler.sendEmptyMessage(0);
}
}
};
thread.start();
threads.add(thread);
return super.onStartCommand(intent, flags, startId);
}
private synchronized void setCount(){
count++;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Log.i(TAG,"onTaskRemoved");
}