Update AlarmReceiver.java

main
mxvwfs5gq 2 months ago
parent 19e602a656
commit 8d46cf1709

@ -19,12 +19,187 @@ package net.micode.notes.ui;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
/**
* 广 - 广
*
*
* 1.
* 2. 广
* 3.
* 4. Android 8.0+
* 5.
*/
public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG = "AlarmReceiver";
private static final String WAKE_LOCK_TAG = "Notes:AlarmWakeLock";
private static final long WAKE_LOCK_TIMEOUT = 30 * 1000; // 30秒超时
/**
* 广 - 广
*
* @param context
* @param intent 广
*/
@Override
public void onReceive(Context context, Intent intent) {
intent.setClass(context, AlarmAlertActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
// 日志记录接收到的广播
logBroadcastReceived(intent);
// 验证广播是否有效
if (!isValidAlarmIntent(intent)) {
Log.w(TAG, "收到无效的闹钟广播,忽略处理");
return;
}
// 获取唤醒锁确保设备保持活动状态
WakeLock wakeLock = acquireWakeLock(context);
try {
// 启动闹钟提醒界面
startAlarmAlertActivity(context, intent);
} catch (Exception e) {
// 异常处理
Log.e(TAG, "启动闹钟提醒活动失败", e);
} finally {
// 确保释放唤醒锁
releaseWakeLock(wakeLock);
}
}
/**
* 广
*
* @param intent
*/
private void logBroadcastReceived(Intent intent) {
if (intent == null) {
Log.i(TAG, "收到空意图的闹钟广播");
return;
}
StringBuilder logMsg = new StringBuilder("收到闹钟广播: ");
logMsg.append("Action=").append(intent.getAction());
if (intent.getData() != null) {
logMsg.append(", URI=").append(intent.getData().toString());
}
if (intent.getExtras() != null) {
logMsg.append(", Extras=").append(intent.getExtras().toString());
}
Log.d(TAG, logMsg.toString());
}
/**
* 广
*
* @param intent
* @return true
*/
private boolean isValidAlarmIntent(Intent intent) {
return intent != null &&
intent.getData() != null &&
intent.getData().getScheme() != null &&
intent.getData().getScheme().equals("content");
}
/**
*
*
* @param context
* @return
*/
private WakeLock acquireWakeLock(Context context) {
try {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (pm == null) {
Log.w(TAG, "无法获取电源服务");
return null;
}
WakeLock wakeLock = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP,
WAKE_LOCK_TAG
);
wakeLock.setReferenceCounted(false);
wakeLock.acquire(WAKE_LOCK_TIMEOUT);
Log.d(TAG, "成功获取唤醒锁");
return wakeLock;
} catch (Exception e) {
Log.e(TAG, "获取唤醒锁时出错", e);
return null;
}
}
/**
*
*
* @param wakeLock
*/
private void releaseWakeLock(WakeLock wakeLock) {
if (wakeLock == null) {
return;
}
try {
if (wakeLock.isHeld()) {
wakeLock.release();
Log.d(TAG, "唤醒锁已释放");
}
} catch (Exception e) {
Log.e(TAG, "释放唤醒锁时出错", e);
}
}
/**
*
*
* @param context
* @param originalIntent 广
*/
private void startAlarmAlertActivity(Context context, Intent originalIntent) {
// 创建启动AlarmAlertActivity的意图
Intent alertIntent = new Intent(context, AlarmAlertActivity.class);
// 传递原始意图的数据
alertIntent.setData(originalIntent.getData());
// 添加必要的标志
alertIntent.addFlags(
Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
// 复制额外的参数
if (originalIntent.getExtras() != null) {
alertIntent.putExtras(originalIntent.getExtras());
}
// 启动活动
try {
Log.d(TAG, "正在启动闹钟提醒活动");
context.startActivity(alertIntent);
} catch (SecurityException e) {
Log.e(TAG, "启动活动权限不足", e);
} catch (Exception e) {
Log.e(TAG, "启动活动失败", e);
}
}
/**
* -
*/
@Override
public void finalize() {
// 添加资源清理逻辑
Log.v(TAG, "闹钟接收器实例被回收");
}
}
}
Loading…
Cancel
Save