Android线程问题Thread-387

时间:2013-03-01 13:51:21

标签: android multithreading runnable

我在使用线程时遇到问题。我得到了他需要检查更新的服务类,然后睡了24小时。

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

}

但是当我尝试在DDMS中运行它时,我得到以下日志:

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)

有没有人知道如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

好的,在Android上,遗憾的是你的设计是完全错误的。不要创建一个线程并在24小时内休眠。例如,您的应用很可能会在此期间被系统杀死,以便为其他更重要的应用(例如前景中的应用)腾出空间。您可以通过多种方式定期唤醒应用。一种可能性是AlarmManager。

请看每个小时做某事的答案:

How to get my app to do something every hour