0

我有这个 android 服务,我试图每 10 秒获取一次信息,用于测试目的。但据我所知,服务正在启动,但不是每 10 秒更新一次。可能是什么问题?

这是我的服务:

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 int onStartCommand(Intent intent, int flags, int startId) {
        //TODO do something useful
        return START_STICKY;
      }

    @Override
    public void onCreate() {
        super.onCreate();
        if (isOnline()) {
            if (getPrefs("vesti")!="") {
                String vesti, oglasna;
                Toast.makeText(this, "ima pref", Toast.LENGTH_SHORT).show();
                vesti = readRss("http://www.zasvadba.mk/Vesti.xml");
                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);
                }
            }

        }

    }
    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
        Toast.makeText(this, "rebind", Toast.LENGTH_SHORT).show();

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

    // title for both
    public String readRss(String feedLink) {
        titles = new ArrayList<String>();
        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;
    }

    private static boolean isOnline() {
        try {
            InetAddress.getByName("google.com").isReachable(3);
            return true;
        } catch (UnknownHostException e) {
            return false;
        } catch (IOException e) {
            return false;
        }
    }
}

这是我的主要活动

Intent myIntent = new Intent(Main.this, SimpleService.class);
            pendingIntent = PendingIntent.getService(Main.this, 0, myIntent, 0);
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.add(Calendar.SECOND, 10);
            alarmManager.set(AlarmManager.RTC_WAKEUP,
                    calendar.getTimeInMillis(), pendingIntent);
4

3 回答 3

2

设置闹钟重复 10 分钟你可以保持在毫秒 1000*60*10

alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);
于 2013-03-01T17:46:09.873 回答
1

方法是使用 aHandle并将您的活动放入其中:

new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
            Intent myIntent = new Intent(Main.this, SimpleService.class); 
        //etc.....
        }, 10*1000);
    }
于 2013-03-01T17:51:02.967 回答
1

利用:

alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),10000, pendingIntent);

于 2013-03-12T09:19:46.160 回答