Question

In my code, I am starting timerTask when broadcast message received. Here I displayed code of two classes. 1. broadcast receiver. 2. XML parsing.

my Question is when I call timerTask, at scheduled time Timer call & I get all data into xml class.Like "mServiceList" .But When I want to use this array every time it is getting null.

I am not sure where I am doing wrong.

If any body have idea, then its great.

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();
        }

    }
}

Still you have any query regarding to understand my question pls tell me I will try my best to understand my query. because I want answer.

Short Note : I want to use parsing data into broadcast receiver class.

Thanks,

Was it helpful?

Solution

I couldn't understand, in your BroadcastReceiver's onReceive(Context context, Intent intent) , you called updatePreviousWidgetListener(context); this method. But rest of your code shows no call to this method.

If I am not wrong, you have called wrong method in onReceive(Context context, Intent intent). When does updatePreviousWidgetListener(context); get called?

Correct me if I am wrong.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top