我有一个活动,该活动通过通知启动前台服务。如果相关,也可以将服务设置为在引导时启动,即。我希望它也可以在没有活动的情况下启动。
我希望它是这样,如果我点击通知,然后转到我的“活动”,可以停止服务,更改设置等。
我将带有Activity类的Intent提供给PendingIntent,以用于前台服务的通知。
如果我点击通知,它将在我的应用的系统设置中打开应用信息(您可以在其中强制停止,卸载等),而不是活动本身。我该怎么做?
这是在服务启动时创建通知的代码:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Context con = getApplicationContext();
String chanID = "com.blah.MyApp";
NotificationChannel chan = new NotificationChannel(chanID, getString(R.string.app_name), NotificationManager.IMPORTANCE_NONE);
NotificationManager manager = (NotificationManager) con.getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(chan);
PendingIntent penitent = PendingIntent.getActivity(con, 1, new Intent(getBaseContext(), MyApp.class), 0);
Notification notif = new NotificationCompat.Builder(con, chanID).setContentIntent(penitent).build();
startForeground(1, notif);
...
我也尝试过使用
创建意图 Intent.makeMainActivity(new ComponentName("com.blah", "MyApp"));
具有相同的结果。我想知道问题是否与上下文有关?
编辑:我也无法更改通知中的文本。
Logcat:
2019-04-12 09:50:56.062 1395-6289/? I/ActivityManager: START u0 {act=android.settings.APPLICATION_DETAILS_SETTINGS dat=package:com.blah cmp=com.android.settings/.applications.InstalledAppDetails} from uid 1000
2019-04-12 09:50:56.067 1395-6289/? W/ActivityManager: startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { act=android.settings.APPLICATION_DETAILS_SETTINGS dat=package:com.blah cmp=com.android.settings/.applications.InstalledAppDetails }
2019-04-12 09:50:56.072 30945-30957/com.blah I/zygote64: Do partial code cache collection, code=61KB, data=57KB
2019-04-12 09:50:56.076 30945-30957/com.blah I/zygote64: After code cache collection, code=61KB, data=57KB
2019-04-12 09:50:56.079 30945-30957/com.blah I/zygote64: Increasing code cache capacity to 256KB
2019-04-12 09:50:56.102 1395-25718/? E/ActivityManager: applyOptionsLocked: Unknown animationType=0
2019-04-12 09:50:56.142 31157-31157/? I/zygote64: Deoptimizing void android.widget.LinearLayout.<init>(android.content.Context, android.util.AttributeSet, int, int) due to JIT inline cache
2019-04-12 09:50:56.204 31157-31157/? I/zygote64: Deoptimizing void android.widget.RelativeLayout$LayoutParams.<init>(android.content.Context, android.util.AttributeSet) due to JIT inline cache
2019-04-12 09:50:56.285 31157-31157/? W/Settings: Unable to find info for package: null
2019-04-12 09:50:56.339 31157-31189/? W/TileUtils: Found com.android.settings.backup.BackupSettingsActivity for intent Intent { act=com.android.settings.action.SETTINGS pkg=com.android.settings } missing metadata com.android.settings.category
2019-04-12 09:50:56.350 31157-31189/? W/TileUtils: Found org.lineageos.lineageparts.trust.TrustPreferences for intent Intent { act=org.lineageos.lineageparts.action.SETTINGS pkg=org.lineageos.lineageparts } missing metadata
2019-04-12 09:50:56.362 31157-31157/? W/InstalledAppDetails: App is not explicitly stopped
2019-04-12 09:50:56.367 31157-31157/? W/ContextImpl: Calling a method in the system process without a qualified user: android.app.ContextImpl.bindService:1556 android.content.ContextWrapper.bindService:684 android.content.pm.permission.RuntimePermissionPresenter$RemoteService.processMessage:171 android.content.pm.permission.RuntimePermissionPresenter.getAppPermissions:118 com.android.settingslib.applications.PermissionsSummaryHelper.getPermissionSummary:34
2019-04-12 09:50:56.372 31157-31157/? D/AppUtils: Have 0 number of activities in preferred list
2019-04-12 09:50:56.397 1395-1431/? I/WifiService: requestActivityInfo uid=1000
2019-04-12 09:50:56.397 1395-1431/? I/WifiService: reportActivityInfo uid=1000
2019-04-12 09:50:56.397 1395-1431/? I/WifiService: getSupportedFeatures uid=1000
2019-04-12 09:50:56.410 1395-1431/? E/BatteryExternalStatsWorker: no controller energy info supplied for wifi
2019-04-12 09:50:56.410 1395-1431/? E/BatteryExternalStatsWorker: no controller energy info supplied for bluetooth
2019-04-12 09:50:56.500 31157-31189/? D/Settings: No enabled state changed, skipping updateCategory call
2019-04-12 09:50:56.528 31157-31157/? W/InstalledAppDetails: App is not explicitly stopped
2019-04-12 09:50:56.537 31157-31157/? D/AppUtils: Have 0 number of activities in preferred list
2019-04-12 09:50:56.555 31157-31157/? W/InstalledAppDetails: App is not explicitly stopped
2019-04-12 09:50:56.566 31157-31157/? D/AppUtils: Have 0 number of activities in preferred list
2019-04-12 09:50:56.582 31157-31189/? W/ProcStatsManager: No process com.google.android.configupdater/10017 for service com.google.android.configupdater.MainJobService
2019-04-12 09:50:56.582 31157-31189/? W/ProcStatsManager: No process com.android.defcontainer/10026 for service com.android.defcontainer.DefaultContainerService
2019-04-12 09:50:56.582 31157-31189/? W/ProcStatsManager: No process com.android.vending:instant_app_installer/10029 for service com.google.android.finsky.instantapps.InstantAppHygieneService
2019-04-12 09:50:56.582 31157-31189/? W/ProcStatsManager: No process com.android.vending:instant_app_installer/10029 for service com.google.android.finsky.instantapps.PhenotypeUpdateService
2019-04-12 09:50:56.583 31157-31189/? W/ProcStatsManager: No process com.android.vending:instant_app_installer/10029 for service com.google.android.finsky.instantapps.metrics.LogFlushJob
2019-04-12 09:50:56.583 31157-31189/? W/ProcStatsManager: No process com.firebase.jobdispatcher.GooglePlayReceiver
2019-04-12 09:50:56.583 31157-31189/? W/ProcStatsManager: No process com.android.cellbroadcastreceiver/10015 for service com.android.cellbroadcastreceiver.CellBroadcastConfigService
2019-04-12 09:50:56.605 31157-31189/? W/ProcStatsManager: No process com.google.android.gms.ui/10010 for service com.google.android.gms.chimera.UiIntentOperationService
2019-04-12 09:50:56.605 31157-31189/? W/ProcStatsManager: No process com.google.android.gms.learning/10010 for service com.google.android.gms.learning.training.background.TrainingGcmTaskService
2019-04-12 09:50:56.605 31157-31189/? W/ProcStatsManager: No process com.google.android.partnersetup/10016 for service com.google.android.partnersetup.InstalledAppJobService
2019-04-12 09:50:56.605 31157-31189/? W/ProcStatsManager: No process com.google.android.apps.translate/10102 for service com.google.android.libraries.internal.growth.growthkit.internal.jobs.impl.GrowthKitJobService
2019-04-12 09:50:56.698 1395-1435/? I/ActivityManager: Displayed com.android.settings/.applications.InstalledAppDetails: +565ms
我还应该提到这是LineageOS Android风格。谢谢。
答案 0 :(得分:0)
如果由于艺术缺陷我设置了一个尚未制作的图标,此方法将起作用。 Unlike this question在这种情况下,无需先设置图标。
就好像它忽略传递给startForeground方法的通知,并提供一个默认通知,除非它包含一个图标。
一旦我补充说,通过setSmallIcon(R.drawable ...)都能按预期工作。