BroadcastReceiver 没有收到意图 Intent.ACTION_LOCALE_CHANGED
BroadcastReceiver does not receive intent Intent.ACTION_LOCALE_CHANGED
我希望我的应用程序能够在用户更改语言环境时随时知道。所以在我的 Application
class 中,我创建了一个 receiver
来监听系统意图 ACTION_LOCALE_CHANGED
:
public final class MyApp extends Application {
private BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override public void onReceive(Context context, Intent intent) {
String locale = Locale.getDefault().getCountry();
Toast.makeText(context, "LOCALE CHANGED to " + locale, Toast.LENGTH_LONG).show();
}
};
@Override public void onCreate() {
IntentFilter filter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED);
LocalBroadcastManager.getInstance(this).registerReceiver(myReceiver, filter);
}
}
当我按主页并转到设置应用程序以更改我的语言环境时,没有显示 Toast。在 onReceive
内设置断点表明它永远不会被命中。
Intent.ACTION_LOCALE_CHANGED
不是本地广播,所以用LocalBroadcastManager
注册就不行了。 LocalBroadcastManager
用于您应用内使用的广播。
public class MyApp extends Application {
private BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String locale = Locale.getDefault().getCountry();
Toast.makeText(context, "LOCALE CHANGED to " + locale,
Toast.LENGTH_LONG).show();
}
};
@Override
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED);
registerReceiver(myReceiver, filter);
}
}
为什么要在应用程序中使用 BroadcastReceiver class。我的建议是为 BroadcastRecevier 设置一个单独的 class。
public class LocaleChangedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction (). compareTo (Intent.ACTION_LOCALE_CHANGED) == 0)
{
Log.v("LocaleChangedRecevier", "received ACTION_LOCALE_CHANGED");
}
}
}
并在清单文件中注册您的 Brodcast 接收器。
<receiver
android:name=".LocaleChangedReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.LOCALE_CHANGED" />
</intent-filter>
</receiver>
我希望我的应用程序能够在用户更改语言环境时随时知道。所以在我的 Application
class 中,我创建了一个 receiver
来监听系统意图 ACTION_LOCALE_CHANGED
:
public final class MyApp extends Application {
private BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override public void onReceive(Context context, Intent intent) {
String locale = Locale.getDefault().getCountry();
Toast.makeText(context, "LOCALE CHANGED to " + locale, Toast.LENGTH_LONG).show();
}
};
@Override public void onCreate() {
IntentFilter filter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED);
LocalBroadcastManager.getInstance(this).registerReceiver(myReceiver, filter);
}
}
当我按主页并转到设置应用程序以更改我的语言环境时,没有显示 Toast。在 onReceive
内设置断点表明它永远不会被命中。
Intent.ACTION_LOCALE_CHANGED
不是本地广播,所以用LocalBroadcastManager
注册就不行了。 LocalBroadcastManager
用于您应用内使用的广播。
public class MyApp extends Application {
private BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String locale = Locale.getDefault().getCountry();
Toast.makeText(context, "LOCALE CHANGED to " + locale,
Toast.LENGTH_LONG).show();
}
};
@Override
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED);
registerReceiver(myReceiver, filter);
}
}
为什么要在应用程序中使用 BroadcastReceiver class。我的建议是为 BroadcastRecevier 设置一个单独的 class。
public class LocaleChangedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction (). compareTo (Intent.ACTION_LOCALE_CHANGED) == 0)
{
Log.v("LocaleChangedRecevier", "received ACTION_LOCALE_CHANGED");
}
}
}
并在清单文件中注册您的 Brodcast 接收器。
<receiver
android:name=".LocaleChangedReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.LOCALE_CHANGED" />
</intent-filter>
</receiver>