我仍然在前台开始服务时感到十分困惑。Android PendingIntent for foreGround服务
当前:我有一个活动,在后台启动一个服务来执行传感器数据+ GPS数据的统计。它不断被杀死,所以我开始在前台。工作正常。 但是,当我触摸状态栏中的通知图标时,它似乎开始一个新的活动(我在待定意图中声明)。
所以简而言之,我仍然对整个事情感到困惑。 我想要一个活动在前台启动一项服务,并且当您单击通知栏时,它会将启动服务的现有活动带到前台。
这里的活动(当前设置的PendingIntent为null)中的一些代码:
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with the service has been
// established, giving us the service object we can use to
// interact with the service. Because we have bound to a explicit
// service that we know is running in our own process, we can
// cast its IBinder to a concrete class and directly access it.
cycleDataService = ((CycleDataProService.LocalBinder) service)
.getService();
//Todo the notification bullshaite here...
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.icon;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
//Intent notificationIntent = new Intent(cycleDataService, CycleDataProService.class);
//PendingIntent contentIntent = PendingIntent.getActivity(context, 0, myOwnIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pi = PendingIntent.getActivity(context, 0, null, 0);
//PendingIntent contentIntent = PendingIntent.getActivity(context, 0, , 0);
notification.setLatestEventInfo(context, contentTitle, contentText, pi);
mNotificationManager.notify(HELLO_ID, notification);
cycleDataService.startForeground(HELLO_ID, notification);
/*cycleDataService.startService(new Intent(cycleDataService,
CycleDataProService.class));*/
serviceIsRunning = true;
// Tell the user about this for our demo.
// Toast.makeText(Binding.this, R.string.local_service_connected,
// Toast.LENGTH_SHORT).show();
}