0

テスト目的で、10秒ごとに情報を取得しようとしているこのAndroidサービスがあります。しかし、私が見ることができることから、サービスは開始されていますが、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

使用する:

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

于 2013-03-12T09:19:46.160 に答える
1

方法は、を使用しHandleてアクティビティをその中に入れることです。

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 に答える