2017-01-19 8 views
0

2つの異なるアクティビティを実行して2つの異なる操作を行う2つのボタンを持つ通知を作成しようとしています。通知とそのボタンは通知バーに表示されますが、いずれかのボタンを押すと指定したアクティビティが起動しません。単にの値を変更するのいずれかの簡単な方法がある場合は通知ボタン(.addaction)起動アクティビティがありません

public class stopActivity { 
    public stopActivity() { 
    MainActivity.stopped = true; 
    } 
} 

private void addNotification() { 
    int requestID = (int) System.currentTimeMillis(); 

    NotificationCompat.Builder builder = (android.support.v7.app.NotificationCompat.Builder) new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.ibutton) 
      .setContentTitle("Timer running..") 
      .setContentText(timerString); 

    Intent notificationIntent = new Intent(this, MainActivity.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, requestID, notificationIntent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 
    builder.setContentIntent(contentIntent); 

    //Add buttons to notification 
    //First make the intent 
    Intent pauseIntent = new Intent(this, pauseActivity.class); 
    //Wrap it in pending intent to trigger later 
    PendingIntent pausePendingIntent = PendingIntent.getActivity(this, requestID, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    //Then add the action to your notification 
    builder.addAction(R.drawable.pause, "Pause", pausePendingIntent); 

    //Same again for stop button 
    Intent stopIntent = new Intent(this, stopActivity.class); 
    PendingIntent stopPendingIntent = PendingIntent.getActivity(this, requestID, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    builder.addAction(R.drawable.stop, "Stop", stopPendingIntent); 

    // Add as notification 
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    int mNotificationId = 001; 
    notificationManager.notify(mNotificationId, builder.build()); 


} 

EDIT:私は起動しようとしている活動の一つの例ここで

コードです通知バーのボタンからMainActivityの変数を入力すると、それを聞きたいと思っていますが、唯一の選択肢は新しいアクティビティを開始することだけです。

+0

あなたは、あなたのアプリの古いバージョンを見ていませんか?インスタンスの.classを呼び出すことはコンパイルされていません。 MainActivityで行ったのと同じように、これをClassの代わりに変更してみてください。 – Sander

+0

私はちょうどあなたのコードをテストし、それは完全に働いた。 pauseActivityとstopActivityはアクティビティ名ですか? –

+0

マニフェストに 'pauseActivity'と' stopActivity'を追加しましたか? –

答えて

0

編集:私は起動しようとしている活動の一つの例:

パブリッククラスstopActivity {公共stopActivity(){ をMainActivity.stopped =はtrue。 }}

これはActivityではありません。 Activityextends Activity

これはちょうどPOJOクラスであり、実行中の方法でPendingIntentに入れて開始することはできません。


あなたはそれで値を変更するには、「余分な」とMainActivityためIntentを使用したほうが良いでしょう。ここでは例です:

Intent pauseIntent = new Intent(this, MainActivity.class); 
pauseIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
pauseIntent.putExtra("pause", true); 
PendingIntent pausePendingIntent = PendingIntent.getActivity(this, requestID, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
//Then add the action to your notification 
builder.addAction(R.drawable.pause, "Pause", pausePendingIntent); 

その後、MainActivity.onNewIntent()に:

if (intent.hasExtra("pause")) { 
    this.paused = true; // whatever you want to do when "pause" is pressed 
} 
関連する問題