安卓开发,开启服务service,并持续调用get请求接口的代码每3秒钟调用一次

//在AndroidManifest.xml  添加服务
  <service android:name="com.tjtykj.xtl.AutoUpdateService" ></service>
//在MainActivity中启动服务
  public static String ACdeviceToken = "";  //要读取的变量
public class MainActivity extends CordovaActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        // enable Cordova apps to be started in the background
        Bundle extras = getIntent().getExtras();
        if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
            moveTaskToBack(true);
        }

        // Set by  in config.xml
        loadUrl(launchUrl);


        if (hasAgreedAgreement()) {
            PushAgent.getInstance(this).onAppStart();
            setDeviceToken();
        } else {
            showAgreementDialog();
        }


        Intent intent = new Intent(this, AutoUpdateService.class);
        startService(intent);
    }



//在AutoUpdateService文件中写具
体代码
package com.tjtykj.xtl;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class AutoUpdateService extends Service {


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    private int a = 1;
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        TimerTask  task= new TimerTask() {
            @Override
            public void run() {

                Log.i("MainActivity","撒旦法师打发手动阀手动阀三大发啥打法手打"+ (a++)+"-----"+ MainActivity.ACdeviceToken);

                try {
                    // 发送GET请求的URL
                    String url = "https://v0.yiketianqi.com/api?unescape=1&version=v91&appid=43656176&appsecret=I42og6Lm&ext=&cityid=&city="; // 请替换为实际的天气API接口地址
                    // 创建URL对象
                    URL apiUrl = new URL(url);
                    // 打开URL连接
                    HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
                    // 设置请求方法
                    connection.setRequestMethod("GET");
                    // 获取响应状态码
                    int responseCode = connection.getResponseCode();
                    if (responseCode == HttpURLConnection.HTTP_OK) {
                        // 读取响应内容
                        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                        String line;
                        StringBuilder response = new StringBuilder();
                        while ((line = reader.readLine()) != null) {
                            response.append(line);
                        }
                        reader.close();
                        // 处理响应结果
                        System.out.println(response.toString());
                    } else {
                        System.out.println("请求失败,错误码:" + responseCode);
                    }

                    // 关闭连接
                    connection.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
         new Timer().schedule(task,0,5*1000);
        return a;
    }

}


你可能感兴趣的:(android)