简体   繁体   中英

How to update UI from Broadcast ONRECEIVE in Android?

I have read many tutorials, but I can't figure out how to do it :(

I have the following code:

@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");
    }

On onReceive, I can track each click for each item. However, I want to change the button image when it is clicked(enable/disable)

I know that I can update my view from method onUpdate, but on onReceive I cannot.

I don't understand how to implement the intent for onUpdate to make this happen.

I need help please :( Bad logic I think :(

Tell me about the class type in which these two methods are written?

Meanwhile, you can try this

try to get the instance of the Activity on which the buttons are present. Say it is mActivty . Now using this variable inside onUpdate() method do

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM