Элемент обновления списка просмотра http и внешнее приложение Intent onclick item - PullRequest
0 голосов
/ 18 октября 2018

Виджет в начале просит вставить слово для фазы конфигурации.

Это слово должно всегда использоваться для последующей выборки.

Затем производится первая выборка, чтобы взять данные и затем загрузить их в просмотр списка, и с помощью кнопки обновления следует обновить новые элементы, вместо этого при нажатии на элемент следует открыть приложение.в этом конкретном сообщении, если приложение установлено.

Проблемы:

  • a) Получить http
    1. Когда я добавляюВиджет в первый раз, данные не загружаются, поэтому представление списка пусто.
    2. Во второй раз, когда я удаляю и добавляю виджет (с тем же словом), данные загружаются, затем представление списка заполняется.
    3. Если я удаляю виджет или добавляю другой (с тем же словом), данные можно увидеть, но они всегда одинаковы, поэтому новый вызов http не выполняется, поэтому данные не меняются.
  • b) Получить обновление http
    • Я не могу быть уверен, что при нажатии кнопки обновления данные списка будут изменены с новыми данными, взятыми из http.
  • c) Намеренный объект onclick
    • Я хотел бычтобы убедиться, что при нажатии элемента во внешнем приложении запускается Intent, я перепробовал все, но, похоже, не могу.есть ли способ?

Манифест

       <activity android:name=".WidgetConfigureActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".Widget">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action android:name="MY_PACKAGE_NAME.WIDGET_BUTTON" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget_info" />
        </receiver>

        <service
            android:name=".WidgetService"
            android:permission="android.permission.BIND_REMOTEVIEWS" />

WidgetService

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.widget.RemoteViewsService;

public class WidgetService extends RemoteViewsService {
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return (new ListProvider(this.getApplicationContext(), intent));
    }
}

Виджет

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

public class Widget extends AppWidgetProvider {

    public static String WIDGET_BUTTON = "MY_PACKAGE_NAME.WIDGET_BUTTON";
    static Context contextApp;
    static int appWidgetId;


    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                int appWidgetId) {
        appWidgetId = appWidgetId;
        contextApp = context;
        CharSequence widgetText = WidgetConfigureActivity.loadTitlePref(context, appWidgetId);
        Log.v("Ok:widget", String.valueOf(widgetText));
        // Construct the RemoteViews object
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
        views.setTextViewText(R.id.hashtag, "#" + widgetText);

        Intent intentR = new Intent(WIDGET_BUTTON);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentR, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.refresh, pendingIntent);

        // Instruct the widget manager to update the widget
        Intent intent = new Intent(context, WidgetService.class);
        views.setRemoteAdapter(R.id.widgetListView, intent);
        views.setEmptyView(R.id.widgetListView, R.id.empty_view);
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them
        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        // When the user deletes the widget, delete the preference associated with it.
        for (int appWidgetId : appWidgetIds) {
            WidgetConfigureActivity.deleteTitlePref(context, appWidgetId);
        }
    }

    @Override
    public void onEnabled(Context context) {
        // Enter relevant functionality for when the first widget is created
    }

    @Override
    public void onDisabled(Context context) {
        // Enter relevant functionality for when the last widget is disabled
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        if (WIDGET_BUTTON.equals(intent.getAction())) {
            CharSequence widgetText = WidgetConfigureActivity.loadTitlePref(context, appWidgetId);
            Toast.makeText(contextApp, "Refresh-"+widgetText, Toast.LENGTH_SHORT).show();
        }
    }

}

WidgetConfigureActivity

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

/**
 * The configuration screen for the {@link Widget Widget} AppWidget.
 */
public class WidgetConfigureActivity extends Activity {

    private static final String PREFS_NAME = "com.widgettwitter.Widget";
    private static final String PREF_PREFIX_KEY = "appwidget_";
    int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
    EditText mAppWidgetText;
    View.OnClickListener mOnClickListener = new View.OnClickListener() {
        public void onClick(View v) {
            final Context context = WidgetConfigureActivity.this;

            // When the button is clicked, store the string locally
            String widgetText = mAppWidgetText.getText().toString();
            saveTitlePref(context, mAppWidgetId, widgetText);

            // It is the responsibility of the configuration activity to update the app widget
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
            Widget.updateAppWidget(context, appWidgetManager, mAppWidgetId);

            // Make sure we pass back the original appWidgetId
            Intent resultValue = new Intent();
            resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
            setResult(RESULT_OK, resultValue);
            finish();
        }
    };

    public WidgetConfigureActivity() {
        super();
    }

    // Write the prefix to the SharedPreferences object for this widget
    static void saveTitlePref(Context context, int appWidgetId, String text) {
        SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
        prefs.putString(PREF_PREFIX_KEY + appWidgetId, text);
        prefs.apply();
    }

