![](/img/trans.png)
[英]How to update the UI with BroadcastReceiver onReceive in Android?
[英]How to update UI from Broadcast ONRECEIVE in Android?
我讀過很多教程,但是我不知道該怎么做:(
我有以下代碼:
@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更新視圖,但是在onReceive上我不能。
我不明白如何實現onUpdate的意圖來實現這一目標。
我需要幫助:(我認為邏輯不好:(
告訴我有關這兩種方法編寫的類類型的信息嗎?
同時,您可以嘗試
嘗試獲取存在按鈕的Activity實例。 說這是mActivty 。 現在在onUpdate()方法中使用此變量
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
// update you button here
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.