1

我使用startForefround()方法顯示背景爲ServiceNotificationAndroid用戶點擊我的通知並返回到我的應用程序

if (notif == null) { 

     // Create the pending intent 
     Intent intentForeground = new Intent(this, BackgroundLocationService.class) 
     .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);  
     PendingIntent pendIntent = PendingIntent.getActivity(getApplicationContext(), 0, intentForeground, 0); 

     notif = new NotificationCompat.Builder(getApplicationContext()) 
     .setSmallIcon(R.drawable.ic_launcher) 
     .setDefaults(Notification.DEFAULT_ALL) 
     .setTicker(getText(R.string.location_service_starting)) 
     .setOnlyAlertOnce(true) 
     .setOngoing(true) 
     .setContentIntent(pendIntent) 
     .build(); 
     notif.flags |= Notification.FLAG_NO_CLEAR; 
    } 
    startForeground(notificationID, notif); 

它正確地顯示通知用戶,當我滑下通知欄我想有用戶點擊此通知並返回到我的應用程序。我怎樣才能做到這一點?

回答

1

使用此

Notification notification = new Notification(R.drawable.logo2, "app is running on the background!", System.currentTimeMillis()); 

notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; 

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 

notification.setLatestEventInfo(this, "title", "message!", contentIntent); 

this.startForeground(1023, notification); 
+0

啊,我是缺少MainActivity.class部分。謝謝! –

相關問題