Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javaandroid意图服务只工作一次_Java_Android_Android Intent_Android Service - Fatal编程技术网

javaandroid意图服务只工作一次

javaandroid意图服务只工作一次,java,android,android-intent,android-service,Java,Android,Android Intent,Android Service,我想创建一个intent服务,该服务在整个应用程序中(在所有活动中)始终工作,并执行任务。我使用处理器。我在活动中开始了一项服务。我看到只有一次服务工作(做it任务)为什么? 这是我的服务: public class SenderXML extends Service { public static boolean running = false; private Timer timer = new Timer(); private SendXMLTask sendXM

我想创建一个intent服务,该服务在整个应用程序中(在所有活动中)始终工作,并执行任务。我使用处理器。我在活动中开始了一项服务。我看到只有一次服务工作(做it任务)为什么? 这是我的服务:

public class SenderXML extends Service {

    public static boolean running = false;
    private Timer timer = new Timer();
    private SendXMLTask sendXMLTask;
    private Context context;

    static String convertStreamToString(java.io.InputStream is) {
        java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        Gson gson = new Gson();
        MainActivity.xmlList = null;
        MainActivity.xmlList = new ArrayList<>();
        SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("TAG", Context.MODE_PRIVATE);
        List<String> productFromShared1 = new ArrayList<>();
        String jsonPreferences1 = sharedPref.getString("TAG1", "");

        Type type1 = new TypeToken<List<String>>() {
        }.getType();
        productFromShared1 = gson.fromJson(jsonPreferences1, type1);
        if (productFromShared1 != null)
            MainActivity.xmlList.addAll(productFromShared1);
        Log.e("tworzenie serwisu ", "tworzenie");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("Dziełanie serwisu ", "Dziełanie");
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                Log.e("Dziełanie serwisu 1111", "Dziełanie 111");
                if (!MainActivity.xmlList.isEmpty()) {
                    if (NetworkUtil.isNetworkAvailable(context)) {
                        if (!running) {
                            sendXMLTask = new SendXMLTask();
                            sendXMLTask.execute();
                        }
                    }
                }
            }
        }, 1000 * 60 * 2);

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        if (running) {
            timer.cancel();
            sendXMLTask = new SendXMLTask();
            sendXMLTask.cancel(true);
            running = false;
        }
        Log.e("service ", "nie działa");
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    class SendXMLTask extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... voids) {
            Log.e("xml ", MainActivity.xmlList.size() +"");
            running = true;
            HttpClient httpclient = HttpClientUtil.getHttpClient(getApplicationContext());
            HttpPost httppost = new HttpPost(Util.getServerUrl(getApplicationContext()) + "/SetData");
            ArrayList<NameValuePair> namevaluepairs = new ArrayList<>(2);
            namevaluepairs.add(new BasicNameValuePair("token", String.valueOf(E_Gps.TOKEN)));
            namevaluepairs.add(new BasicNameValuePair("xml", MainActivity.xmlList.get(0)));
            if (MainActivity.xmlList.size() > 0) {
                MainActivity.btTransfer.setBackgroundColor(Color.YELLOW);
            }
            try {
                httppost.setEntity(new UrlEncodedFormEntity(namevaluepairs));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            HttpResponse response = null;
            try {
                response = httpclient.execute(httppost);
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
            InputStream responseInputStream = null;
            try {
                responseInputStream = new BufferedInputStream(response.getEntity().getContent());
                try {
                    int tt = ResponseParser.getResponseType(responseInputStream);
                    if (tt == 0) {
                        return null;
                    }
                } catch (EmptyResponseException | UnknownAnswerName | XmlPullParserException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            Log.e("xml", MainActivity.xmlList.get(0));
            Log.e("xml wys ", convertStreamToString(responseInputStream));
            return convertStreamToString(responseInputStream);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s != null) {
                MainActivity.xmlList.remove(0);
                Gson gson = new Gson();
                String jsonCurProduct = gson.toJson(MainActivity.xmlList);
                SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("TAG", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString("TAG1", jsonCurProduct);
                editor.apply();
            }
            if (!MainActivity.xmlList.isEmpty()) {
                sendXMLTask = new SendXMLTask();
                sendXMLTask.execute();
            } else {
                MainActivity.btTransfer.setBackgroundColor(Color.GREEN);
            }
            running = false;
        }
    }
}
公共类SenderXML扩展服务{
公共静态布尔运行=false;
专用计时器=新计时器();
私有SendXMLTask SendXMLTask;
私人语境;
静态字符串convertStreamToString(java.io.InputStream为){
java.util.Scanner s=新的java.util.Scanner(is).useDelimiter(“\\A”);
返回s.hasNext()?s.next():“”;
}
@凌驾
public void onCreate(){
super.onCreate();
context=getApplicationContext();
Gson Gson=新的Gson();
MainActivity.xmlList=null;
MainActivity.xmlList=新的ArrayList();
SharedReferences SharedReference=getApplicationContext().GetSharedReferences(“标记”,Context.MODE\u PRIVATE);
List productFromShared1=新的ArrayList();
字符串jsonPreferences1=sharedPref.getString(“TAG1”,”);
类型type1=新的TypeToken(){
}.getType();
productFromShared1=gson.fromJson(jsonPreferences1,type1);
if(productFromShared1!=null)
MainActivity.xmlList.addAll(productFromShared1);
Log.e(“tworzenie serwisu”,“tworzenie”);
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
Log.e(“Dziełanie serwisu”、“Dziełanie”);
Handler=newhandler();
handler.postDelayed(新的Runnable(){
@凌驾
公开募捐{
Log.e(“Dziełanie serwisu 1111”、“Dziełanie 111”);
如果(!MainActivity.xmlList.isEmpty()){
if(NetworkUtil.isNetworkAvailable(上下文)){
如果(!正在运行){
sendXMLTask=新建sendXMLTask();
sendXMLTask.execute();
}
}
}
}
}, 1000 * 60 * 2);
返回super.onStartCommand(intent、flags、startId);
}
@凌驾
公共空间{
如果(正在运行){
timer.cancel();
sendXMLTask=新建sendXMLTask();
sendXMLTask.cancel(true);
运行=错误;
}
Log.e(“服务”,“nie działa”);
super.ondestory();
}
@可空
@凌驾
公共IBinder onBind(意向){
返回null;
}
类SendXMLTask扩展了AsyncTask{
@凌驾
受保护的字符串背景(无效…无效){
Log.e(“xml”,MainActivity.xmlList.size()+”);
运行=真;
HttpClient HttpClient=HttpClientUtil.getHttpClient(getApplicationContext());
HttpPost-HttpPost=newhttppost(Util.getServerUrl(getApplicationContext())+“/SetData”);
ArrayList namevaluepairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“token”,String.valueOf(E_Gps.token));
添加(新的BasicNameValuePair(“xml”,MainActivity.xmlList.get(0));
if(MainActivity.xmlList.size()>0){
MainActivity.btTransfer.setBackgroundColor(颜色.黄色);
}
试一试{
setEntity(新的UrlEncodedFormEntity(namevaluepairs));
}捕获(不支持的编码异常e){
e、 printStackTrace();
}
HttpResponse响应=null;
试一试{
response=httpclient.execute(httppost);
}捕获(IOE异常){
e、 printStackTrace();
返回null;
}
InputStream响应InputStream=null;
试一试{
responseInputStream=新的BufferedInputStream(response.getEntity().getContent());
试一试{
int tt=ResponseParser.getResponseType(responseInputStream);
如果(tt==0){
返回null;
}
}catch(EmptyResponseException | UnknownAnswerName | XmlPullParserException e){
e、 printStackTrace();
}
}捕获(IOE异常){
e、 printStackTrace();
}
Log.e(“xml”,MainActivity.xmlList.get(0));
e(“xml wys”,convertStreamToString(responseInputStream));
返回convertStreamToString(responseInputStream);
}
@凌驾
受保护的void onPostExecute(字符串s){
super.onPostExecute(s);
如果(s!=null){
MainActivity.xmlList.remove(0);
Gson Gson=新的Gson();
字符串jsonCurProduct=gson.toJson(MainActivity.xmlList);
SharedReferences SharedReference=getApplicationContext().GetSharedReferences(“标记”,Context.MODE\u PRIVATE);
SharedPreferences.Editor=sharedPref.edit();
putString(“TAG1”,jsonCurProduct);
editor.apply();
}
如果(!MainActivity.xmlList.isEmpty()){
sendXMLTask=新建sendXMLTask();
sendXMLTask.execute();
}否则{
MainActivity.btTransfer.setBackgroundColor(Color.GREEN);
}
运行=错误;
}
}
}

您是否尝试过使用Alarm Manager定期启动意向服务? 如果您还没有尝试过,您可以通过此链接了解更多信息


您可以使用Alarm Manager定期启动意向服务。

您是否尝试过使用Alarm Manager定期启动意向服务? 如果您还没有尝试过,您可以通过此链接了解更多信息

您可以使用Alarm Manager定期启动意向服务。

当您校准