2011-08-25 5 views
12

サービスを経由AlarmManager開始されたとき、私は通知を起動するには、次のコードを使用しています:通知のためにクリックリスナーを設定するにはどうすればよいですか?

ユーザーがそれをクリックしたとき、それは一定の起動なるように、私はこの項目の意図を設定するにはどうすればよい
nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); 
CharSequence from = "App"; 
CharSequence message = "Getting Latest Info..."; 
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 0); 
Notification notif = new Notification(R.drawable.icon, 
    "Getting Latest Info...", System.currentTimeMillis()); 
notif.setLatestEventInfo(this, from, message, contentIntent); 
nm.notify(1, notif); 

アクティビティ?

答えて

12

基本的に、あなたの意図の一部としてActivityクラスをあなたのPendingIntentに入れる必要があります。現在、あなたの意図は空です。新しい活動にリダイレクトするには、それは次のようになります。

// This line of yours should contain the activity that you want to launch. 
// You are currently just passing empty new Intent() 
PendingIntent contentIntent = 
    PendingIntent.getActivity(this, 0, new Intent(this, MyActivity.class), 0); 

+0

例を挙げてください。 – yoshi24

+0

私はあなたが変更する必要がある関連コードを追加しました – momo

+0

また、私は質問の外でこれを知っています、とにかく私は余分なものを渡すことができますか? – yoshi24

23

yoshi24さんのコメントとして、あなたはこのようにエキストラを設定することができるかもしれません。

final Intent intent = new Intent(this, MyActivity.class); 
intent.setData(data); 
intent.putExtra("key", "value"); 
final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0); 

はあなたにも、このようないくつかの事はあなた

INTごmainfest

のために動作します未決の意図

https://stackoverflow.com/questions/1198558/how-to-send-parameters-from-a-notification-click-to-an-activity

UPDATE のために行く前にこのことを認識する必要があります

<activity android:name=".MyActivity" android:launchMode="singleTop" ... /> 
あなたの活動の

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    processIntent(getIntent()); 
} 

@Override 
protected void onNewIntent(Intent intent) {  
    processIntent(intent); 
}; 

private void processIntent(Intent intent){ 
    //get your extras 
} 
+0

あなたが投稿したリンクは、何をしようとしていますか? – yoshi24

+1

投稿したのと同じです – Samuel

+1

インベントフラグを**アンドロイド:launchMode = "singleTask" **としてマニフェストに変更し、アクティビティ内の** onNewIntent(インテントの意図)**を上書きする必要があります。これにより、パラメータを受け取ることができます。 – Samuel

6

私はそれをやった、

  • 私は次のように行うのonCreate私は新しい意思

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    Notification notification = new Notification(R.drawable.ic_launcher, 
         "A new notification", System.currentTimeMillis()); 
    // Hide the notification after its selected 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    
    Intent intent = new Intent(this, NoficationDemoActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    Bundle bundle = new Bundle(); 
    bundle.putString("buzz", "buzz"); 
    intent.putExtras(bundle); 
    PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0); 
    notification.setLatestEventInfo(this, "This is the title", 
         "This is the text", activity); 
    notification.number += 1; 
    notificationManager.notify(0, notification); 
    
  • Intent.FLAG_ACTIVITY_CLEAR_TOPを追加します。

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    if(getIntent().getExtras()!=null){ 
        Toast.makeText(this, "Click", Toast.LENGTH_SHORT).show(); 
    } 
    
関連する問題