使用 activity 上下文和使用应用程序上下文发送广播之间有区别吗
is there a difference between using activity context and using application context to sendBroadcast
sendBroadcast 可以在 activity context 或 application context 上调用,有什么区别吗?
另一个问题,如果上下文已经是应用程序上下文,我想 context.getApplicationContext() 只会 return 本身,对吗?
一个场景可能是将 activity 上下文传递给接收者对象,该对象稍后使用此上下文绑定或午餐其他服务。该接收器在几个不同的活动中本地实例化。上下文在接收器内部引用如下,
如果没有区别,也许它可以将 applicationConext 传递给接收者,
或者更好的方法是在
中获取传入的上下文
onReceive(Context context, Intent intent),
然后 context.getApplicationContext 在那里?
// inside the receiver it will the following with the context:
mContext.bindService(new Intent(mContext, OtherService.class), mInitServiceConnection, Context.BIND_AUTO_CREATE);
mContext.unbindService(mInitServiceConnection);
Intent newStartIntent = new Intent(mContext, InitService.class);
mContext.startService(newStartIntent);
接收者是这样的:
class LocalBroadcastReceiver extends BroadcastReceiver {
private final Context mContext;
public LocalBroadcastReceiver(@NonNull Context context) {
mAppContext = context;
}
@Override
public void onReceive(Context context, Intent intent) {
// could it here use the context to get application context, like:
mContext = context.getApplicationContext()
//then do something like:
Intent newStartIntent = new Intent(mContext, InitService.class);
mContext.startService(startMailAccountInitIntent);
}
}
我的情况是使用上下文从 onReceive() 获取 applicationContext 有效,
onReceive(Context context, Intent intent) {
var appContext = context.applicationContext
...
mContext.bindService(new Intent(appContext, OtherService.class), mInitServiceConnection, Context.BIND_AUTO_CREATE);
}
不能使用将获取
的上下文
android.content.ReceiverCallNotAllowedException: BroadcastReceiver components are not allowed to bind to services
sendBroadcast 可以在 activity context 或 application context 上调用,有什么区别吗?
另一个问题,如果上下文已经是应用程序上下文,我想 context.getApplicationContext() 只会 return 本身,对吗?
一个场景可能是将 activity 上下文传递给接收者对象,该对象稍后使用此上下文绑定或午餐其他服务。该接收器在几个不同的活动中本地实例化。上下文在接收器内部引用如下,
如果没有区别,也许它可以将 applicationConext 传递给接收者,
或者更好的方法是在
中获取传入的上下文onReceive(Context context, Intent intent),
然后 context.getApplicationContext 在那里?
// inside the receiver it will the following with the context:
mContext.bindService(new Intent(mContext, OtherService.class), mInitServiceConnection, Context.BIND_AUTO_CREATE);
mContext.unbindService(mInitServiceConnection);
Intent newStartIntent = new Intent(mContext, InitService.class);
mContext.startService(newStartIntent);
接收者是这样的:
class LocalBroadcastReceiver extends BroadcastReceiver {
private final Context mContext;
public LocalBroadcastReceiver(@NonNull Context context) {
mAppContext = context;
}
@Override
public void onReceive(Context context, Intent intent) {
// could it here use the context to get application context, like:
mContext = context.getApplicationContext()
//then do something like:
Intent newStartIntent = new Intent(mContext, InitService.class);
mContext.startService(startMailAccountInitIntent);
}
}
我的情况是使用上下文从 onReceive() 获取 applicationContext 有效,
onReceive(Context context, Intent intent) {
var appContext = context.applicationContext
...
mContext.bindService(new Intent(appContext, OtherService.class), mInitServiceConnection, Context.BIND_AUTO_CREATE);
}
不能使用将获取
的上下文 android.content.ReceiverCallNotAllowedException: BroadcastReceiver components are not allowed to bind to services