S
schlossero
Neues Mitglied
- 0
Hallo zusammen. Ich komme mit dem AsyncTask einfach nicht zurecht. Könnte mir bei folgendem Schnipsel jemand helfen?
Bin für jeden Rat Dankbar
So habe ich es jetzt versucht. Aber das ist Falsch.
Bin für jeden Rat Dankbar
So habe ich es jetzt versucht. Aber das ist Falsch.
Code:
package com.fff.android.reader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.SystemClock;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class RssService extends Service{
private RSSFeed myRssFeed = null;
private ArrayAdapter<RSSItem> adapter;
private String old;
private Handler mHandler = new Handler();
private long mStartTime;
/** Keeps track of all current registered clients. */
ArrayList<Messenger> mClients = new ArrayList<Messenger>();
/** Holds last value set by a client. */
int mValue = 0;
/**
* Handler of incoming messages from clients.
*/
class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
}
NotificationManager mNM;
@Override
public void onCreate() {
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
getRSSRead();
old = adapter.getItem(0).toString();
}
@Override
public void onDestroy() {
mHandler.removeCallbacks(checkUpdates);
}
@Override
public void onStart(Intent intent, int startid) {
mStartTime = System.currentTimeMillis();
mHandler.removeCallbacks(checkUpdates);
mHandler.postDelayed(checkUpdates, 100);
}
private Runnable checkUpdates = new Runnable(){
public void run(){
final long start = mStartTime;
long millis = SystemClock.uptimeMillis() - start;
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
getRSSRead();
if (adapter != null && !adapter.isEmpty()){
String update = adapter.getItem(0).toString();
if (!update.equals(old))
showNotification(update);
old = update;
}
mHandler.postAtTime(this, start + (((minutes * 60) + seconds + 60) * 10000));
mHandler.postDelayed(checkUpdates, 3600000);
}
};
private void showNotification(String update) {
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.ya, adapter.getItem(0).toString(), System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, AndroidRssReader.class), 0);
// Set the info that shows in the notification panel.
notification.setLatestEventInfo(this, update, "", contentIntent);
// Send the notification.
mNM.notify(0, notification);
}
public void getRSSRead(){
new MyTask().execute();
}
private class MyTask extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... arg0) {
try {
URL rssUrl = new URL("http://xxx/index.php/ueber-uns/presse/2009/rss?format=feed&type=rss");
SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler();
myXMLReader.setContentHandler(myRSSHandler);
InputSource myInputSource = new InputSource(rssUrl.openStream());
myXMLReader.parse(myInputSource);
myRssFeed = myRSSHandler.getFeed();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if (myRssFeed!=null)
{
ArrayAdapter<RSSItem> adapter =
new ArrayAdapter<RSSItem>(getApplicationContext(),
android.R.layout.simple_list_item_1,myRssFeed.getList());
}
}
/* protected Void doInBackground(Void... arg0) {
try {
URL rssUrl = new URL("http://www.xxx.de/index.php/ueber-uns/presse/2009/rss?format=feed&type=rss");
SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler();
myXMLReader.setContentHandler(myRSSHandler);
InputSource myInputSource = new InputSource(rssUrl.openStream());
myXMLReader.parse(myInputSource);
myRssFeed = myRSSHandler.getFeed();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (myRssFeed!=null)
adapter = new ArrayAdapter<RSSItem>(this, android.R.layout.simple_list_item_1,myRssFeed.getList());
}*/
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}}
Zuletzt bearbeitet: