Как передать данные и открыть активность из виджета?
У меня есть виджет только с одним textview, который меняется каждые 24 часа. То, что я хочу, это когда я нажимаю на виджет сверху, чтобы открыть действие с текстом из textview.
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
TinyDB tinydb;
Random r = new Random();
int RandomNr;
tinydb = new TinyDB(context);
String[] list = tinydb.getListString(Constants.from_Quotes).toArray(new String[0]);
RandomNr = r.nextInt(list.length - 1);
String Quote = list[RandomNr];
Intent intent = new Intent(context, QuoteActivity.class);
intent.putExtra("StartedFrom", Constants.from_Widget );
tinydb.putString(Constants.from_Widget,Quote);
tinydb.putBoolean("WidgedAdded",true);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.quote_widget);
views.setTextViewText(R.id.appwidget_text, Quote);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.appwidget_text, pendingIntent);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
и там никаких лишних не прошло..
на QuoteActivity.java я проверяю так:
try {
Bundle bundle = getIntent().getExtras();
StartedFrom = bundle.getString("StartedFrom");
}catch (Exception e){
e.printStackTrace();
}
Я получаю нулевое исключение.
Я не знаю, где проблема, есть идеи?
1 ответ
Решение
void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
TinyDB tinydb;
Random r = new Random();
int RandomNr;
tinydb = new TinyDB(context);
String[] list = tinydb.getListString(Constants.from_Quotes).toArray(new String[0]);
RandomNr = r.nextInt(list.length - 1);
String Quote = list[RandomNr];
Intent intent = new Intent(context, QuoteActivity.class);
intent.putExtra("StartedFrom", Quote);
tinydb.putString(Constants.from_Widget,Quote);
tinydb.putBoolean("WidgedAdded",true);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.quote_widget);
views.setTextViewText(R.id.appwidget_text, Quote);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.appwidget_text, pendingIntent);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
And in QuoteActivity
try {
Bundle bundle = getIntent().getExtras();
String StartedFrom = bundle.getString("StartedFrom");
}catch (Exception e){
e.printStackTrace();
}