Как передать данные и открыть активность из виджета? - PullRequest
0 голосов
/ 24 февраля 2019

У меня есть виджет только с одним просмотром текста, который меняется каждые 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 Ответ

0 голосов
/ 24 февраля 2019
 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();
 } 
...