【发布时间】:2016-10-17 20:03:36
【问题描述】:
我已经创建了一个服务,并且我还创建了一个通知,所以当我的服务运行时会有一个通知。 现在,我希望用户能够滑动/关闭通知,但尝试这样做时遇到了两个问题:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
password = intent.getStringExtra("password");
number = intent.getStringExtra("number");
Intent activityIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this).
setContentTitle(getText(R.string.app_name)).
setContentText("Subject").
setContentInfo("Doing stuff in the background...").
setSmallIcon(R.drawable.ic).
setAutoCancel(true).
setContentIntent(pendingIntent).build();
startForeground(1, notification);
return super.onStartCommand(intent, flags, startId);
}
这段代码运行得很好,唯一的问题是用户不能滑动关闭通知,所以通过搜索我发现我可以通过用'startForeground'函数替换它来修复它
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0,notification);
然后它起作用了,我可以滑动关闭,但现在我遇到了一个不同的问题,一旦我关闭我的应用程序(使用长按中间按钮然后关闭所有应用程序) 几分钟后,我的应用程序抛出了一个指向该行的空指针异常:
password = intent.getStringExtra("password");
好像意图是空的。当我使用 startForeground 函数时不会发生这种情况
可能是什么问题?
【问题讨论】:
标签: android android-studio service notifications