2016-10-02 14 views
0

私はアラームから呼び出されている通知レシーバを持っています。通知救助者から開始されたアクティビティへの同意の取得

通知受信機は、新たな意図(活動)のためにデータをバンドルし、このようにそれを開始します。新しいアクティビティで

// Create an in intent to go to the new alarm video 
Intent repeatingAlarmIntent = new Intent(context,RepeatingAlarmActivity.class); 

// Bundle the videoID the alarm has the correct video to open 
Bundle alarmExtras = new Bundle(); 
extras.putString("AlarmVidId", VideoId); 
repeatingAlarmIntent.putExtras(alarmExtras); 

// Set the flags for the alarm intent 
repeatingAlarmIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        repeatingAlarmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

// Start the alarm intent 
context.startActivity(repeatingAlarmIntent); 

私はそれがnullのバンドルデータを取得しようとすると、これはどのようですバンドルされたデータを取得しようとしています:

public class RepeatingAlarmActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener { 

    String VideoId; 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     Intent intent = getIntent(); 
     VideoId= intent.getStringExtra("AlarmVidId"); 

私が間違っていることがわかりません。

ご協力いただきありがとうございます。

答えて

1

は、以下で、すべてを置き換えます

Intent repeatingAlarmIntent = new Intent(context,RepeatingAlarmActivity.class); 
repeatingAlarmIntent.putExtra("AlarmVidId", VideoId); 
repeatingAlarmIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
repeatingAlarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
context.startActivity(repeatingAlarmIntent); 

あなたの問題は、あなたがextraという変数に余分なフィールドを入れている、しかし、あなたはalarmExtrasと呼ばれるBundleを作成しています。

Bundleを作成する必要はありません。 Intentに値を直接追加するだけです。

また、setFlags()を実行しています。 2つ以上必要な場合は、addFlags()代わりに

0

extrasの変数がputStringを呼び出すときに、alarmExtrasと一致しない場合は、インテントに追加されます。

Bundle alarmExtras = new Bundle(); 
alarmExtras.putString("AlarmVidId", VideoId); 
repeatingAlarmIntent.putExtras(alarmExtras); 
+0

お世話になりました。ですから、変数名を両側で同じにする必要がありますか?私はVideoIdを両側で変更しましたが、それはまだ同じです。それはあなたが意味することですか? –

+0

すべての変数名があなたのソリューションで試したものと同じになるように私は自分の質問を編集しました。私があなたを間違って理解した場合 –

+0

私はそれを確認した –

0

私は解決策を見つけました。

通知受信者のライフサイクルとコンテキストのためです。

受信者の起動時に渡されたコンテキストを使用する代わりに、そのコンテキストを使用してアプリケーションコンテキストを取得しました。

このメソッドは動作していますが、このメソッドの潜在的な問題については、前にこのような問題を解決する必要はありませんでした。

これは私のために働いていたものです:

// Create an in intent to go to the new alarm video 
Intent repeatingAlarmIntent = new Intent(context.getApplicationContext(),RepeatingAlarmActivity.class); 

// Bundle the videoID the alarm has the correct video to open 
Bundle alarmExtras = new Bundle(); 
extras.putString("AlarmVidId", VideoId); 
repeatingAlarmIntent.putExtras(alarmExtras); 

// Set the flags for the alarm intent 
repeatingAlarmIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        repeatingAlarmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

// Start the alarm intent 
context.getApplicationContext().startActivity(repeatingAlarmIntent); 

を意図を作成し、今そこにある意図を起動する時には、上記:アクティビティ側で

getApplicationContext() 

は必要は変化はなかったです。

関連する問題