Engaged in creating a widget. I do something like pressing a button. I use Broadcast. It is assumed that after clicking on a certain button (in this example there are 4 of them), some text variable is saved in the extra and a broadcast message is assigned. Depending on this, the messages in StringBuilder write down some extras and pass them to the widget. The problem is that StringBuilder does not write these extras in order. For example, there are 4 extras - 1,2,3,4, I need one line of 1234, and every time I press the button, I display either 1 or 2, and so on. What is a mistake, put on the right path, please.

public class MyWidget extends AppWidgetProvider { StringBuilder sb= new StringBuilder(""); final String LOG_TAG = "myLogs"; public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget"; public static String ACTION_WIDGET_RECEIVER2 = "ActionReceiverWidget2"; public static String ACTION_WIDGET_RECEIVER3= "ActionReceiverWidget3"; public static String ACTION_WIDGET_RECEIVER4 = "ActionReceiverWidget4"; private ComponentName thisWidget; String msg4 = "null"; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); Log.d(LOG_TAG, "onUpdate " + Arrays.toString(appWidgetIds)); //Создаем новый RemoteViews RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget); //Подготавливаем Intent для Broadcast Intent active = new Intent(context, MyWidget.class); active.setAction(ACTION_WIDGET_RECEIVER); active.putExtra("msg1", "1"); //создаем наше событие PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0); //регистрируем наше событие remoteViews.setOnClickPendingIntent(R.id.button, actionPendingIntent); //Подготавливаем Intent для Broadcast active.setAction(ACTION_WIDGET_RECEIVER2); active.putExtra("msg2", "2"); //создаем наше событие actionPendingIntent= PendingIntent.getBroadcast(context, 0, active, 0); //регистрируем наше событие remoteViews.setOnClickPendingIntent(R.id.button2, actionPendingIntent); active.setAction(ACTION_WIDGET_RECEIVER3); active.putExtra("msg3", "+"); //создаем наше событие actionPendingIntent= PendingIntent.getBroadcast(context, 0, active, 0); //регистрируем наше событие remoteViews.setOnClickPendingIntent(R.id.button4, actionPendingIntent); active.setAction(ACTION_WIDGET_RECEIVER4); active.putExtra("msg4", "result"); //создаем наше событие actionPendingIntent= PendingIntent.getBroadcast(context, 0, active, 0); //регистрируем наше событие remoteViews.setOnClickPendingIntent(R.id.button3, actionPendingIntent); //обновляем виджет appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); } @Override public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); String msg; //Ловим наш Broadcast, проверяем и выводим сообщение thisWidget = new ComponentName(context, MyWidget.class); RemoteViews view = new RemoteViews(context.getPackageName(), R.layout.widget); AppWidgetManager manager = AppWidgetManager.getInstance(context); final String action = intent.getAction(); switch (action){ case "ActionReceiverWidget": msg = intent.getStringExtra("msg1"); sb.append(intent.getStringExtra("msg1")); break; case "ActionReceiverWidget2": msg = intent.getStringExtra("msg2"); sb.append(msg); break; case "ActionReceiverWidget3": msg = intent.getStringExtra("msg3"); sb.append(msg); break; case "ActionReceiverWidget4": msg = intent.getStringExtra("msg4"); sb.append(msg); break; } view.setTextViewText(R.id.textView, sb.toString()); manager.updateAppWidget(thisWidget,view); //Toast.makeText(context, msg4, Toast.LENGTH_SHORT).show(); } 

}

  • I tried, it does not work. The strange thing is that as soon as I make operations with sb outside the switch construction, everything works as it should. The whole brain broke itself, I do not understand what's the matter - element111

1 answer 1

Let me explain how your code works:

  1. BroadcastReceiver receives an Intent , and is called onReceive .
  2. A new StringBuilder : StringBuilder sb= new StringBuilder("");
  3. You add a piece of text to it: sb.append(msg);
  4. Write the text in the View : view.setTextViewText(R.id.textView, sb.toString());
  5. Go to step 1.

That is, you do not need to create a new StringBuilder in paragraph 1, but bring it into the field.

  • I made the creation of a new builder at the very beginning of the class. Nothing has changed. Tell me on the fingers, otherwise I got lost in three pines:) I am beginning to go crazy slowly - element111
  • @ element111 show how you register the receiver, and where you took the creation of the builder. - Vladyslav Matviienko
  • @ element111, no, add code to the question. And this is not all the code I requested. - Vladyslav Matviienko
  • added the whole class - element111
  • one
    static StringBuilder sb; if(sb==null) sb = new StringBuilder(""); try this - santavital