0

異なる特定の時間に複数のアラームを繰り返すことを実装しようとしています。問題は、Alarm Receiver がいつでも呼び出されないことです。

アクティビティ内の私のコード:

private void setAlarms() {
    Intent myIntent=new Intent(this, AlarmReceiver.class);
    myIntent.putExtra("MedName",medication_name);
    myIntent.setAction("b5.project.medibro.receivers.AlarmReceiver");
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    //am.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
    ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
    int counter=0;
    for(String timer: timers){
       //timers is an array of Strings in the format hh:mm
         try {
            String[] comps=timer.split(":");
            Calendar cal=Calendar.getInstance();
            cal.set(Calendar.HOUR_OF_DAY, Integer.valueOf(comps[0]));
            cal.set(Calendar.MINUTE, Integer.valueOf(comps[1]));
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            Log.d(TAG,comps[0]+" "+comps[1]+ "Alarm Time: " + cal.getTime().toString());
            PendingIntent pendingIntent = PendingIntent.getService(this, counter, myIntent,0);
            am.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                    cal.getTimeInMillis(),
                    AlarmManager.INTERVAL_DAY,
                    pendingIntent);
            counter++;
        } catch (ParseException e) {
            e.printStackTrace();
            Log.d(TAG,"Time Parsing error: "+e.getMessage());
        }
    }
}

これは私のアラーム受信機です:

public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals("b5.project.medibro.receivers.AlarmReceiver")) {
        Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
        Log.d("Alarm Receiver", "Alarm is invoked. ");

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setSmallIcon(R.drawable.messenger_bubble_large_blue);
        builder.setContentTitle("Time to take your medicine");
        String medName = intent.getStringExtra("MedName");
        String text = "Medicine name: " + medName;
        builder.setContentText(text);

        Notification notification = builder.build();
        NotificationManagerCompat.from(context).notify(0, notification);
        Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
    }
}

}

マニフェスト宣言:(アプリケーションタグ内)

<receiver android:name="b5.project.medibro.receivers.AlarmReceiver"
        android:enabled="true">
        <intent-filter>
            <action android:name="b5.project.medibro.receivers.AlarmReceiver"/>
        </intent-filter>
    </receiver>

上記のコードはブロードキャスト レシーバーを少なくとも 1 回実行する必要があると思いますが、logcat には何も表示されません。誰かが私に何が欠けているか教えてもらえますか?

4

1 に答える 1

0

getService の代わりに getBroadcast を試してください:

PendingIntent pendingIntent = PendingIntent.getService(this, counter, myIntent,0);

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, counter, myIntent,0);

http://developer.android.com/reference/android/app/PendingIntent.html#getBroadcast(android.content.Context , int, android.content.Intent, int)も見て、必要なフラグを見つけます。最後のアラーム (for ループ内) だけが機能しても驚かないからです。

于 2016-01-31T21:00:12.430 に答える