2016-08-15 1 views
1

私は、素晴らしい作品で、ユーザーが選択した特定の時間に簡単なリマインダとして通知を表示します(デバイスが起きていない、通知音を鳴らします)。デバイスが数時間眠っているときにアラームが消えない(マシュマロで)

午前中に目を覚ますためにアラームとして使用すると、通知音が再生されません。

デバイスを数時間ロックする必要があるため、テストするのは難しいです。私はそれがドーズと関係があると思うが、わからない。また、デバイスが夜通し充電されている場合は、アラームを再生します。手動で午前中にデバイスを目覚めた後、通知を受けます

...

アラームを設定上のコード:

 // Create an intent that will be wrapped in PendingIntent 
     Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class); 
     intent.putExtra("id",id); 

     // Create the pending intent and wrap our intent 
     PendingIntent pendingIntent = 
       PendingIntent.getBroadcast(getApplicationContext(),(int)id, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

     //get the alarm manager 
     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
     if (android.os.Build.VERSION.SDK_INT >= 19) 
     { 
      alarmManager.setExact(AlarmManager.RTC_WAKEUP, date.getTime(), pendingIntent); 
     } 
     else if (android.os.Build.VERSION.SDK_INT >= 23) 
     { 
      alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, date.getTime() , pendingIntent); 
     } 
     else alarmManager.set(AlarmManager.RTC_WAKEUP, date.getTime() , pendingIntent); 


     //go back to the main activity 
     onBackPressed(); 

マイAlarmReceiver:

public class AlarmReceiver extends WakefulBroadcastReceiver 
{ 
@Override 
public void onReceive(Context context, Intent intent) 
{ 
    Log.e("ALARMRECEIVER","ONRECEIVE"); 

    //Create a notification 
    long notificationId = intent.getLongExtra("id", -1); 

    if(notificationId == -1) 
    { 
     Log.e("AlarmReceiver","id went missing"); 
    } 
    else 
    { 
     NotificationRepository repository = NotificationRepository.getInstance(context); 
     Notification notification = repository.getNotification(notificationId); 


     if(notification != null) 
     { 
      String[] icons = context.getResources().getStringArray(R.array.icons); 
      int iconId = context.getResources().getIdentifier(context.getPackageName() 
        + ":drawable/" + icons[notification.getIconIndex()], null, null); 

      //create the android notification 
      NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) 
        .setSmallIcon(iconId) 
        .setContentTitle(notification.getTitle()) 
        .setContentText(notification.getSubtitle()) 
        .setColor(ContextCompat.getColor(context, R.color.colorPrimary)); 

      if(notification.isPlaySound()) 
      { 
       mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); 
       Log.e("ALARMRECEIVER", "SOUND"); 
      } 
      else Log.e("ALARMRECEIVER","NO SOUND"); 

      if (notification.isVibrate()) 
      { 
       mBuilder.setVibrate(new long[]{1000, 1000}); 
      } 

      NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
      // mId allows you to update the notification later on. 
      mNotificationManager.notify((int) notificationId, mBuilder.build()); 
      //Delete the notification from the database 
      repository.removeNotification(notificationId); 

      Intent i = new Intent("dvanack.gmail.com.NOTIFY"); 
      context.sendBroadcast(i); 
      AlarmReceiver.completeWakefulIntent(intent); 
      Log.w("ONRECEIVE","ENDED"); 
     } 

    } 

} 

答えて

4

私は思いますあなたはマシュマロ以上でこの問題を抱えています。

以下のコードに問題があるようです:アンドロイドバージョンはマシュマロであれば、それは再び最初の場合の条件になりますので、到達できない「場合は他に」...ここ

if (android.os.Build.VERSION.SDK_INT >= 19) 
    { 
     alarmManager.setExact(AlarmManager.RTC_WAKEUP, date.getTime(), pendingIntent); 
    } 
    else if (android.os.Build.VERSION.SDK_INT >= 23) 
    { 
     alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, date.getTime() , pendingIntent); 
    } 

バージョンが> = 19であるためです。これが問題を解決することを願っています。

だから条件は次のようになります。実際に

if (android.os.Build.VERSION.SDK_INT >= 23) 
{ 
    alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, date.getTime() , pendingIntent); 
} 
else if (android.os.Build.VERSION.SDK_INT >= 19) 
{ 
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, date.getTime(), pendingIntent); 
} 
+0

!ありがとう、私はこれが問題を解決することをかなり確信しています、私はそれをできるだけ早くテストします – PrisonMike

+0

私は結果を知らせてください、他の人にも役立つようにこれを答えとしてマークしてください。 –

+0

もちろん!良い眼btw私はそこにエラーを見ていない方法を理解していない.. – PrisonMike

関連する問題