Reputation: 416
I have a widget with only one textview,that changes every 24 hours. What I want is when I click on widget top open an activity with text from 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);
}
and There no extra passed..
on QuoteActivity.java I check like this:
try {
Bundle bundle = getIntent().getExtras();
StartedFrom = bundle.getString("StartedFrom");
}catch (Exception e){
e.printStackTrace();
}
I get null Exception.
I don't know where the problem is, any idea ?
Upvotes: 1
Views: 196
Reputation: 1151
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();
}
Upvotes: 1