2013-01-15 16 views
5

通知とイベントを通じてアクティビティを実行しようとしていますonCreate "リダイレクト"したいと思います。これには、Intentクラスの情報を考えてください。重要な特徴は、通知を生成するクラスがサービスを通じて実行されることです。私は、コンテキストandroid.app.Applicationによって提供されるgetApplicationContextメソッドから取得します。私がメソッドgetExtras()を呼び出すときはいつでも、nullを返す。私は間違って何をしていますか?Intent.getExtras()は常にnullを返します

public class OXAppUpdateHandler { 

    private void addNotification(Context context, int iconID, 
      CharSequence tickerText, CharSequence title, CharSequence content) { 

     CharSequence notificationTicket = tickerText; 
     CharSequence notificationTitle = title; 
     CharSequence notificationContent = content; 

     long when = System.currentTimeMillis(); 

     Intent intent = new Intent(context, MainActivity_.class); 
     intent.setFlags(
      Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     intent.putExtra(OPEN_UPDATE_ACTIVITY_KEY, 1); 

     PendingIntent pendingIntent = 
      PendingIntent.getActivity(context, 0, intent, 0); 

     NotificationManager notificationManager = 
      (NotificationManager) context.getSystemService(
       Context.NOTIFICATION_SERVICE); 
     Notification notification = 
      new Notification(iconID, notificationTicket, when); 
     notification.setLatestEventInfo(context, notificationTitle, 
             notificationContent, pendingIntent); 
     notificationManager.notify(NOTIFICATION_ID, notification); 
    } 

    public static boolean isUpdateStart(Intent intent) { 
     Bundle bundle = intent.getExtras(); 
     boolean result = bundle != null && 
         bundle.containsKey(OPEN_UPDATE_ACTIVITY_KEY); 
     if (result) { 
      bundle.remove(OPEN_UPDATE_ACTIVITY_KEY); 
     } 
     return result; 
     } 
    } 

    @EActivity(R.layout.activity_main) 
    public class MainActivity extends Activity { 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      if (OXAppUpdateHandler.isUpdateStart(getIntent())) { 
       startActivity(new Intent(this, UpdateActivity_.class)); 
      } 
     } 
    } 
+0

エクストラをインテントに入れるコードを投稿できますか? –

+0

通知をブロードキャストにするか、その他の場合はベースコンテキストを取得しようとします。 –

+0

@ChangdeoJadhavメソッドOXAppUpdateHandler。コードintent.putExtra(OPEN_UPDATE_ACTIVITY_KEY、1)でaddNotification(...)を実行します。 – adrianosepe

答えて

20

を、私は窓の外に傾くために行くとあなたの問題がここにあると推測しています:

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 
あなたはブール値をチェックしている場合は、次のようにあなたがバンドルに置くデータを取得する必要があります

intentgetActivity()に渡しており、Intentに一致するPendingIntentが返ってきて、あなたのエキストラが含まれていることが予想されます。お使いのIntentIntentのエキストラを考慮してを使用していない)に一致するPendingIntentがシステム内に既に存在する場合、getActivity()は代わりにPendingIntentを返します。

これが問題であるかどうかを確認するには、この方法を試してください。

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 
        PendingIntent.FLAG_UPDATE_CURRENT); 

これはPendingIntentがすでに存在する場合、それはあなたの中のものでエキストラを置き換える必要があり、システム内のあなたのIntentどこかに一致することを言いますintentパラメータ。

+0

これは私のために働いた! 活性が常に再現されなかったので、私はまた、以下のコードを含む: '@EActivity(R.layout.activity_main) パブリッククラスMainActivityはOXBaseActivity {\tを拡張... \t @Override \t保護ボイドonNewIntent(目的意識) \t { \t \t super。onNewIntent(インテント); \t \t \t \t IF(OXAppUpdateHandler.isUpdateStart(意図)) \t \t { \t \t \t startActivity(新しいインテント(この、UpdateActivity_.class))。 \t \t} \t}} あなたは 'Intent.FLAG_ACTIVITY_CLEAR_TOP'と' Intent.FLAG_ACTIVITY_SINGLE_TOP'の両方を設定しているので、 ' – adrianosepe

+1

活動は常に再作成されていません。アクティビティを常に再作成したい場合は、 'Intent.FLAG_ACTIVITY_SINGLE_TOP'を' IntAnt'から削除して 'getActivity()'に渡してください。 –

+0

それは働いた。私はPendingIntent.FLAG_UPDATE_CURRENTの代わりに0を渡していました。どうもありがとう。 –

-1

(1)私はあなたがデータに意図を追加しているコードが表示されないよう/あなたはPUTを使用していることを確認し、正しくエキストラを取得するためにhereを確認してください。

(2)get intentを呼び出して余分になり、実際にはバンドルから何も取得していないようです(情報が存在すると仮定して)。

Bundle bundle = getIntent().getExtras(); 

if (bundle.getBooleanExtra("WHATEVER"){ 
    //whatever you want to do in here 
} 
+0

Iは '... intent.putExtra(OPEN_UPDATE_ACTIVITY_KEY、1)に情報を追加してから' と 'バンドルのバンドル= intent.getExtrasを(回復)。 ブール値result = bundle! = null && bundle.containsKey(OPEN_UPDATE_ACTIVITY_KEY); ' – adrianosepe

+0

あなたは余分なものを正しく取得していません。 。 。私が提案した方法で試しましたか? – Rarw

+0

私の場合はgetIntent()を呼び出すとGetExtras()がnullを返します – adrianosepe

関連する問題