Android 未决意图和警报管理器

Android 未决意图和警报管理器,android,android-intent,alarmmanager,android-pendingintent,Android,Android Intent,Alarmmanager,Android Pendingintent,我最近一直在做一个安卓应用。。 在其中,我使用了Pending Intent和Alarm Manager。 我需要有多个待定的意图,所以我使用FLAG_ONE_SHOT。报警管理器将按上述间隔发送广播。此外,我还使用intent的setAction()方法并将currentTimeMillis()作为参数传递。我有相应的广播接收器。问题是,一旦应用程序关闭或从“最近”托盘中删除,广播接收器就不会运行。 代码如下: 设置报警: private void setupAlarm(int seconds

我最近一直在做一个安卓应用。。 在其中,我使用了Pending Intent和Alarm Manager。 我需要有多个待定的意图,所以我使用FLAG_ONE_SHOT。报警管理器将按上述间隔发送广播。此外,我还使用intent的setAction()方法并将currentTimeMillis()作为参数传递。我有相应的广播接收器。问题是,一旦应用程序关闭或从“最近”托盘中删除,广播接收器就不会运行。 代码如下:

  • 设置报警:

    private void setupAlarm(int seconds) {
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent intent = new Intent(getBaseContext(), OnAlarmReceive.class);
    //PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
    intent.setAction(Long.toString(System.currentTimeMillis()));
    intent.putExtra("id", ID);
    
    PendingIntent pendingIntent = PendingIntent.getBroadcast(ChatActivity.this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    
    Log.e(TAG, "Setup the Alarm");
    
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.SECOND, seconds);
    
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);}
    
  • 广播接收机

    public void onReceive(Context context, Intent intent) {
    
    String id = intent.getStringExtra("id");
    Log.e(TAG,"On the verge of deleting the message with id: "+id);
    SQLiteDatabase database = context.openOrCreateDatabase("/sdcard/userlists.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
    database.execSQL("DELETE FROM " + "MESSAGE" + " WHERE " + "id" + "= '" + id + "'");
    
    broadcaster = LocalBroadcastManager.getInstance(context);
    intent = new Intent(COPA_RESULT);
    broadcaster.sendBroadcast(intent);}
    
  • Manifest.xml

    <receiver android:name=".OnAlarmReceive" android:enabled="true" android:exported="true"/>
    
    
    
  • 请帮帮我。我需要广播公司来做这项工作,即使应用程序关闭。

    这是一个进程生命周期错误,当应用程序进入后台回收内存时,系统可能会杀死进程。

    无论应用程序是否处于活动状态,您都需要计划接收作业

    来自流程和应用程序生命周期的官员

    进程生命周期错误的一个常见示例是广播接收器 当线程在其 方法,然后从 功能。一旦返回,系统将考虑广播接收器 不再处于活动状态,因此不再需要其托管进程 (除非其他应用程序组件在其中处于活动状态)。那么这个系统呢, 可以随时终止进程以回收内存,在这样做时, 它终止进程中运行的派生线程。解决方案 解决此问题的方法通常是从 BroadcastReceiver,因此系统知道仍有活动工作 在这个过程中被完成


    您可以按照以下步骤完成您的要求

    使用
    服务
    。!那么,如果不使用服务,我就不能接收广播?如果是,你能提供步骤吗?你理解了吗?是的。。。非常抱歉,回复太晚了。。。但是非常感谢你@KishoreJethava你能详细说明一下吗?