简体   繁体   English

如何从Android中的Broadcast ONRECEIVE更新UI?

[英]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. 在onReceive上,我可以跟踪每个项目的每次点击。 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. 我知道我可以从方法onUpdate更新视图,但是在onReceive上我不能。

I don't understand how to implement the intent for onUpdate to make this happen. 我不明白如何实现onUpdate的意图来实现这一目标。

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. 尝试获取存在按钮的Activity实例。 Say it is mActivty . 说这是mActivty Now using this variable inside onUpdate() method do 现在在onUpdate()方法中使用此变量

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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