每10秒启动一次服务

时间:2013-03-01 17:37:55

标签: android android-service android-alarms

我有这个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);

3 个答案:

答案 0 :(得分:2)

设置警报重复10分钟,你可以保持毫秒1000 * 60 * 10

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

答案 1 :(得分:1)

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

new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
            Intent myIntent = new Intent(Main.this, SimpleService.class); 
        //etc.....
        }, 10*1000);
    }

答案 2 :(得分:1)

使用:

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