【发布时间】:2019-02-25 15:30:08
【问题描述】:
如何从 onReceive 调用的方法中检索 context?
这是我要完成的示例:
@Override
public void onReceive(Context context, Intent intent) {
...
...
if(...) {
callMethodOne();
callMethodTwo();
} else if (...) {
callMethodOne();
}
...
}
private void callMethodOne() {
// Cant use getApplicationContext
SharedPreferences getPrefs =
PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
}
private void callMethodTwo() {
// Cant use getSystemService
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
如您所见,由于方法被多次/方式调用,将所有代码移动到 onReceive 内部最终会非常重复且效率极低。
非常感谢任何帮助!
谢谢。
【问题讨论】:
标签: android broadcastreceiver android-context android-broadcastreceiver