如何从 Widget 传递数据并打开一个 Activity?
How to pass data and open an Activity from Widget?
我有一个只有一个文本视图的小部件,每 24 小时更改一次。
我想要的是当我点击小部件顶部时打开一个 activity,其中包含文本视图中的文本。
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();
}
我得到空异常。
我不知道问题出在哪里,知道吗?
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();
}
我有一个只有一个文本视图的小部件,每 24 小时更改一次。 我想要的是当我点击小部件顶部时打开一个 activity,其中包含文本视图中的文本。
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();
}
我得到空异常。
我不知道问题出在哪里,知道吗?
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();
}