    // Read the prefix from the SharedPreferences object for this widget.
    // If there is no preference saved, get the default from a resource
    static String loadTitlePref(Context context, int appWidgetId) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
        String titleValue = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null);
        //Log.v("Ok:", titleValue);
        if (titleValue != null) {
            return titleValue;
        } else {
            return "photo";
            //return context.getString(R.string.appwidget_text);
        }
    }

    static void deleteTitlePref(Context context, int appWidgetId) {
        SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
        prefs.remove(PREF_PREFIX_KEY + appWidgetId);
        prefs.apply();
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        // Set the result to CANCELED.  This will cause the widget host to cancel
        // out of the widget placement if the user presses the back button.
        setResult(RESULT_CANCELED);

        setContentView(R.layout.widget_configure);
        mAppWidgetText = (EditText) findViewById(R.id.appwidget_text);
        findViewById(R.id.add_button).setOnClickListener(mOnClickListener);

        // Find the widget id from the intent.
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            mAppWidgetId = extras.getInt(
                    AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        // If this activity was started with an intent without an app widget ID, finish with an error.
        if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
            finish();
            return;
        }

        mAppWidgetText.setText(loadTitlePref(WidgetConfigureActivity.this, mAppWidgetId));
    }
}

ListProvider

import android.appwidget.AppWidgetManager;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService.RemoteViewsFactory;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

class ListItem {
    public String id, ch, fullname, username, timestamp, text;
}

public class ListProvider implements RemoteViewsFactory {
    private ArrayList<ListItem> listItemList = new ArrayList<ListItem>();
    private Context context = null;
    private int appWidgetId;

    private String url;
    private JSONArray array = new JSONArray();

    public ListProvider(Context context, Intent intent) {
        this.context = context;
        appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        CharSequence widgetText = WidgetConfigureActivity.loadTitlePref(context, appWidgetId);

        url = "https://twitter.com/search?f=tweets&q=%23"+String.valueOf(widgetText)+"&lang=it";
        new Html().execute();
    }


    @Override
    public int getCount() {
        return listItemList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public RemoteViews getViewAt(int position) {
        final RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.list_row);
        ListItem listItem = listItemList.get(position);
        URL url = null;
        try {
            url = new URL(listItem.ch);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        Bitmap bmp = null;
        try {
            bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        bmp = getCroppedBitmap(bmp);
        remoteView.setImageViewBitmap(R.id.ch, bmp);
        remoteView.setTextViewText(R.id.name, listItem.fullname);
        remoteView.setTextViewText(R.id.username, listItem.username);
        remoteView.setTextViewText(R.id.timestamp, listItem.timestamp);
        remoteView.setTextViewText(R.id.text, listItem.text);

        Intent intent;
        try{
            intent = new Intent(Intent.ACTION_VIEW,Uri.parse("twitter://status?status_id="+listItem.id));
            intent.setPackage("com.twitter.android");
        }catch(ActivityNotFoundException ex){
            intent = new Intent(Intent.ACTION_VIEW,Uri.parse("https://twitter.com/"+listItem.username+"/status/"+listItem.id));
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        remoteView.setOnClickFillInIntent(R.id.widgetItemContainer, intent);
        return remoteView;
    }

    public Bitmap getCroppedBitmap(Bitmap bitmap) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        // canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
                bitmap.getWidth() / 2, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        //Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
        //return _bmp;
        return output;
    }

    @Override
    public RemoteViews getLoadingView() {
        return null;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public void onCreate() {
    }

    @Override
    public void onDataSetChanged() {
    }

    @Override
    public void onDestroy() {
    }

    private class Html extends AsyncTask<Void, Void, Void> {
        String desc;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {
            try {
                // Connect to the web site
                Document doc = Jsoup.connect(url).get();
                // Using Elements to get the Meta data
                Elements mElementDataSize = doc.select("div[class=content]");
                // Locate the content attribute
                int mElementSize = mElementDataSize.size();

                for (int i = 0; i < mElementSize; i++) {
                    JSONObject obj = new JSONObject();

                    Elements mElementId = doc.select("li[class=js-stream-item stream-item stream-item]").eq(i);
                    String mId = mElementId.attr("data-item-id");

                    Elements mElementAvatar = doc.select("img[class=avatar js-action-profile-avatar]").eq(i);
                    String mAvatar = mElementAvatar.attr("src");

                    Elements mElementFullName = doc.select("strong[class=fullname show-popup-with-id u-textTruncate]").eq(i);
                    String mFullName = mElementFullName.text();

                    Elements mElementUsername = doc.select("span[class=username u-dir u-textTruncate]").eq(i);
                    String mUsername = mElementUsername.text();

                    Elements mElementTimestamp = doc.select("span[class=_timestamp js-short-timestamp js-relative-timestamp]").eq(i);
                    String mTimestamp = mElementTimestamp.text();

                    Elements mElementText = doc.select("p[class=TweetTextSize  js-tweet-text tweet-text]").eq(i);
                    String mText = mElementText.text();

                    try {
                        obj.put("id", mId);
                        obj.put("avatar", mAvatar);
                        obj.put("fullname", mFullName);
                        obj.put("username", mUsername);
                        obj.put("timestamp", mTimestamp);
                        obj.put("text", mText);
                        array.put(obj);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            populateListItem(array);
        }

        private void populateListItem(JSONArray array) {
            for (int i = 0; i < array.length(); i++) {
                ListItem listItem = new ListItem();
                try {
                    JSONObject o = array.getJSONObject(i);
                    Log.v("Ok:", String.valueOf(array.getJSONObject(i)));
                    listItem.id = o.getString("id");
                    listItem.fullname = o.getString("fullname");
                    listItem.username = o.getString("username");
                    listItem.timestamp = "· " + o.getString("timestamp");
                    listItem.text = o.getString("text");
                    listItem.ch = o.getString("avatar");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                listItemList.add(listItem);
            }
        }
    }

}
  • Несколько советов?
...