用于前台服务的Android PendingEvent
我仍然对在前台启动服务感到完全困惑 目前:我有一项活动,在后台启动一项服务,对传感器数据+gps数据执行统计。它不断被杀死,所以我从前景开始。很好。 但是,当我触摸状态栏中的通知图标时,它似乎开始了一个新的活动(我在挂起的意图中确实说明了这一点) 所以简而言之,我仍然对整件事感到困惑。 我希望某个活动在前台启动服务,当您单击通知栏时,它会将启动服务的现有活动带到前台 以下是活动中的一些代码(当前将PendingEvent设置为null): 这样做:用于前台服务的Android PendingEvent,android,service,foreground,Android,Service,Foreground,我仍然对在前台启动服务感到完全困惑 目前:我有一项活动,在后台启动一项服务,对传感器数据+gps数据执行统计。它不断被杀死,所以我从前景开始。很好。 但是,当我触摸状态栏中的通知图标时,它似乎开始了一个新的活动(我在挂起的意图中确实说明了这一点) 所以简而言之,我仍然对整件事感到困惑。 我希望某个活动在前台启动服务,当您单击通知栏时,它会将启动服务的现有活动带到前台 以下是活动中的一些代码(当前将PendingEvent设置为null): 这样做: Intent notificationInte
Intent notificationIntent = new Intent(context, YourActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notification.setLatestEventInfo(context, "title", null, contentIntent);
如果您希望活动继续,而不是重新启动,请使用FLAG_ACTIVITY_SINGLE_TOP
Intent notificationIntent = new Intent(context, YourActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notification.setLatestEventInfo(context, "title", null, contentIntent);
Intent notificationIntent = new Intent(context, YourActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notification.setLatestEventInfo(context, "title", null, contentIntent);