gpt4 book ai didi

java - 如何在 Android 中从 Broadcast ONRECEIVE 更新 UI?

转载 作者:行者123 更新时间:2023-12-01 08:56:12 26 4
gpt4 key购买 nike

我读了很多教程,但我不知道该怎么做:(

我有以下代码:

@Override
public void onUpdate(Context context, SlookCocktailManager cocktailBarManager, int[] cocktailIds) {

// create RemoteViews
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.feeds_panel_layout);

rv.setRemoteAdapter(R.id.btn_camera, intent);
rv.setEmptyView(R.id.widgetlist, R.id.emptylist);

Intent itemClickIntent = new Intent(context, CocktailFeedsProvider.class);
itemClickIntent.setAction(Constants.COCKTAIL_LIST_ADAPTER_CLICK_ACTION);

rv.setOnClickPendingIntent(R.id.btn_internet, getPendingSelfIntent(context, MyOnClick1, cocktailBarManager));
rv.setOnClickPendingIntent(R.id.btn_camera, getPendingSelfIntent(context, MyOnClick2, cocktailBarManager));
rv.setOnClickPendingIntent(R.id.btn_phone, getPendingSelfIntent(context, MyOnClick3, cocktailBarManager));
// update cocktail
for (int i = 0; i < cocktailIds.length; i++) {
cocktailBarManager.updateCocktail(cocktailIds[i], rv);
}
}

protected PendingIntent getPendingSelfIntent(Context context, String action, SlookCocktailManager cocktailBarManager) {
Intent intent = new Intent(context, getClass());
intent.setAction(action);

return PendingIntent.getBroadcast(context, 0, intent, 0);
}


@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);

if (MyOnClick1.equals(intent.getAction())) {
// your onClick action is here
Toast.makeText(context, "Button1", Toast.LENGTH_SHORT).show();
Log.d("vuta", "Clicked button1");
SharedPreferences prefs = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();

if (prefs.getBoolean("istorch", false)) {
context.stopService(new Intent(context, Flashlight.class));
Log.d("vuta", "Flashlight is running= stop");
editor.putBoolean("istorch", false).commit();
} else {
context.startService(new Intent(context, Flashlight.class));
Log.d("vuta", "flashlight is no running = start");
editor.putBoolean("istorch", true).commit();
}

} else if (MyOnClick2.equals(intent.getAction())) {
Toast.makeText(context, "Button2", Toast.LENGTH_SHORT).show();
Log.d("vuta", "Clicked button2");

WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if(wifi.isWifiEnabled()) {

//rv.setImageViewResource(R.id.btn_camera, R.drawable.wifi_inactive);
wifi.setWifiEnabled(false);
Log.d("vuta", "wifi is enabled");
} else {
Log.d("vuta", "wifi is disabled");
// rv.setImageViewResource(R.id.btn_camera, R.drawable.wifi_active);
wifi.setWifiEnabled(true);
}

} else if (MyOnClick3.equals(intent.getAction())) {
Toast.makeText(context, "Button3", Toast.LENGTH_SHORT).show();
Log.d("vuta", "Clicked button3");
}

在 onReceive 上,我可以跟踪每个项目的每次点击。但是,我想在单击时更改按钮图像(启用/禁用)

我知道我可以通过 onUpdate 方法更新我的 View ,但在 onReceive 上我不能。

我不明白如何实现 onUpdate 的 Intent 来实现这一点。

我需要帮助:(我认为逻辑不好:(

最佳答案

告诉我这两个方法是用什么类类型编写的?

同时,您可以尝试一下

尝试获取按钮所在 Activity 的实例。假设它是mActivty。现在在 onUpdate() 方法中使用这个变量

mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
// update you button here
}
});

关于java - 如何在 Android 中从 Broadcast ONRECEIVE 更新 UI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42024598/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com