Search code examples
androidmultithreadingrunnable

Android thread issue Thread-387


I have a problem using threads. I got his service class that i need to check for updates and then sleep for 24h.

public class SimpleService extends Service {
    private static final int NOVI_VESTI = 1;
    private static final int NOVA_OGLASNA = 2;
    private List<String> titles;

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        Thread t = new Thread() {
            @Override
            public void run() {
                while (true) {
                    try {
                        String vesti, oglasna;
                        vesti = readRss("http://www.finki.ukim.mk/mk/rss/news");
                        // readRss("http://www.finki.ukim.mk/mk/rss/news");
                        // getPrefs("vesti");
                        if (!vesti.equals(getPrefs("vesti"))) {
                            Context context = SimpleService.this;
                            NotificationManager notificationManager = (NotificationManager) context
                                    .getSystemService(NOTIFICATION_SERVICE);
                            Notification updateComplete = new Notification();
                            updateComplete.icon = R.drawable.ic_launcher;
                            updateComplete.tickerText = context
                                    .getText(R.string.newVesti);
                            updateComplete.when = System.currentTimeMillis();
                            Intent notificationIntent = new Intent(context,
                                    Vesti.class);
                            PendingIntent contentIntent = PendingIntent
                                    .getActivity(context, 0,
                                            notificationIntent, 0);

                            String contentTitle = context.getText(
                                    R.string.newVesti).toString();
                            String contentText;
                            contentText = vesti.toString();
                            updateComplete.setLatestEventInfo(context,
                                    contentTitle, contentText, contentIntent);

                            notificationManager.notify(NOVI_VESTI,
                                    updateComplete);

                        }

                        oglasna = readRss("http://www.finki.ukim.mk/mk/rss/announcements");
                        if (!oglasna.equals(getPrefs("oglasna"))) {

                            Context context = SimpleService.this;
                            NotificationManager notificationManager = (NotificationManager) context
                                    .getSystemService(NOTIFICATION_SERVICE);
                            Notification updateComplete = new Notification();
                            updateComplete.icon = R.drawable.ic_launcher;
                            updateComplete.tickerText = context
                                    .getText(R.string.newOglasna);
                            updateComplete.when = System.currentTimeMillis();
                            Intent notificationIntent = new Intent(context,
                                    OglasnaTabla.class);
                            PendingIntent contentIntent = PendingIntent
                                    .getActivity(context, 0,
                                            notificationIntent, 0);

                            String contentTitle = context.getText(
                                    R.string.newOglasna).toString();
                            String contentText;
                            contentText = vesti.toString();
                            updateComplete.setLatestEventInfo(context,
                                    contentTitle, contentText, contentIntent);

                            notificationManager.notify(NOVA_OGLASNA,
                                    updateComplete);

                            sleep(60 * 60 * 24);

                        }
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        };
        t.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    // title for both
    public String readRss(String feedLink) {

        try {
            URL url = new URL(feedLink);

            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)
                            titles.add(xpp.nextText());
                        // headlines.add(xpp.nextText()); // extract the
                    }
                } 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();
        }
        return titles.get(0);
    }

    public InputStream getInputStream(URL url) {
        try {
            return url.openConnection().getInputStream();
        } catch (IOException e) {
            return null;
        }
    }

    private String getPrefs(String category) {
        SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        String pref = preferences.getString(category, "");
        return pref;
    }

}

But when I try to run this in DDMS i get the following log:

03-01 14:46:26.913: E/AndroidRuntime(3308): FATAL EXCEPTION: Thread-387
03-01 14:46:26.913: E/AndroidRuntime(3308): java.lang.NullPointerException
03-01 14:46:26.913: E/AndroidRuntime(3308):     at com.finki.darko.mk.services.SimpleService.readRss(SimpleService.java:146)
03-01 14:46:26.913: E/AndroidRuntime(3308):     at com.finki.darko.mk.services.SimpleService$1.run(SimpleService.java:47)

Does anyone has an idea how to fix this problem?


Solution

  • Ok, on Android, your design is completely wrong unfortunately. Do not create a thread and sleep for 24 hours. For example, it is very likely that your app will get killed by the system during that time to make room for other more important apps (like the one in the foreground). There are a variety of ways to have your app awakened periodically. One possibility is AlarmManager.

    See this answer for doing something every hour:

    How to get my app to do something every hour