启动服务每10秒 [英] Start service every 10 seconds

查看:199
本文介绍了启动服务每10秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我试图从每10秒的信息,用于测试目的这个Android服务。但是,从我所看到的服务正在启动而不是从它得到更新,每10秒。可以采取什么问题?

下面是我的服务:

 公共类SimpleService延伸服务{
    私有静态最终诠释NOVI_VESTI = 1;
    私有静态最终诠释NOVA_OGLASNA = 2;
    私人列表<串GT;标题;    @覆盖
    公众的IBinder onBind(意向为arg0){
        返回null;
    }    @覆盖
      公众诠释onStartCommand(意向意图,诠释标志诠释startId){
        // TODO做些有益的事
        返回START_STICKY;
      }    @覆盖
    公共无效的onCreate(){
        super.onCreate();
        如果(isOnline()){
            如果(获得preFS(vesti)!=){
                串vesti,oglasna;
                Toast.makeText(这一点,IMA preF,Toast.LENGTH_SHORT).show();
                vesti = readRss(http://www.zasvadba.mk/Vesti.xml);
                如果(!vesti.equals(获得preFS(vesti))){
                    上下文的背景下= SimpleService.this;
                    NotificationManager notificationManager =(NotificationManager)上下文
                            .getSystemService(NOTIFICATION_SERVICE);
                    通知updateComplete =新的通知();
                    updateComplete.icon = R.drawable.ic_launcher;
                    updateComplete.tickerText =上下文
                            .getText(R.string.newVesti);
                    updateComplete.when = System.currentTimeMillis的();
                    意图notificationIntent =新意图(背景下,Vesti.class);
                    的PendingIntent contentIntent = PendingIntent.getActivity(
                            上下文,0,notificationIntent,0);                    字符串contentTitle = context.getText(R.string.newVesti)
                            的ToString();
                    串contentText;
                    contentText = vesti.toString();
                    updateComplete.setLatestEventInfo(背景下,contentTitle,
                            contentText,contentIntent);                    notificationManager.notify(NOVI_VESTI,updateComplete);                }                oglasna = readRss(http://www.finki.ukim.mk/mk/rss/announcements);
                如果(!oglasna.equals(获得preFS(oglasna))){                    上下文的背景下= SimpleService.this;
                    NotificationManager notificationManager =(NotificationManager)上下文
                            .getSystemService(NOTIFICATION_SERVICE);
                    通知updateComplete =新的通知();
                    updateComplete.icon = R.drawable.ic_launcher;
                    updateComplete.tickerText =上下文
                            .getText(R.string.newOglasna);
                    updateComplete.when = System.currentTimeMillis的();
                    意图notificationIntent =新意图(背景下,
                            OglasnaTabla.class);
                    的PendingIntent contentIntent = PendingIntent.getActivity(
                            上下文,0,notificationIntent,0);                    字符串contentTitle = context.getText(R.string.newOglasna)
                            的ToString();
                    串contentText;
                    contentText = vesti.toString();
                    updateComplete.setLatestEventInfo(背景下,contentTitle,
                            contentText,contentIntent);                    notificationManager.notify(NOVA_OGLASNA,updateComplete);
                }
            }        }    }
    @覆盖
    公共无效onRebind(意向意图){
        super.onRebind(意向);
        Toast.makeText(这一点,重新绑定,Toast.LENGTH_SHORT).show();    }
    @覆盖
    公共无效的onDestroy(){
        super.onDestroy();
    }    //标题为
    公共字符串readRss(字符串feedLink){
        标题=新的ArrayList<串GT;();
        尝试{
            网址URL =新的URL(feedLink);            XmlPullParserFactory工厂= XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(假);
            XmlPullParser XPP = factory.newPullParser();            //我们将从输入流获取XML
            xpp.setInput(的getInputStream(URL),UTF_8);            布尔insideItem = FALSE;            //返回当前事件的类型:START_TAG,END_TAG等。
            INT EVENTTYPE = xpp.getEventType();
            而(EVENTTYPE!= XmlPullParser.END_DOCUMENT){
                如果(EVENTTYPE == XmlPullParser.START_TAG){                    如果(xpp.getName()。equalsIgnoreCase(项目)){
                        insideItem = TRUE;
                    }否则如果(xpp.getName()。equalsIgnoreCase(标题)){
                        如果(insideItem)
                            titles.add(xpp.nextText());
                        // headlines.add(xpp.nextText()); //提取
                    }
                }否则如果(EVENTTYPE == XmlPullParser.END_TAG
                        &功放;&安培; xpp.getName()。equalsIgnoreCase(项目)){
                    insideItem = FALSE;
                }                EVENTTYPE = xpp.next(); //移动到下一个元素
            }        }赶上(MalformedURLException的E){
            e.printStackTrace();
        }赶上(XmlPullParserException E){
            e.printStackTrace();
        }赶上(IOException异常五){
            e.printStackTrace();
        }
        返回titles.get(0);
    }    公众的InputStream的getInputStream(网址URL){
        尝试{
            返回url.openConnection()的getInputStream()。
        }赶上(IOException异常五){
            返回null;
        }
    }    私人字符串获取preFS(串类){
        共享preferences preferences = preferenceManager
                .getDefaultShared preferences(本);
        字符串preF = preferences.getString(类别,);
        返回preF;
    }    私有静态布尔isOnline(){
        尝试{
            InetAddress.getByName(google.com)isReachable(3)。
            返回true;
        }赶上(UnknownHostException异常五){
            返回false;
        }赶上(IOException异常五){
            返回false;
        }
    }
}

下面是我在我的主要活动

 意图myIntent =新意图(Main.this,SimpleService.class);
            的PendingIntent = PendingIntent.getService(Main.this,0,myIntent,0);
            AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);
            台历挂历= Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis的());
            calendar.add(Calendar.SECOND,10);
            alarmManager.set(AlarmManager.RTC_WAKEUP,
                    calendar.getTimeInMillis(),的PendingIntent);


解决方案

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

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

I have this android service that I'm trying to get information from every 10 sec, for testing purposes. But from what I can see service is starting but not getting update from it every 10 second. What can be the problem?

Here is my service:

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

Here is what I have in my main activity

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

解决方案

set alarm repeating for 10 mins you can keep in milliseconds 1000*60*10

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

这篇关于启动服务每10秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