Why is the code in the schedule Task Fulfillment of a fixed rate Does not work?

I am trying to add the Sheduled Task function in android to do something after some time, as if I want to know when the user will lose his Internet connection, then I want to make a warning dialog. Therefore, I do this using the Sheduled Task Execution, but whenever I ran my startup code in Runnable, Task didnot work.

It is important that I do this in the service class

CODE IS

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(); } } 

when I did nothing in a given task and did not print one line, then I work fine, as

log.e ("," I work after 15 seconds ") →> print a line in the log

but when I put my code, then it does not work, for example, the code does not run.

Can anyone suggest something, this will be really useful for noob.

+5
source share
1 answer

Wrap your run method in a try-catch .

Just guess: an exception is thrown. A ScheduledExecutorService silently stops if it encounters an Exception.

The code of the execution methods should always be surrounded by try-catch to handle and try-catch any thrown exception.

If you try to create a Looper before you try to catch and make it open, then it will work fine, because you cannot process the UI thread from the workflow.

+2
source

Source: https://habr.com/ru/post/1269321/


All Articles