質問

私のコードでは、ブロードキャストメッセージが受信されたときにTimerTaskを開始しています。ここでは、2つのクラスのコードを表示しました。 1.ブロードキャストレシーバー。 2. XML解析。

私の質問は、スケジュールされたタイムタイマーコールでTimerTaskに電話するとき、すべてのデータをXMLクラスに入手してください。Mservicelist「しかし、この配列を使用したいときは、nullを取得するたびに。

どこで間違っているのかわかりません。

誰かがアイデアを持っているなら、それは素晴らしいです。

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

    }
}

それでも、あなたは私の質問を理解するための質問がありますplsは私が私の質問を理解するために最善を尽くすと教えてくれます。答えが欲しいから。

短いメモ:ブロードキャストレシーバークラスに解析データを使用したいと思います。

ありがとう、

役に立ちましたか?

解決

あなたの放送局の中で、私は理解できませんでした onReceive(Context context, Intent intent) 、 きみが呼んだ updatePreviousWidgetListener(context); この方法。ただし、残りのコードには、この方法への呼び出しが表示されません。

私が間違っていない場合、あなたは間違った方法を呼びました onReceive(Context context, Intent intent). 。いつ updatePreviousWidgetListener(context); 呼ばれますか?

私が間違っているなら私を修正してください。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top