未决意图和警报管理器
Pending Intent and Alarm Manager
我最近一直在制作一个 Android 应用程序..
在其中我使用了 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"/>
请帮助我。我需要 Broadcaster 来完成这项工作,即使应用程序已关闭。
这是 进程生命周期 错误,其中 系统可能会在应用进入后台回收内存时终止进程
您需要安排 JobService 以 接收工作 无论申请是否有效
来自进程和应用程序生命周期的官方document
A common example of a process life-cycle bug is a BroadcastReceiver
that starts a thread when it receives an Intent in its
BroadcastReceiver.onReceive() method, and then returns from the
function. Once it returns, the system considers the BroadcastReceiver
to be no longer active, and thus, its hosting process no longer needed
(unless other application components are active in it). So, the system
may kill the process at any time to reclaim memory, and in doing so,
it terminates the spawned thread running in the process. The solution
to this problem is typically to schedule a JobService from the
BroadcastReceiver, so the system knows that there is still active work
being done in the process.
这是example您可以按照以下步骤完成您的要求
我最近一直在制作一个 Android 应用程序.. 在其中我使用了 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"/>
请帮助我。我需要 Broadcaster 来完成这项工作,即使应用程序已关闭。
这是 进程生命周期 错误,其中 系统可能会在应用进入后台回收内存时终止进程
您需要安排 JobService 以 接收工作 无论申请是否有效
来自进程和应用程序生命周期的官方document
A common example of a process life-cycle bug is a BroadcastReceiver that starts a thread when it receives an Intent in its BroadcastReceiver.onReceive() method, and then returns from the function. Once it returns, the system considers the BroadcastReceiver to be no longer active, and thus, its hosting process no longer needed (unless other application components are active in it). So, the system may kill the process at any time to reclaim memory, and in doing so, it terminates the spawned thread running in the process. The solution to this problem is typically to schedule a JobService from the BroadcastReceiver, so the system knows that there is still active work being done in the process.
这是example您可以按照以下步骤完成您的要求