应用关闭时,警报管理器无法正常工

时间:2017-11-22 01:58:12

标签: android broadcastreceiver alarmmanager

我已经被困在这几天了。 即使应用程序关闭,我希望我的闹钟管理器每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);
    }
 }
}

在主要的情况下,我打电话给设置警报以使警报初始化,然后每当我的广播接收器内的onReceive被调用时,我重置警报。

在应用程序关闭时它无法正常工作,我做错了什么?

3 个答案:

答案 0 :(得分:0)

由于您错过了服务的代码,警报无法在后台运行。 您必须创建一个服务,负责在后台触发警报。

public class AlarmService extends Service {
AlarmReceiver alarm = new AlarmReceiver();
public void onCreate()
{
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    alarm.setupAlarm(this);
    return START_STICKY;
}

@Override
public void onStart(Intent intent, int startId)
{
    alarm.setupAlarm(this);
}

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

从MainActivity启动此服务,也不要忘记在mainfest文件中添加条目。

答案 1 :(得分:0)

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

}

答案 2 :(得分:0)

顺便说一句:不会在锁定屏幕和启用节能模式的情况下调用AlarmManager