gpt4 book ai didi

android - 应用程序关闭时警报管理器不工作

转载 作者:太空狗 更新时间:2023-10-29 14:41:31 29 4
gpt4 key购买 nike

我已经坚持了好几天了。我希望我的闹钟管理器每 15 分钟触发一次,即使在应用程序关闭时也是如此,但是当应用程序关闭时它不起作用。它在应用程序打开时有效。

在我的 list 文件中我有:

  <!-- Used to consume the alarm manager alerts when app clsoed -->
<receiver
android:name="biz.customName.pkg.AlarmReceiver"
android:enabled="true">
<intent-filter>
<action android:name="biz.customName.pkg.msg"/>
</intent-filter>
</receiver>

我的 BroadcastReceiver 类(AlarmReceiver)

public class AlarmReceiver extends BroadcastReceiver
{

// Alarm manager used to run install when app is closed
AlarmManager alarmManager;


// Called when alarm received
@Override
public void onReceive(Context context, Intent intent)
{

// Enable alarm
setupAlarm(context);

// Perform background task
}


// Setup alarm
public void setupAlarm(Context context)
{
// Setup reciever for alarm
// context.registerReceiver(this, new IntentFilter("biz.customName.pkg.msg"));

// Setup pending intent
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(Loader.filterName), PendingIntent.FLAG_UPDATE_CURRENT);

// Setup alarm
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

final long triggerTime = System.currentTimeMillis() + 900 * 1000;

// Newest OS
if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 23)
{
alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent);
}
}
}

在 main 中,我首先调用 setup alarm 来启动闹钟,然后每次调用我的广播接收器中的 onReceive 时,我都会重置闹钟。

当应用程序关闭时它不起作用,我做错了什么?

最佳答案

将此添加到您的 AndroidManifest.xml 中

<service
android:name=".MyService"
android:enabled="true"
android:exported="true" />

<receiver
android:name=".MyAlarmReceiver"
android:enabled="true"
android:exported="true" />

MyAlarmReceiver.java

public class MyAlarmReceiver extends BroadcastReceiver {
Context context;

public MyAlarmReceiver() {
}

@Override
public void onReceive(Context context, Intent intent) {
intent = new Intent(context, MyService.class);
context.startService(intent);
}

}

MyService.java

public class MyService extends Service {

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

YourTask();

return Service.START_STICKY;
}

private void YourTask(){
// call api in background

// send push notification

//etc...
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}


}

MainActivity.java

public class MainActivity extends AppCompatActivity {
PendingIntent pendingIntent;
AlarmManager alarmManager;
Intent alarmIntent;

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

private void AutoUpdateDataInBackground() {
// Retrieve a PendingIntent that will perform a broadcast

alarmIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

long interval = 15 * 60 * 1000;

// Repeating on every 15 minutes interval

Calendar calendar = Calendar.getInstance();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),interval, pendingIntent);
}

}

关于android - 应用程序关闭时警报管理器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47425668/

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