2015-08-15 100 views
0

我一直在試圖解決這個問題數週。基本問題是,長時間放置設備(8小時)後,預定的通知將無法激活。在測試中,我一直在使用兩個設備,LG Optimus Elite和三星Galaxy Note S4。在Optimus Elite上,通知會在設備被喚醒時發生,但在注意時,通知會完全消失並且不會顯示。整個系統在設備醒着或短時間不活動後運行。Android Alarmmanager通知在長時間閒置期間未出現

public class AlarmReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    try { 
     WakeLocker.acquire(context); 
     Bundle bundle = intent.getExtras(); 
     Intent i = new Intent(context, AlarmService.class); 
     i.putExtra("name", bundle.getString("name")); 
     i.putExtra("alarm", bundle.getString("alarm")); 
     context.startService(i); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

}

**public class AlarmService extends Service { 

LocalBroadcastManager broadcaster; 
static final public String REFRESH_INTENT = "rf"; 

@Override 
public IBinder onBind(Intent arg0) 
{ 
    // TODO Auto-generated method stub 
    return null; 
} 

public void handleIntent(Intent intent){ 
    broadcaster = LocalBroadcastManager.getInstance(this); 
    Bundle bundle = intent.getExtras(); 
    String message = bundle.getString("alarm"); 
    if(message.equals("dosesTakenResetAlarm")){ 
     dailyReset(getApplicationContext()); 
    } 
    else if(message.equals("scheduledReminder")){ 
     scheduledReminder(bundle.getInt("code", 0), bundle.getString("name"), getApplicationContext()); 
    } 
    WakeLocker.release(); 
} 

public void onStart(Intent intent, int startId){ 
    handleIntent(intent); 
} 

public int onStartCommand(Intent intent, int flags, int startId){ 
    handleIntent(intent); 
    return START_NOT_STICKY; 
} 

@Override 
public void onDestroy() 
{ 
    // TODO Auto-generated method stub 
    super.onDestroy(); 
    WakeLocker.release(); 
} 

private void dailyReset(Context context){ 
    FileWriter fw = new FileWriter(context); 
    AlarmCreator ac = new AlarmCreator(); 
    fw.fp.onloadString(fw.readFromFile()); 
    int medNum = 0; 
    while(fw.fp.hasNext()){ 
     SaveData mv = fw.readEntry(medNum); 
     fw.fp.onloadMedString(mv.data); 
     String name = fw.fp.nextMedVariable(); 
     String doses = fw.fp.nextMedVariable(); 
     String frequency = fw.fp.nextMedVariable(); 
     String frequencyS = fw.fp.nextMedVariable(); 
     String reminders = fw.fp.nextMedVariable(); 
     String dosesT = fw.fp.nextMedVariable(); 
     frequencyS = ""+(Integer.parseInt(frequencyS)-1); 
     if(Integer.parseInt(frequencyS) < 0){ 
      frequencyS = ""+(Integer.parseInt(frequency)-1); 
     } 
     fw.replaceEntry(fw.fp.buildMedSaveString(name, doses, frequency, frequencyS, reminders, "0"), mv.position1, mv.position2); 
     if(frequencyS.equals("0")){ 
      fw.fp.onloadReminderString(reminders); 
      int reminderNum = 0; 
      while(fw.fp.hasNextReminder()){ 
       ReminderData temp = fw.fp.nextReminder(); 
       if(temp.half.equals("1")){ 
        temp.hour = ""+(Integer.parseInt(temp.hour)+12); 
       } 
       ac.setAlarm(Integer.parseInt(temp.hour), Integer.parseInt(temp.minute), 0, ac.buildAlarmCode(medNum, reminderNum), name); 
       reminderNum++; 
      } 
     } 
     medNum++; 
     fw.fp.nextMedication(); 
    } 

    Intent intent = new Intent(REFRESH_INTENT); 
    broadcaster.sendBroadcast(intent); 
} 

private void scheduledReminder(int code, String n, Context context){ 
    Intent intent = new Intent(context, Main.class); 
    PendingIntent pIntent = PendingIntent.getActivity(context, 101, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(context) 
        .setSmallIcon(R.drawable.deleted) 
        .setContentTitle("Pillbud Reminder") 
        .setContentText("Take a dosage of "+n+"."); 
    mBuilder.setDefaults(Notification.DEFAULT_ALL); 
    mBuilder.setContentIntent(pIntent); 
    mBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); 
    NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    mNotifyMgr.notify(code, mBuilder.build()); 
} 

} **

此外,如果任何人有關於如何誘導此睡眠狀態下這將是測試非常有幫助的任何建議。

+0

爲什麼要以'droids全稱的名義,是否有人投下此問題? WTF? –

回答

0

您需要搶一個WakeLock。看一看無論是WakefulBroadcastReceiver,或CommonsWare蓋伊的WakefulIntentService

至於把設備進入休眠狀態,如果你有根,則亞行外殼進去,獲得root並輸入:

echo standby > /sys/power/state 

還有更多有關電源狀態的信息here

如果您沒有root用戶,那麼應撥打PowerManager,goToSleep,這應該這樣做。您需要授予調用應用「DEVICE_POWER」權限

+0

WakefulBroadcastReceiver是我最初使用的,它似乎工作得很好,除了每個人都有一次,它不會完成「AlarmService」代碼。所以我會再次嘗試,除了每次循環時更新我的​​文件,我會在最後更新它,並將「Start_Not_Sticky」更改爲「Start_Redeliver_Intent」。這將有望帶來理想的效果。 – Asky

+0

不客氣。 –

相關問題