2016-10-28 11 views
0

新しいアクティビティで非アクティビティからダイアログを開くにはどうすればよいですか?私はカウントダウンタイマーを実行するサービスを持っており、カウントダウンタイマーが終了したら、メインアクティビティでダイアログを開くことができます。私はこれを理解しているようです。私は正常に動作しなかったアラートダイアログを設定しようとしましたが、エクストラとバンドルでインテントを送信しようとしましたが、まだ運がありませんでした。私はGoogleで検索して、this linkの解決に疲れました。別のアクティビティでアラートダイアログを開く方法

 @Override 
     public void onFinish() { 
      Log.i(TAG, "Timer finished"); 
      cdt.start(); 
      showNotification(); 
      addpoints(); 
      savepref(); 
      Intent intent = new Intent(BroadcastService.this, MainActivity.class); 
      Bundle bundle = new Bundle(); 
      bundle.putInt("alert_icon_res_id", android.R.drawable.ic_dialog_info); 
      bundle.putString("alert_title", "Some Title"); 
      bundle.putString("alert_message", "Some message"); 
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

      startActivity(intent); 





     } 
    }; 

    cdt.start(); 
} 

ここでの主な活動コード:OnFinish

setContentView(R.layout.activity_main); 

    startService(new Intent(this, BroadcastService.class)); 
    Log.i(TAG, "Started service"); 

    Bundle b = getIntent().getExtras(); 
    if (b != null && b.containsKey("alert_icon_res_id")) { 
     int icon = b.getInt("alert_icon_res_id"); 
     String title = b.getString("alert_title"); 
     String message = b.getString("alert_message"); 

     new AlertDialog.Builder(this).setIcon(icon) 
       .setTitle(title).setMessage(message) 
       .setPositiveButton("Close", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
        } 
       }).create().show(); 
    } 

答えて

0

あなたは意志でバンドルを入れていません。

はこれを試してみてください:

Bundle bundle = new Bundle(); 

bundle.putInt("alert_icon_res_id", android.R.drawable.ic_dialog_info); 

bundle.putString("alert_title", "Some Title"); 

bundle.putString("alert_message", "Some message"); 

intent.putExtras(bundle); // add this line 

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

startActivity(intent); 
+0

この作品!!今はお互いに新しい活動を続けています。 –

関連する問題