Alarm receive don't work

前端 未结 2 1962
闹比i
闹比i 2021-01-26 06:08

I\'m making an alarm receiver, but the alarm is not triggered.

This is my code :

Start alarm in MainActivity :

private void setupAlarm(){
    In         


        
相关标签:
2条回答
  • 2021-01-26 06:26

    Try To Use THis. It will Work for you

    private void setupAlarm(){
    Intent intent = new Intent(this, com.logdata.AlarmReceiver.class);
    PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, 0);       
    AlarmManager manager = (AlarmManager)  getSystemService(Context.ALARM_SERVICE);
     manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime() + 1, 1000, pIntent);
    
    Log.e("setupAlarm", "Setup alarm complete");
    

    }

    0 讨论(0)
  • 2021-01-26 06:28

    Your Intent is used to explicitly start the AlarmReceiver class which is a BroadcastReceiver. Hence, you need to use getBroadcast(), not getService(), to create the PendingIntent object.

    Replace

    PendingIntent pIntent = PendingIntent.getService(this, 0, intent, 0);       
    

    with

    PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, 0);  
    

    Try this. This will work.

    0 讨论(0)
提交回复
热议问题