繁体   English   中英

为什么计划中的代码任务执行固定率不起作用?

[英]Why the code in schedule Task Execution fixed Rate not working?

我试图在android中添加Sheduled Task的功能,以便在我想知道每当用户丢失他的互联网连接之后做一些事情,然后我想做一个警告对话框。 所以我使用Sheduled Task Execution来做这件事但是每当我把我的运行代码放在Runnable中时,Task都没有用。

重要的是我在服务类中这样做

代码是

package com.example.sid.marwadishaadi.LoginHistory;

import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
 import android.os.IBinder;
import android.provider.Settings;
   import android.support.v7.app.AlertDialog;
  import android.util.Log;
     import android.widget.Toast;

    import java.util.concurrent.Executors;

   import java.util.concurrent.ScheduledExecutorService;
  import java.util.concurrent.TimeUnit;

     import static com.bumptech.glide.gifdecoder.GifHeaderParser.TAG;

       public class OnClearFromRecentService extends Service {

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

            @Override
            public int onStartCommand(Intent intent, int flags, int startId) {
                Log.e("ClearFromRecentService-", "-----------------------------------------Service Started");
                SharedPreferences sharedPreferences=getSharedPreferences("userinfo",MODE_PRIVATE);
                SharedPreferences.Editor edtr=sharedPreferences.edit();
                String id=sharedPreferences.getString("customer_id","");
                Log.e(TAG, "onStartCommand: .........................."+id);
                if(isOnline()) {
                    Toast.makeText(this, "You are online", Toast.LENGTH_SHORT).show();
                }

                ScheduledExecutorService scheduleTaskExecutor = Executors.newScheduledThreadPool(5);
                scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
                    public void run() {                            

                        Log.i(TAG, "run: ----I'm running after 15 seconds");
                        if(isOnline()) {
                            //Dummy TODO, you can do something if you want, 
                            Toast.makeText(OnClearFromRecentService.this, "You are not online", Toast.LENGTH_SHORT).show();
                        }

                        else{
                            Log.i(TAG, "run: --- exited from here or not :::: yes ");
                            AlertDialog.Builder network =new AlertDialog.Builder(OnClearFromRecentService.this);
                            network.setTitle("No Internet");
                            network.setMessage("Please check your internet connection or go to the internet option by clicking #settings");
                            network.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    getApplicationContext().startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
                                }
                            });
                            network.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Intent intent = new Intent(Intent.ACTION_MAIN);
                                    intent.addCategory(Intent.CATEGORY_HOME);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                    startActivity(intent);
                                }
                            });
                            network.setCancelable(false);
                            AlertDialog alertDialog = network.create();
                            alertDialog.show();


                        }

                    }
                }, 0, 15, TimeUnit.SECONDS);

                return START_NOT_STICKY;
            }

        public boolean isOnline() {
    ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = conMgr.getActiveNetworkInfo();

    if(netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()){
        Toast.makeText(getApplicationContext(), "No Internet connection!", Toast.LENGTH_LONG).show();
        return false;
    }
    return true;
}



            @Override
            public void onDestroy() {
                super.onDestroy();
                Log.e("ClearFromRecentService-", "-----------------------------------Service Destroyed");
            }

            @Override
            public void onTaskRemoved(Intent rootIntent) {
                Log.e("Clearvi--------------", "---------------------------------END");
                //Code here

                stopSelf();
            }


         }

当我没有在任务中完成任务并打印单行然后工作得很好

log.e(“”,“我在15秒后运行”) - >>在日志中打印行

但是当我把我的代码放到那时它不起作用,就像代码没有运行一样。

任何人都可以推荐一些东西,对菜鸟来说真的很有帮助。

try-catch块中包装run方法。

只是一个猜测:抛出异常。 如果ScheduledExecutorService遇到异常,则会以静默方式暂停。

run方法的代码应始终由try-catch包围,以处理和吸收任何抛出的Exception。

如果您尝试在尝试catch之前创建一个Looper并将其打开,那么它将正常工作,因为您无法处理来自工作线程的UI线程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM