Android无启动Activity启动Service触发通知失败问题排查
问题描述
我要开发一款无启动Activity的Android应用,通过Service触发一条简单通知,但功能无法正常运行。我尝试通过终端启动Service,终端显示已启动但通知并未弹出,请问该如何解决?
Service代码
public class NetworkService extends android.app.Service { private NotificationManager notificationManager; @Override public void onCreate() { super.onCreate(); showNetworkChangeNotification(); } @Override public IBinder onBind(Intent intent) { return null; } private void showNetworkChangeNotification() { notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel("network_change_channel", "Network Change Channel", NotificationManager.IMPORTANCE_DEFAULT); notificationManager.createNotificationChannel(channel); } RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.custom_ui_layout); remoteViews.setTextViewText(R.id.text,"This is a testing text"); Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "network_change_channel") .setContentTitle("Network ") .setContentText("Sample") .setSmallIcon(R.drawable.baseline_circle_notifications_24) .setContentIntent(pendingIntent) .setContent(remoteViews) .addAction(R.drawable.baseline_circle_notifications_24, "Reset", getCancelPendingIntent()) .setAutoCancel(true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForeground(1, notificationBuilder.build()); } else { notificationManager.notify(1, notificationBuilder.build()); } } private PendingIntent getCancelPendingIntent() { Intent intent = new Intent(this, NetworkChangeReceiver.class); intent.setAction("com.z.widgets.networkChangeReceiver.actionCancelNotification"); return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_IMMUTABLE); } @Override public void onDestroy() { super.onDestroy(); notificationManager.cancel(1); stopSelf(); } }
Receiver代码
public class NetworkChangeReceiver extends BroadcastReceiver { private TelephonyManager telephonyManager; private NotificationManager notificationManager; @SuppressLint("MissingPermission") @Override public void onReceive(Context context, Intent intent) { Intent serviceIntent = new Intent(context, NetworkService.class); if (intent != null && "com.z.widgets.networkChangeReceiver.actionCancelNotification".equals(intent.getAction())) { Toast.makeText(context,"Reset Clicked",Toast.LENGTH_LONG).show(); } } }
Manifest配置文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> <application android:name=".BaseApplication" android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.WidgetJava" tools:targetApi="31"> <service android:name=".NetworkService" android:exported="true" android:enabled="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <action android:name="com.example.action.START_FOREGROUND_SERVICE" /> </intent-filter> </service> <receiver android:name=".NetworkChangeReceiver" android:exported="true" android:enabled="true"> <intent-filter> <action android:name="com.z.widgets.networkChangeReceiver.actionCancelNotification"/> </intent-filter> </receiver> </application> </manifest>
终端启动截图

解决方案
1. 排查自定义通知布局问题
直接注释掉notificationBuilder.setContent(remoteViews)代码,用系统默认布局测试。如果通知能正常显示,再检查自定义布局:
- 确认
custom_ui_layout.xml中存在R.id.text控件 - 自定义布局根节点用
LinearLayout或RelativeLayout,尺寸设为wrap_content避免超出系统限制
2. 修复无效的PendingIntent
你设置的通知跳转意图指向MainActivity,但应用无启动Activity,直接替换为空白Intent:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(), PendingIntent.FLAG_IMMUTABLE);
3. 修正Service的IntentFilter配置
删除Service的android.intent.action.MAIN和android.intent.category.LAUNCHER配置,保留自定义action即可:
<service android:name=".NetworkService" android:exported="true" android:enabled="true"> <intent-filter> <action android:name="com.example.action.START_FOREGROUND_SERVICE" /> </intent-filter> </service>
4. 补全Foreground Service权限
Android 8.0+启动前台Service需要添加权限,在Manifest中补充:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
5. 确保通知权限生效(Android 13+)
手动前往应用设置开启通知权限,或者在应用启动逻辑中动态申请POST_NOTIFICATIONS权限(无Activity的话可引导用户去设置页开启)。
6. 检查终端启动命令
确保启动Service的命令包名和action正确:
am startservice -n 你的应用包名/.NetworkService -a com.example.action.START_FOREGROUND_SERVICE
内容的提问来源于stack exchange,提问作者Taki
相关产品推荐
相关产品推荐

