首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

Android BroadcastReceiver 中使用 AlarmManager:保存 Context 獲取系統服務

     編輯:程式設計綜合問答

痛點:BroadcastReceiver 無 getSystemService() 方法,直接調用編譯報錯。

解決方案:onReceive 參數 Context 保存為成員變量

public class MyReceiver extends BroadcastReceiver {
    private Context context; // 成員變量持有
    
    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context; // 保存
        // 立即使用或啟動 Service/線程中使用
        setAlarm(context);
    }
    
    private void setAlarm(Context ctx) {
        AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
        // ...
    }
}

注意事項

  • Context 生命周期:onReceive 的 Context 是 ReceiverRestrictedContext,**僅在 onReceive 存活期間有效**。
  • 異步操作:若在 onReceive 後異步使用(如線程、Handler),**必須傳遞 Application Context**:context.getApplicationContext(),避免內存洩漏。
  • 長時間任務:onReceive 執行超過 10 秒會 ANR,鬧鐘設置邏輯應極快,或啟動 IntentService/JobIntentService 異步處理。

最佳實踐模板

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // 1. 獲取 Application Context 長期持有
        Context appCtx = context.getApplicationContext();
        
        // 2. 啟動 Service 異步處理鬧鐘邏輯
        Intent serviceIntent = new Intent(appCtx, AlarmService.class);
        serviceIntent.putExtra("action", intent.getAction());
        ContextCompat.startForegroundService(appCtx, serviceIntent);
    }
}

// AlarmService extends IntentService { ... }

牢記:BroadcastReceiver 靠傳入 Context 訪問系統服務,異步場景必轉 Application Context。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved