Android 10应用正常但Service不显示的问题排查求助
Android 10中Service无法在设置“应用和运行中”页面显示的问题
我的应用在Android 10系统上可正常运行,但在设置的应用和运行中页面里,仅显示1个进程、0个服务,Service未被系统识别显示。已在Service的onCreate()中调用super.onCreate(),且实现了前台通知逻辑,但问题仍未解决,无法定位原因。相关代码如下:
Manifest文件
<service android:name=".Service.AlarmService" android:label="Alarm" android:enabled="true" android:foregroundServiceType="mediaPlayback" android:icon="@drawable/ic_send"/> <uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/> <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/> <uses-permission android:name="android.permission.WAKE_LOCK" />
Service代码
public class AlarmService extends Service { private MediaPlayer player; IBinder iBinder; @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); } @Override public void onCreate() { super.onCreate(); player = MediaPlayer.create(this, R.raw.ringtone); if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) startMyOwnForeground(); else startForeground(1, new Notification()); } @Override public int onStartCommand(Intent intent, int flags, int startId) { player.start(); // bettrylife(this); // stopSelf(); return START_NOT_STICKY; } @Override public void onDestroy() { super.onDestroy(); } @Nullable @Override public IBinder onBind(Intent intent) { return iBinder; } private void bettrylife(Context context){ PackageManager manager= context.getPackageManager(); ComponentName componentName = new ComponentName(context, AlarmService.class); int settingcode =PackageManager.COMPONENT_ENABLED_STATE_ENABLED; manager.setComponentEnabledSetting(componentName,settingcode, DONT_KILL_APP); } @RequiresApi(Build.VERSION_CODES.O) private void startMyOwnForeground() { String NOTIFICATION_CHANNEL_ID = "example.permanence"; String channelName = "Background Service"; NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE); chan.setLightColor(Color.BLUE); chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); assert manager != null; manager.createNotificationChannel(chan); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID); Notification notification = notificationBuilder.setOngoing(true) .setContentTitle("App is running in background") .setPriority(NotificationManager.IMPORTANCE_MIN) .setCategory(Notification.CATEGORY_SERVICE) .build(); startForeground(2, notification); } }
Fragment代码
Intent intent =new Intent(getActivity(),AlarmService.class); startService(intent);
问题排查与解决方案
1. 适配前台服务启动方式
Android 8.0(API 26)开始,后台启动Service必须使用startForegroundService(),而非startService()。Android 10对这一限制执行更严格,直接用startService()启动前台服务可能导致系统无法正确标记服务状态。修改Fragment中的启动代码:
Intent intent = new Intent(getActivity(), AlarmService.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { getActivity().startForegroundService(intent); } else { getActivity().startService(intent); }
2. 修正前台通知的合法性
- Android O以下不能使用空
Notification,必须包含图标、标题等基础元素,否则系统可能不认可前台服务状态:
@Override public void onCreate() { super.onCreate(); player = MediaPlayer.create(this, R.raw.ringtone); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startMyOwnForeground(); } else { // 构建合法的基础通知 NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_send) // 必须设置图标 .setContentTitle("闹钟服务运行中") .setContentText("正在播放闹钟铃声") .setPriority(NotificationCompat.PRIORITY_LOW); startForeground(1, builder.build()); } }
- Android O及以上的通知渠道不能用
IMPORTANCE_NONE,改为IMPORTANCE_LOW确保系统能识别前台服务,同时匹配闹钟场景修改通知类别:
@RequiresApi(Build.VERSION_CODES.O) private void startMyOwnForeground() { String NOTIFICATION_CHANNEL_ID = "example.alarm_service"; String channelName = "闹钟前台服务"; // 修改渠道重要性为IMPORTANCE_LOW NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_LOW); chan.setLightColor(Color.BLUE); chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); assert manager != null; manager.createNotificationChannel(chan); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID); Notification notification = notificationBuilder.setOngoing(true) .setSmallIcon(R.drawable.ic_send) // 必须设置图标 .setContentTitle("闹钟服务运行中") .setContentText("正在播放闹钟铃声") .setPriority(NotificationManager.IMPORTANCE_LOW) .setCategory(Notification.CATEGORY_ALARM) // 改为闹钟类别更贴合场景 .build(); startForeground(2, notification); }
3. 检查Service声明与生命周期
- 确认Manifest中Service的路径
.Service.AlarmService与实际包结构完全匹配,包名错误会导致系统无法识别Service。 - 在Service的
onCreate()、onStartCommand()、onDestroy()方法中添加日志,确认Service是否正常启动、是否被意外销毁:
@Override public void onCreate() { super.onCreate(); Log.d("AlarmService", "Service onCreate"); // ... 原有代码 } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("AlarmService", "Service onStartCommand"); // ... 原有代码 } @Override public void onDestroy() { Log.d("AlarmService", "Service onDestroy"); super.onDestroy(); }
4. 匹配前台服务类型
你的Service是闹钟用途,Android 10虽未引入foregroundServiceType="alarm",但mediaPlayback是合法类型,若后续适配更高版本,建议改为对应类型以符合系统规范。
内容的提问来源于stack exchange,提问作者Ibrahem Hamdy
相关产品推荐
相关产品推荐

