2015-01-17 2 views
2

나는 이것을 위해 3 일을 검색했습니다. 나는 밖으로 거기에 모든 솔루션을 시도 구글stackoverflow.com.AlarmManager는 응용 프로그램이 시작될 때마다 작동합니다

저는 MainActivity입니다.이 부분은 BroadcastReceiver입니다.

Calendar calendar3 = Calendar.getInstance(); 

    calendar3.set(Calendar.HOUR_OF_DAY, 7); 
    calendar3.set(Calendar.MINUTE, 34); 
    calendar3.set(Calendar.SECOND, 00); 
    Intent intent = new Intent(MainAcitivty.this, BroadCastReciver.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainAcitivty.this, 0, intent, 0); 
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
    alarmManager.set(AlarmManager.RTC, calendar3.getTimeInMillis(), pendingIntent); 

나는 행운도없이 alarmManager.setReapeating을 시도했다.

Intent i = new Intent(context,MyService.class); 
context.startService(i); 

서비스

public class MyService extends Service { 
MyDB db; 

@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Log.e("Yamen", "service Started"); 
    db = new MyDB(getApplicationContext()); 
    GregorianCalendar calendar = new GregorianCalendar(); 
    Date time = calendar.getTime(); 
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy"); 
    String timeString = simpleDateFormat.format(time); 
    SharedPreferences mySharedPreferences = getApplicationContext().getSharedPreferences("myPref",Context.MODE_PRIVATE); 
    boolean isNoti=mySharedPreferences.getBoolean("isNoti",true); 
    if (isNoti) { 
     ArrayList<AnniBean> annis = 
       db.getAnni(timeString); 
     Log.e("Yamen", "size is " + annis.size()); 
     for (AnniBean anni : annis) { 
      new NotificationCreator(getApplicationContext(), "Anniversary Reminder", anni.getAnniName(), anni.getAnniDetails()); 
     } 
    } 
    Log.e("Yamen", "service finished"); 
    return START_STICKY; 
} 

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

notification 34 초에 매일 나타납니다

그런 다음 BroadcastReceiver는 DB에서 일부 정보를 얻고 이런 notification 이상에 넣어 service을 시작합니다 매력과 같습니다. 문제는 BroadcastReceiver이고 service은 응용 프로그램을 열 때마다 호출됩니다.

많은 링크를 방문했기 때문에 어떤 링크도 참조하지 마십시오. 조언에 감사드립니다.

+0

시도가 아직 설정 여부를 응용 프로그램에 다시 –

+0

을 알람을 설정하기 전에'PendingIntent.FLAG_NO_CREATE'을 사용하기 시작 자사의 –

+0

후 방송 수신기 코드 및 매니페스트 –

답변

0

방송 수신 인 텐트 XML에 문제가 있습니다. 자바에서는 : 그것의 수신하는 다른 방송은이 같은 작용에 의해 필터링해야

Intent intent = new Intent(MainAcitivty.this, BroadCastReciver.class); 
intent.setAction("your.package.name.your.anything"); 

XML에 :

<receiver android:name=".MyBroadcastReceiver"> 
<intent-filter> 
     <action android:name="your.package.name.your.anything" /> 
</intent-filter> 
</receiver> 
+0

나는 이것을 실제로 같은 오류로 시도했다. 앱은 매번 알람 관리자를 계속 실행합니다. –

0

를 다음 코드로 시도 :

Calendar cal = Calendar.getInstance(); 
cal.set(Calendar.HOUR_OF_DAY, 7); 
cal.set(Calendar.MINUTE, 34); 
cal.set(Calendar.SECOND, 0); 

if(cal.before(Calendar.getInstance())){ // if it's in the past, increment 
    cal.add(Calendar.DATE, 1); 
} 

Intent intent = new Intent(MainAcitivty.this, BroadCastReciver.class); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainAcitivty.this, 0, intent, 0); 
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); 

이 알람이 실행됩니다 지정된 시간에 매일. 알람을 확인

관련 문제