小部件中的Android Rss提要

小部件中的Android Rss提要,android,android-widget,rss-reader,Android,Android Widget,Rss Reader,我尝试编写一个小部件,该小部件用以下代码显示在主屏幕上: 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;

我尝试编写一个小部件,该小部件用以下代码显示在主屏幕上:

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;
            }
        }
    }
}
package com.example.anirudh.chatter;
导入android.app.pendingent;
导入android.appwidget.AppWidgetManager;
导入android.appwidget.AppWidgetProvider;
导入android.content.Context;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.widget.ArrayAdapter;
导入android.widget.remoteview;
导入org.xmlpull.v1.XmlPullParser;
导入org.xmlpull.v1.XmlPullParserException;
导入org.xmlpull.v1.XmlPullParserFactory;
导入java.io.IOException;
导入java.io.InputStream;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.util.ArrayList;
/**
*应用程序小部件功能的实现。
*/
公共类NewAppWidget扩展了AppWidgetProvider{
void updateAppWidget(上下文上下文,AppWidgetManager AppWidgetManager,
int-appWidgetId){
RemoteView视图=新远程视图(context.getPackageName(),R.layout.new\u app\u小部件);
views.setTextViewText(R.id.tv1,“hello”);
系统输出打印(“abcdef”);
Intent Intent=newintent(上下文,NewAppWidget.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetId);
意图。设定行动(“更新”);
//此挂起的意图将运行上面创建的意图
PendingIntent pi=PendingIntent.getBroadcast(上下文,0,意图,PendingIntent.FLAG_UPDATE_CURRENT);
views.SetOnClickPendingContent(R.id.btnRefresh,pi);
新建MyAsyncTask().execute(上下文);
//指示小部件管理器更新小部件
UpdateAppWidgetManager.UpdateAppWidgetId(appWidgetId,视图);
}
@凌驾
公共void onUpdate(上下文上下文,AppWidgetManager AppWidgetManager,int[]AppWidgetId){
//可能有多个窗口小部件处于活动状态,因此请更新所有窗口小部件
for(int-appWidgetId:appWidgetId){
updateAppWidget(上下文、appWidgetManager、appWidgetId);
}
}
@凌驾
公共void onReceive(上下文、意图){
super.onReceive(上下文、意图);
if(intent.getAction().equals(“更新”)){
新建MyAsyncTask().execute(上下文);
系统输出打印(“abcdef”);
}
}
@凌驾
公共void已启用(上下文){
//输入创建第一个小部件时的相关功能
}
@凌驾
已禁用公共无效(上下文){
//输入禁用最后一个小部件时的相关功能
}
类MyAsyncTask扩展了AsyncTask{
ArrayList links=新的ArrayList();
语境;
远程视图;
@凌驾
受保护的ArrayList doInBackground(上下文…参数){
上下文=参数[0];
ArrayList headlines=新的ArrayList();
试一试{
URL=新URL(“http://www.codingconnect.net/feed");
XmlPullParserFactory工厂=XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp=factory.newPullParser();
//我们将从输入流中获取XML
setInput(getInputStream(url),“UTF_8”);
布尔值insideItem=false;
//返回当前事件的类型:开始标记、结束标记等。。
int eventType=xpp.getEventType();
while(eventType!=XmlPullParser.END_文档){
if(eventType==XmlPullParser.START_标记){
if(xpp.getName().equalsIgnoreCase(“项”)){
insideItem=真;
}else if(xpp.getName().equalsIgnoreCase(“title”)){
如果(内部项目)
headlines.add(xpp.nextText());//提取标题
}else if(xpp.getName().equalsIgnoreCase(“链接”)){
如果(内部项目)
links.add(xpp.nextText());//提取文章的链接
}
}else if(eventType==XmlPullParser.END_标记&&xpp.getName().equalsIgnoreCase(“项”)){
insideItem=假;
}
eventType=xpp.next();//移动到下一个元素
}
}捕获(格式错误){
e、 printStackTrace();
}catch(XMLPullParseRexE){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
系统输出打印(“标题:+标题”);
返回标题;
}
PostExecute上受保护的void(ArrayList头){
视图=新的远程视图(context.getPackageName(),R.layout.new\u app\u小部件);
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));
}
}
公共输入流getInputStream(URL){
试一试{
返回url.openConnection().getInputStream();
}捕获(IOE异常){
返回null;
}
}
}
}
但是小部件在文本视图上显示时没有数据

从不调用onreceive的
onreceive
方法,用于rss-f的Asynctask方法也是如此