>

안드로이드에서 매일 알림을 받기 위해 alarmanager를 시도했습니다 .... 알람은 지정된 시간에 시작되지만 그 후 1 분마다 반복됩니다 ... Mainactivity의 setrepeating () 함수에 INTERVAL_DAY를 지정했지만 그렇게합니다. 작동하지 않는 것 같습니다. Mainactivity, MyReceiver&AlarmService의 세 부분으로 구성됩니다. 누구든지 이것을 고칠 수 있습니까 ??

유지 관리

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Calendar calendar = Calendar.getInstance();
// we can set time by open date and time picker dialo
        calendar.set(Calendar.HOUR_OF_DAY, 12);
        calendar.set(Calendar.MINUTE, 10);
        calendar.set(Calendar.SECOND, 0);
        Intent intent1 = new Intent(MainActivity.this, MyReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am = (AlarmManager) getSystemService(this.ALARM_SERVICE);
        Log.e("Tag","calling here");
        am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),864000, pendingIntent);
    }
}

내 수신자

public class MyReceiver extends BroadcastReceiver{
    int MID=0;
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Intent notificationIntent = new Intent(context, MainActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
                context).setSmallIcon(R.drawable.icon)
                .setContentTitle("Alaram Fired")
                .setContentText("Events To be PErformed").setSound(alarmSound)
                .setAutoCancel(true).setWhen(when)
                .setContentIntent(pendingIntent)
                .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
        notificationManager.notify(MID, mNotifyBuilder.build());
        MID++;
    }
}

MyAlarmService

public class MyAlarmService extends Service
{
    private NotificationManager mManager;
    @Override
    public IBinder onBind(Intent arg0)
    {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public void onCreate()
    {
        // TODO Auto-generated method stub
        super.onCreate();
    }
    @SuppressWarnings("static-access")
    @Override
    public void onStart(Intent intent, int startId)
    {
        Log.e("Tag1","alarmservice here");
        super.onStart(intent, startId);
        mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
        Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);
        Notification notification = new Notification(R.drawable.icon,"This is a test message!", System.currentTimeMillis());
        intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_CANCEL_CURRENT);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
       //notification.setLatestEventInfo(this.getApplicationContext(), "Daily Notification Demo", "This is a test message!", pendingNotificationIntent);
        mManager.notify(0, notification);
    }
    @Override
    public void onDestroy()
    {
        // TODO Auto-generated method stub
        super.onDestroy();
    }
}


  • 답변 # 1

    AlarmManager.setRepeating이 다른 Android 버전에서 제대로 작동하지 않습니다.

    setExact를 시도하십시오. 반복되지는 않지만 아래 언급 된대로 반복 기능을 수행 할 수 있습니다.

    MyReceiver 업데이트

    AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent alarmIntent = new Intent(this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
    manager.setExact(AlarmManager.RTC_WAKEUP, (864000 + System.currentTimeMillis()),pendingIntent);
    
    

    여기서는 864000 + System.currentTimeMillis ()를 사용하여 nextAlarmTime을 계산하여 알람을 다시 예약합니다.

  • 이전 java - 저지 + 스파크 javaxwsrscoreUriBuilderuri
  • 다음 java - Couchebase는 Couchebase에서 글로벌 보조 인덱스를 만듭니다