2014-07-24 39 views
0

我已經在通知欄上成功創建了一個項目,其中DownloadService擴展了IntentService類。如何點擊通知欄返回到應用程序?

public class MyService extends Activity 
    { 
     ... 
     void startService() 
     { 
      ... 
      Intent myIntent = new Intent(StoryGallery.this,DownloadService.class); 
      myIntent.putExtra("story name", story.title); 
      startService(myIntent); 
     } 
    } 

問題是如何點擊通知項目以返回到我的應用程序?

似乎有很多這樣的問題,但我找不到一個簡單的答案,通過單擊通知項目來啓動應用程序。我試圖讓「DownloadService實現OnClickListener」,但點擊後沒有任何迴應。

所以,在此先感謝您的答覆。

回答

1

A Service不應該實現onClickListener,因爲它沒有UI。它只是在後臺運行。所以,只要你想顯示通知使用:

NotificationCompat.Builder builder = new NotificationCompat.Builder(
      context); 

    /** IMPORTANT : Create an intent which starts the activity **/ 
    Intent intent = new Intent(context, ActivityName.class); 

    /** IMPORTANT : Ensure that you use getActivity with the PendingIntent. **/ 
    /**This pending intent will be fired when you click on the notification.**/ 
    builder.setContentIntent(PendingIntent.getActivity(context, 0, intent, 
      0)); 
    builder.setContentTitle(title); 
    builder.setContentText(message); 
    builder.setSmallIcon(R.drawable.notification_icon); 
    Notification notification = builder.build(); 
    NotificationManager manager = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    manager.notify(0, notification); 
+0

什麼是從通知欄到我的應用程序的關鍵一步? – user2718067

+0

檢查更新的答案。我標出了重要的部分。 –

+0

非常感謝您的評論。那麼試試吧。 – user2718067

相關問題