0

I would create an alarm clock, I wrote this code but return this error:

2019-02-05 10:58:13.902 2663-10077/com.google.android.gms E/ChromeSync: [Sync,SyncIntentOperation] Error handling the intent: Intent { act=android.intent.action.PACKAGE_ADDED dat=package:com.example.iacopo.alarmgroup flg=0x4000010 cmp=com.google.android.gms/.chimera.GmsIntentOperationService (has extras) }.

How can I fix it? Thanks. And this code doesn't create an icon of alarm in the notification panel, near the clock.

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(System.currentTimeMillis());
        cal.clear();
        cal.set(2018,1,5,10,0);

        AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
        this.startService(intent);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
        alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
    }
}

and the receiver

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("test","ok");
    }
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
kanodates
  • 1
  • 1

2 Answers2

0
cal.set(2018,1,5,10,0);

The calendar takes earlier than the current time, so your alarm clock is unlikely to be triggered. Change the time correctly and try again.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Susan
  • 1
  • 1
0

This is a basic snippet from developer.android.com. You can change date and time as per your needs and pass the calendar instance to Alarm Manager intent.

// Example: Wake up the device to fire a one-time (non-repeating) alarm in one minute:

private AlarmManager alarmMgr;
private PendingIntent alarmIntent;

alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
    SystemClock.elapsedRealtime() +
    60 * 1000, alarmIntent);

// Example: Set the alarm to start at approximately 2:00 p.m. say
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);

// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
    AlarmManager.INTERVAL_DAY, alarmIntent);
Ravi Kabra
  • 1,284
  • 1
  • 11
  • 17