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(); }
}