0
public class MyService extends Service {
    long i = 0;
    final String TAG="MyService";
    public void onCreate() {
        //initialize service here
    }
    public int onStartCommand(Intent intent, int flags, int startId) {
        log.i(TAG, i+":BEGIN");
        if (intent!=null) {
            String action = intent.getAction();
            log.i(TAG, i+":"+action);
            if (action.equals("RETRIEVE_SETTINGS_FROM_NET_SERVER") {
                new GetSettings().run();
            } else if (action.equals("RESTART_SERVICE_COMPONENT")) {
                //create alarmmanager code here
            }
            log.i(TAG, i+":complete");
            i++;
        }
    }
    private class GetSettings implements Runnable {
        String urlString;
        public GetSettings(String _urlString) {
            if (_urlString!=null) urlString = _urlString;
        }
        public void run() {
            HttpURLConnection c;
            if (urlString!= null) {
                try {
                    URL u = new URL(urlString);
                    c = (HttpURLConnection) u.openConnection();
                    c.setRequestMethod("GET");
                    c.setInstanceFollowRedirects(true);
                    c.setDoOutput(true);
                    c.connect();
                    int serverResponse = c.getResponseCode();
                    switch (serverResponse) {
                        case HttpURLConnection.HTTP_OK:
                            InputStream in;
                            in = c.getInputStream();
                            int next = -1;
                            StringBuffer tmpSvrResp = new StringBuffer();
                            while ((next = in.read()) != -1) {
                                tmpSvrResp.append((char)next);
                            }
                            in.close();
                            Map <String, String> newSettings = splitQuery(tmpSvrResp.toString());
                            //Apply new settings.
                            break;
                        default:
                            break;
                    }

                } catch (MalformedURLException e) {
                    log.i(e.toString());
                } finally {
                    if (c!=null) c.disconnect();
                    if (isSuccessful)
                        Intent i = new Intent(MyService.this, MyService.class);
                        i.setAction("RESTART_SERVICE_COMPONENT");
                        startService(i); //This call repeats RETRIEVE SETTINGS... action
                    } else {
                        Toast.makeText(MyService.this, "Error: setting new configuration from server!",Toast.LENGTH_SHORT);
                    }
                }
            }
        }
    }
    private Map<String, String> splitQuery(URL url) throws UnsupportedEncodingException {
        Map<String, String> query_pairs = new LinkedHashMap<String, String>();
        String query = url.getQuery();
        String[] pairs = query.split("&");
        for (String pair : pairs) {
            int idx = pair.indexOf("=");
            query_pairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"), URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
        }
        return query_pairs;
    }
}
here is a sample log...

MyService    0:BEGIN
MyService    0:RETRIEVE_SETTINGS_FROM_NET_SERVER
MyService    0:complete
MyService    1:BEGIN
MyService    1:RETRIEVE_SETTINGS_FROM_NET_SERVER
MyService    1:complete
MyService    2:BEGIN
MyService    2:RESTART_SERVICE_COMPONENT
MyService    2:complete

嗨,我搜索了但我找不到类似的东西。我有一项从服务器获取更新设置的服务。然后我重新开始计划。我的问题是每当我调用 startservice(i); 在运行新操作之前,服务将首先使用前一个操作调用意图。

4

1 回答 1

0

我刚刚复制并执行了您的代码,它对我来说工作正常。我只return START_NOT_STICKY;在末尾添加onStartCommand()。这是我的日志:

08-01 11:09:33.840: I/MyService(1333): 0:BEGIN
08-01 11:09:33.840: I/MyService(1333): 0:RETRIEVE_SETTINGS_FROM_NET_SERVER
08-01 11:09:33.861: I/MyService(1333): 0:complete
08-01 11:09:34.051: I/MyService(1333): 1:BEGIN
08-01 11:09:34.051: I/MyService(1333): 1:RESTART_SERVICE_COMPONENT
08-01 11:09:34.051: I/MyService(1333): 1:complete

我猜你在对我们隐藏的代码中做错了什么。像这儿:

//create alarmmanager code here

或在这里:

//retrieve settings from internet server here...
//parse values
//apply new service scheduled interval

请检查自己,或向我们展示代码,以便我们查看问题所在。

还向我们展示您设置 Intent 操作并首次启动此服务的代码。

于 2013-08-01T11:22:37.780 回答