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

本教程将介绍应用程序关闭时报警管理器不工作的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。

问题描述

我已经被困在这个问题上好几天了。
我希望我的闹钟管理器每15分钟触发一次,即使在应用程序关闭时也是如此,但当应用程序关闭时它不起作用。但它可以在应用程序打开时运行。

在我的清单文件中:

  <!-- 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);
}

}

好了关于应用程序关闭时报警管理器不工作的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。

0
没有账号?注册  忘记密码?