下班后Alarmmanager不会开火

时间:2019-04-05 08:28:31

标签: android service alarmmanager

一个小时后,我尝试用Alarmmanager进行消防服务,但是它不起作用。

我也尝试了alarmmanager.setAlarmClock和setExactAndAllowWhileIdle,但是在Android 8.0上没有显示。

维护中

    alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

intent.setAction(PlayerService.ACTION_START_FOREGROUND_SERVICE);

    pendingIntent1 = PendingIntent.getService(getApplicationContext(), 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    pendingIntent2 = PendingIntent.getService(getApplicationContext(), 2, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    pendingIntent3 = PendingIntent.getService(getApplicationContext(), 3, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    pendingIntent4 = PendingIntent.getService(getApplicationContext(), 4, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    pendingIntent5 = PendingIntent.getService(getApplicationContext(), 5, intent, PendingIntent.FLAG_UPDATE_CURRENT);

long p1, p2, p3, p4, p5;

p1=System.currentTimeMillis()+ 60 * 60 * 1000L;
p2=System.currentTimeMillis()+ 105 * 60 * 1000L;
p3=System.currentTimeMillis()+ 240 * 60 * 1000L;
p4=System.currentTimeMillis()+ 310 * 60 * 1000L;
p5=System.currentTimeMillis()+ 400 * 60 * 1000L;

alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, p1, pendingIntent1);
    alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, p2, pendingIntent2);
    alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, p3, pendingIntent3);
    alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, p4, pendingIntent4);
    alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, p5, pendingIntent5);

清单:-服务android:name =“。PlayerService”


PLAYERSERVICE

public class PlayerService extends Service  {
    MediaPlayer mp;

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

    @Override
    public void onCreate() {
        super.onCreate();
        mp = new MediaPlayer();
    }

     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {

    if(intent != null)
    {
        String action = intent.getAction();

        switch (action)
        {
            case ACTION_START_FOREGROUND_SERVICE:
                startThisForegroundService(intent);
                Toast.makeText(getApplicationContext(), "Foreground service is started.", Toast.LENGTH_LONG).show();
                break;
            case ACTION_STOP_FOREGROUND_SERVICE:
                stopThisForegroundService();
                Toast.makeText(getApplicationContext(), "Foreground service is stopped.", Toast.LENGTH_LONG).show();
                break;
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }

   private void startThisForegroundService(Intent intent){
      mp-->setDatasource and prepare and start! 
   }

    private void stopThisForegroundService()
    {
        Log.d(TAG_FOREGROUND_SERVICE, "Stop foreground service.");
        stopForeground(true);
        stopSelf();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mp.stop();
        mp.release();
        Toast.makeText(this, "Service stopped...", Toast.LENGTH_SHORT).show();
    }
}

5分钟后-有效。 1小时后-没有任何反应 5小时后-同样,什么也没发生

0 个答案:

没有答案