2014-09-29 8 views
1

Android에서 알람 서비스를 구현하려고합니다.이 서비스는 매일 alarmmanager를 구입합니다. 나는 alarmmanager setRepeating()의 메소드가 API 18에서 API 19로 변경되었다는 것을 읽었습니다. 이제 setExact()을 사용해야합니다. 현재 API 18을 구현하고 있습니다. 따라서 영향을받지 않아야합니다. setRepeating()을 사용하고 처음 알림이 시작될 때 적절한시기에 발생합니다. 그러나 그때 그것은 미쳐 가고 있습니다 : D 알림은 무작위로 계속 해고됩니다. 때로는 3 번, 2 번은 아무것도하지 않습니다.Android 일일 알림 setRepeating 스팸 메일

내 코드 :

private void startAlarm(){ 
    Calendar calendar = Calendar.getInstance(); 

    calendar.set(Calendar.SECOND, 0); 
    calendar.set(Calendar.MINUTE, 30); 
    calendar.set(Calendar.HOUR, 7); 
    calendar.set(Calendar.AM_PM, Calendar.PM); 
    calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH)); 

    Intent myIntent = new Intent(this , NotifyService.class); 
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
    pendingIntent = PendingIntent.getService(this, 0, myIntent, 0); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 60 * 24, pendingIntent); 
} 

내 NotifyService 클래스는 다음과 같습니다 :

public class NotifyService extends Service { 

private PendingIntent pendingIntent; 

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

@Override 
public void onCreate(){ 
    Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

    NotificationManager mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
    Intent intent1 = new Intent(this.getApplicationContext(), MainActivity.class); 
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0); 

    Notification mNotify = new NotificationCompat.Builder(this) 
      .setAutoCancel(true) 
      .setContentTitle(text) 
      .setContentText(text) 
      .setSmallIcon(R.drawable.pic) 
      .setContentIntent(pIntent) 
      .setSound(sound) 
      .build(); 

    mNM.notify(1, mNotify); 
} 
} 

관련 수입은 다음과 같습니다

처음 내 응용 프로그램은 다음 줄이 실행됩니다 시작되고있다
import android.support.v4.app.NotificationCompat; 
import android.annotation.TargetApi; 
import android.app.AlarmManager; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 

나는 또한 setExact을 사용해 보았습니다. i API 19 및 setExact를 호출 할 때마다 알림이 시작되지만 성공하지 못합니다.

아무도 모릅니다.

답변

2

누군가 내 질문에 대한 답변을 찾고있는 경우 : 여기에 내가 한 일과 효과가 있습니다.

작성한 서비스를 중지해야합니다. 바로

mNM.notify(1, mNotify); 

아래에있는 내 NotifyService 클래스에서 그래서는이 호출을 추가 : 이것은 나를 위해 일한

stopSelf(); 

합니다.

편집 : 장치가 종료 된 경우에도 알림을 표시하려면 Boot_Completed 수신기를 추가하지 않아도됩니다.

그래서 수신기 클래스 :

public class BootCompleteReceiver extends BroadcastReceiver { 

    private PendingIntent pendingIntent; 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     startAlarm(); // See Method above (in Question) 

    } 
} 

그리고 당신의 적하 목록 추가에

:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 

<receiver 
     android:name="package.BootCompleteReceiver" 
     android:process=":remote" > 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
</receiver> 
관련 문제