Android小部件内的Android Rss Feed

时间:2017-01-30 18:56:32

标签: android android-widget rss-reader

我尝试使用以下代码编写一个放在主屏幕上的小部件

   this.userObservable = this.db.getUserAsObjectObservable() as FirebaseObjectObservable<any>;
   this.userObservable.subscribe(data => console.log('Observable', data));

但是小部件在文本视图中显示时没有数据。

package com.example.anirudh.chatter; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.content.Intent; import android.os.AsyncTask; import android.widget.ArrayAdapter; import android.widget.RemoteViews; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; /** * Implementation of App Widget functionality. */ public class NewAppWidget extends AppWidgetProvider { void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) { RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget); views.setTextViewText(R.id.tv1, "hello"); System.out.print("abcdef"); Intent intent = new Intent(context, NewAppWidget.class); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); intent.setAction("update"); //This pending intent will run the intent created above PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); views.setOnClickPendingIntent(R.id.btnRefresh, pi); new MyAsyncTask().execute(context); // Instruct the widget manager to update the widget 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); } } @Override public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); if (intent.getAction().equals("update")) { new MyAsyncTask().execute(context); System.out.print("abcdef"); } } @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 } class MyAsyncTask extends AsyncTask<Context, Void, ArrayList<String>> { ArrayList<String> links = new ArrayList<>(); Context context; RemoteViews views; @Override protected ArrayList<String> doInBackground(Context... params) { context = params[0]; ArrayList<String> headlines = new ArrayList<>(); try { URL url = new URL("http://www.codingconnect.net/feed"); XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); factory.setNamespaceAware(false); XmlPullParser xpp = factory.newPullParser(); // We will get the XML from an input stream xpp.setInput(getInputStream(url), "UTF_8"); boolean insideItem = false; // Returns the type of current event: START_TAG, END_TAG, etc.. int eventType = xpp.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { if (eventType == XmlPullParser.START_TAG) { if (xpp.getName().equalsIgnoreCase("item")) { insideItem = true; } else if (xpp.getName().equalsIgnoreCase("title")) { if (insideItem) headlines.add(xpp.nextText()); //extract the headline } else if (xpp.getName().equalsIgnoreCase("link")) { if (insideItem) links.add(xpp.nextText()); //extract the link of article } } else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")) { insideItem = false; } eventType = xpp.next(); //move to next element } } catch (MalformedURLException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.print("heads:" + headlines); return headlines; } protected void onPostExecute(ArrayList<String> heads) { views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget); views.setTextViewText(R.id.tv1, " "); views.setTextViewText(R.id.tv2, " "); views.setTextViewText(R.id.tv3, " "); views.setTextViewText(R.id.tv4, " "); views.setTextViewText(R.id.tv5, " "); if (heads != null && heads.size() >= 5) { views.setTextViewText(R.id.tv1, heads.get(0)); views.setTextViewText(R.id.tv2, heads.get(1)); views.setTextViewText(R.id.tv3, heads.get(2)); views.setTextViewText(R.id.tv4, heads.get(3)); views.setTextViewText(R.id.tv5, heads.get(4)); } } public InputStream getInputStream(URL url) { try { return url.openConnection().getInputStream(); } catch (IOException e) { return null; } } } } 方法永远不会被调用, Asynctask 也是如此,它用于必须放在文本视图中的rss-feed标题。

有人知道我的代码中有什么问题吗?

0 个答案:

没有答案