【问题标题】:Why does my Intent open App Info in Settings for my app, rather than the Activity class I pass to it?为什么我的 Intent 在我的应用程序的设置中打开应用程序信息,而不是我传递给它的 Activity 类?
【发布时间】:2019-04-11 14:09:46
【问题描述】:

我有一个使用通知启动前台服务的活动。如果相关,该服务也可以设置为在启动时启动,即。我希望它也能够在没有 Activity 的情况下启动。

我希望它是这样的,如果我点击通知,然后我会转到我的活动,在那里我可以停止服务、更改设置等。

我将带有 Activity 类的 Intent 提供给 PendingIntent 用于前台服务的通知。

如果我点击通知,它会在我的应用的系统设置中打开应用信息(您可以在其中强制停止、卸载等),而不是 Activity 本身。我怎样才能让它做后者?

这是在服务启动时创建通知的代码:

@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 风格。谢谢。

【问题讨论】:

  • 毫无意义。你的代码看起来不错。上下文很好,在这里几乎无关紧要。很奇怪。您可以查看 logcat(不要过滤它)并查看当您点击通知时是否记录了任何奇怪的内容?
  • 添加了 Logcat。我已经删除了我认为与此应用程序无关的内容(并且以 31157 开头的某些位可能仍然不相关)。我将尝试向我的 Intent 添加类别和操作。 Context 的想法来自this question(它似乎根本没有反应)在为 Activity 的 PendingIntent 使用前台服务上下文时。

标签: android android-intent android-pendingintent android-8.1-oreo


【解决方案1】:

如果我设置了一个我还没有制作的图标,由于艺术缺陷,这将有效。 Unlike this question这种情况不需要先设置图标。

就好像它忽略了传递给 startForeground 方法的通知并提供一个默认通知,除非它包含一个图标。

一旦我通过 setSmallIcon(R.drawable...) 添加它,一切都按预期工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-29
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 2014-05-16
    • 2019-12-24
    相关资源
    最近更新 更多