2016-07-24 2 views
0

アラームアプリケーションを作成しています。指定した時刻にAlarmBroadcastが起動します。 指定時間にAlert Dialogを追加します。BrodcastReceiverから警告ダイアログを起動できますか?

これは私が行ったことです。

public class AlarmBrodcast extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 


     final AlertDialog alertDialog = new AlertDialog.Builder(context.getApplicationContext()).create(); 
     alertDialog.setTitle("Delete Remainder"); 
     alertDialog.setMessage("Are you sure you want to Delete this Remainder"); 
     alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       alertDialog.dismiss(); 

      } 
     }); 


     alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "No", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       alertDialog.dismiss(); 
      } 
     }); 


     alertDialog.show(); 

これにより、次のエラーメッセージが表示されます。

java.lang.RuntimeException:受信機 com.example.taha.alarmproject.AlarmBrodcastをインスタンス化できません: とjava.lang.ClassCastException: com.example.taha.alarmproject.AlarmBrodcastは アンドロイドにキャストすることはできません

編集 MainActivity .content.BroadcastReceiver

 Intent intent = new Intent(this, AlarmBrodcast.class); 
     intent.putExtra("message", "Alarm Message 00001"); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(
       this.getApplicationContext(), 234324243, intent, 0); 

     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
/*  alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);*/ 


     alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() 
       + (i * 1000), pendingIntent); 


     Toast.makeText(this, "Alarm set in " + i + " seconds", Toast.LENGTH_LONG).show(); 

またActivityBrodcastReceiverから起動しようとしましたが、それも起動しませんでした。

+1

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

+0

はい、私は正常にトーストメッセージを印刷することができました – Kirmani88

+2

ブロードキャストレシーバーをインスタンス化するコードを投稿してください – Lino

答えて

1

この簡単な解決策の1つはEventです。

compile 'org.greenrobot:eventbus:3.0.0' 

があなたのbuild.gradle(モジュールレベルの)ファイルにその単一の行を追加します。これは、あなたが、私は私のプロジェクトで使用気の利いた小さなライブラリでそれを行う方法です。

onReceiveが呼び出されたときにAlarmBroadcastActivityクラスの一部に通知できるようにするためのアイデアです。

イベントを表すPlain Old Java Object (POJO)クラスを作成してください!

public class BroadCastEvent{ 
    private boolean isCompleted; 

    BroadCastEvent(boolean completed){ 
    this.isCompleted = completed; 
    } 

    //getter 
    public boolean isCompleted(){ 
    return this.isCompleted; 
    } 
} 

さて、あなたAlarmBroadcastクラスのonReceiveメソッド内:

EventBus.getDefault().post(new BroadCastEvent(true)); 

次に、あなたの活動に、このようなこのイベントをリッスンするように登録:

EventBus.getDefault().register(this); 

次に、このメソッドをオーバーライド:

public void onEvent(BroadCastEvent event){ 
    if(event.isCompleted()){ 
     //show your dialog here or start next activity 
    } 
} 

次に、onDestroyメソッド内の登録を解除eventbus:これはあなたのコードを分離して、あなたのAlarmBroadcastクラスPublisherとあなたの活動Subscriber行うことができます

@Override 
public void onDestroy(){ 
    super.onDestroy(); 

    EventBus.getDefault().unregister(this); 
} 

私はこれが助けてくれることを願っています。

関連する問題