0

私は現在作業中のプロジェクトにアラームを追加しました。彼らは私のすべての条件でうまくいっています。 1つの小さな問題に直面する。私はワンショットのアラームを追加しました。時間通りに発射したいと思っています。それはうまくいきます。時間がたつと、私が意図している値を使って活動を開始しています。問題が発生してもアラームが発生しても、保留中のインテントが再び呼び出され、そのアクティビティを閉じるとnull値でアクティビティが開きます。そのアクティビティを閉じると何度もポップアップします。保留中の意図は、指定された時刻に起動した後にキャンセルされません。以下はアラームを設定する私のコードです。アラーム保留中のインテントが何度も呼び出される

public void setAlarm(int hours, int minutes,String AMPM, String alarmType,String message, String extraMessage) 
    { 

     Random rand = new Random(); 
     int n = rand.nextInt(10000) + 1; 
     Log.e("HMA",hours+" "+minutes+" "+AMPM + " " + message + extraMessage); 

//      Toast.makeText(getApplicationContext(), "Alarm On", 
     // Toast.LENGTH_SHORT).show(); 
     Calendar calendar = Calendar.getInstance(); 
     // set selected time from timepicker to calendar 
     calendar.set(Calendar.HOUR_OF_DAY, hours); 
     calendar.set(Calendar.MINUTE,minutes); 
     calendar.set(Calendar.SECOND,00); 


     if(AMPM.equals("PM")) { 
      Log.e("PM",""); 
      calendar.set(Calendar.AM_PM, Calendar.PM); 
     } 
     else 
     { 
      calendar.set(Calendar.AM_PM, Calendar.AM); 
     } 
     Intent myIntent = new Intent(this, 
       MyReceiver.class); 
     AlarmManager alarmManager; 
     alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

     myIntent.putExtra("alarmType",alarmType); 
     myIntent.putExtra("gifMessage",message); 
     myIntent.putExtra("extraMessage",extraMessage); 


     PendingIntent pendingIntent; 

     // A PendingIntent specifies an action to take in the 
     // future 
     pendingIntent = PendingIntent.getBroadcast(
       this, n, myIntent, 0); 

     // set alarm time 
     if(Build.VERSION.SDK_INT<19) { 
      alarmManager.set(AlarmManager.RTC, 
        calendar.getTimeInMillis(), pendingIntent); 
     } 
     else if(Build.VERSION.SDK_INT>=19 && Build.VERSION.SDK_INT<=22) 
     { 
      alarmManager.setExact(AlarmManager.RTC, 
        calendar.getTimeInMillis(), pendingIntent); 
     } 
     else if(Build.VERSION.SDK_INT>=23) 
     { 
      alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC, 
        calendar.getTimeInMillis(), pendingIntent); 
     } 
    } 
+0

このクラスのエラーメッセージと完全なコードを投稿してください – Raja

答えて

0

サービスの代わりにIntentServiceを使用し、onStartCommandでSTART_NOT_STICKYを返して解決しました。

関連する問題