当我尝试设置服务前景时,Android重新启动
-
25-10-2019 - |
题
我在应用程序中遇到了一种奇怪的行为:当我尝试设置服务前景时,手机(HTC G1 + Cyanogen mod)重新启动。
当我不尝试推荐这项服务时,它起作用。
这是罪名的代码:
Notification notification = new Notification(R.drawable.icon,
getText(R.string.ticker_text), System.currentTimeMillis());
startForeground(SERVICE_NOTIFICATION_ID, notification);
Log.v(TAG, "Control service foregrounded.");
你能看到问题在哪里吗?
如果您需要更多数据,则可以在GitHub上找到整个项目: https://github.com/rbochet/serval-video-discovery/tree/network-Remote
谢谢。
解决方案
看看这个Android API演示 这里. 。请注意,而不是打电话 startForeground()
, ,它打电话 startForegroundCompat()
, ,根据新的/旧的启动前线API来处理您的请求的包装器。
void handleCommand(Intent intent) {
if (ACTION_FOREGROUND.equals(intent.getAction())) {
...
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.stat_sample, text,
System.currentTimeMillis());
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, Controller.class), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.local_service_label),
text, contentIntent);
startForegroundCompat(R.string.foreground_service_started, notification);
...
}
这是 startForegroundCompat()
:
/**
* This is a wrapper around the new startForeground method, using the older
* APIs if it is not available.
*/
void startForegroundCompat(int id, Notification notification) {
// If we have the new startForeground API, then use it.
if (mStartForeground != null) {
mStartForegroundArgs[0] = Integer.valueOf(id);
mStartForegroundArgs[1] = notification;
invokeMethod(mStartForeground, mStartForegroundArgs);
return;
}
// Fall back on the old API.
mSetForegroundArgs[0] = Boolean.TRUE;
invokeMethod(mSetForeground, mSetForegroundArgs);
mNM.notify(id, notification);
}
不隶属于 StackOverflow