gpt4 book ai didi

java - AlarmManager 在应用程序关闭时不起作用

转载 作者:行者123 更新时间:2023-12-02 00:06:07 28 4
gpt4 key购买 nike

public class background_alarm extends BroadcastReceiver {

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

//CODE EVERY X MINUTES
} }

主要 Activity

public class MainActivity extends AppCompatActivity {



AlarmManager alarmManager;
PendingIntent pendingIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

Intent intent = new Intent(this, background_alarm.class);
intent.putExtra("UserNumber", UserLogged.getNumeroTelefono());

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);


Long intervalMillis = 1L * 60L * 1000L; //IT'S ONLY FOR TEST 1 MINUTES
Long triggerAtMillis = System.currentTimeMillis() + intervalMillis;

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtMillis, intervalMillis, pendingIntent);

}

当应用程序关闭时,它不再起作用......有一个解决方案可以运行吗?注意:当应用程序运行时此工作

最佳答案

您的设备可能正在进入打瞌睡模式。在后台警报类中使用 setAndAllowWhileIdle() 或 setExactAndAllowWhileIdle() 方法 https://developer.android.com/training/monitoring-device-state/doze-standby

关于java - AlarmManager 在应用程序关闭时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58155251/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com