在我的代码中,我在收到广播消息时启动 timerTask。这里我展示了两个类的代码。1.广播接收器。2. XML解析。
我的问题是当我调用 timerTask 时,在预定的时间 Timer 调用 & 我将所有数据放入 xml 类中。就像“ mServiceList ”一样。但是当我想每次使用这个数组时,它都变空了。
我不确定我在哪里做错了。
如果任何人有想法,那就太好了。
public class MyWidgetIntentReceiver extends BroadcastReceiver {
public static int clickCount = 0;
private String msg[] = null;
private ParsingXML myXMLHandler;
private ArrayList<Stream> mServiceList = new ArrayList<Stream>();
private UrlTimerTask mUrlTimerTask = null;
private Timer mTimer = null;
private String rssFeed = "https;??.....";
@Override
public void onReceive(Context context, Intent intent) {
mUrlTimerTask = new UrlTimerTask();
mTimer = new Timer();
mTimer.scheduleAtFixedRate(mUrlTimerTask, 0, 120000); // (60*2*1000)
if (intent.getAction().equals(WidgetUtils.WIDGET_UPDATE_ACTION1)) {
Toast.makeText(context, "NEXT!!", Toast.LENGTH_SHORT).show();
updatePreviousWidgetListener(context);
}
}
private void updatePreviousWidgetListener(Context context) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
// updating view
remoteViews.setTextViewText(R.id.title, getNextTitle(context));
remoteViews.setTextViewText(R.id.desc, getDesc(context));
// re-registering for click listener
remoteViews.setOnClickPendingIntent(R.id.sync_button,
MyWidgetProvider.buildButtonPendingIntent(context));
// re-registering for click listener
remoteViews.setOnClickPendingIntent(R.id.next,
MyWidgetProvider.buildNextButtonPendingIntent(context));
MyWidgetProvider.pushWidgetUpdate(context.getApplicationContext(),
remoteViews);
}
private String getDesc(Context context) {
// some static jokes from xml
msg = context.getResources().getStringArray(R.array.news_headlines);
if (clickCount >= msg.length) {
clickCount = 0;
}
return msg[clickCount];
}
private String getNextTitle(Context context) {
if (mServiceList != null && mServiceList.size() > 0) {
return mServiceList.get(clickCount).getTitle().toString();
} else {
mStreamList = myXMLHandler.getItemsList();
Toast.makeText(context, "no data found", Toast.LENGTH_SHORT).show();
return "no data";
}
}
private class UrlTimerTask extends TimerTask {
@Override
public void run() {
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
myXMLHandler = new ParsingXML();
xr.setContentHandler(myXMLHandler);
URL _url = new URL(rssFeed);
xr.parse(new InputSource(_url.openStream()));
mServiceList = myXMLHandler.getItemsList();
if (!mServiceList.isEmpty()) {
for (int i = 0; i < mServiceList.size(); i++) {
Log.d(TAG, mServiceList.get(i).getTitle());
}
}
} catch (ParserConfigurationException pce) {
Log.e("SAX XML", "sax parse error", pce);
} catch (SAXException se) {
Log.e("SAX XML", "sax error", se);
} catch (IOException e) {
e.printStackTrace();
}
}
}
您仍然有任何关于理解我的问题的疑问,请告诉我,我会尽力理解我的疑问。因为我想要答案。
简短说明:我想将数据解析为广播接收器类。
谢谢,