本地广播管理器使用主 Activity 未收到的警报
Local Broadcast Manager using an alarm not received by main Activity
我已经实现了一个警报管理器,它运行良好,每 15 分钟触发一次。现在我想使用闹钟每 15 分钟调用一次 MainActivity
方法。所以我使用本地广播到 MainActivity
(知道这是我 [=42] 上的当前 activity 运行 =]) 传送启动方法的命令。但问题是 MainActivity
没有在 onReceive 方法上接收数据。
报警
@Override
public void onReceive(Context context, Intent intent) {
cont = context;
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
String url = intent.getExtras().getString("url");
Log.w(TAG, "Service " + url);
new GetJSON_val(){
@Override
protected String[] doInBackground(String... url) {
Some Code...
@Override
protected void onPostExecute(String[] result) {
Some Code...
// I send the broadcast to main Activity
Log.e("sender", "Broadcasting message");
Intent intent = new Intent("custom-event-name");
intent.putExtra("message", "This is my message!");
LocalBroadcastManager.getInstance(cont).sendBroadcast(intent);
}
}
}
}.execute(url);
// End of alarm code
wl.release();
}
主要Activity
@Override
public void onCreate(Bundle savedInstanceState) {
...
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("custom-event-name"));
...
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("message");
Log.e("receiver", "Got message: " + message);
}
};
Local Broadcaster 是在 postExecute
方法中的 Alarm class 中创建的。
我认为问题是您在 onReceive()
中执行长 运行 任务并访问相同的 context
you should never perform long-running operations in it (there is a timeout of 10 seconds that the system allows before considering the receiver to be blocked and a candidate to be killed).
解决方案
要么使用 goAsync() .
或者对后台任务使用IntentService并在任务完成后发送广播。
我已经实现了一个警报管理器,它运行良好,每 15 分钟触发一次。现在我想使用闹钟每 15 分钟调用一次 MainActivity
方法。所以我使用本地广播到 MainActivity
(知道这是我 [=42] 上的当前 activity 运行 =]) 传送启动方法的命令。但问题是 MainActivity
没有在 onReceive 方法上接收数据。
报警
@Override
public void onReceive(Context context, Intent intent) {
cont = context;
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
String url = intent.getExtras().getString("url");
Log.w(TAG, "Service " + url);
new GetJSON_val(){
@Override
protected String[] doInBackground(String... url) {
Some Code...
@Override
protected void onPostExecute(String[] result) {
Some Code...
// I send the broadcast to main Activity
Log.e("sender", "Broadcasting message");
Intent intent = new Intent("custom-event-name");
intent.putExtra("message", "This is my message!");
LocalBroadcastManager.getInstance(cont).sendBroadcast(intent);
}
}
}
}.execute(url);
// End of alarm code
wl.release();
}
主要Activity
@Override
public void onCreate(Bundle savedInstanceState) {
...
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("custom-event-name"));
...
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("message");
Log.e("receiver", "Got message: " + message);
}
};
Local Broadcaster 是在 postExecute
方法中的 Alarm class 中创建的。
我认为问题是您在 onReceive()
中执行长 运行 任务并访问相同的 context
you should never perform long-running operations in it (there is a timeout of 10 seconds that the system allows before considering the receiver to be blocked and a candidate to be killed).
解决方案
要么使用 goAsync() .
或者对后台任务使用IntentService并在任务完成后发送广播。