TimerTask does not stopped instantly android - android

I have two services which run periodically based on condition. In once condition the services are run with 5 second timer while in other they run with 10 mins timer.
But when I want to switch from 5 second to 10 mins timer, it does not happen instantly while it runs 4 times extra. I am canceling timer pefectly but does not seems like working..here is code
public final static Handler serviceHandler = new Handler(){
public void handleMessage(Message msg){
if(null != serviceTimer) {
Log.d(TAG, "Coming insisisi");
serviceTimer.cancel();
serviceTimer.purge();
serviceTimer = null;
}
serviceTimer = new Timer();
serviceTimer.scheduleAtFixedRate(new serviceMonitorAcc(service, target), DELAY_TIMER_TIME, TIMER_START_TIME);
serviceTimer.scheduleAtFixedRate(new serviceMonitorTrafficStats(service, target), DELAY_TIMER_TIME, TIMER_START_TIME);
Log.d(TAG, "TIMER STATTAA"+serviceTimer);
}
};
based on conditions I set the values of variable DELAY_TIMER_TIME and TIMER_START_TIME
private void setTimerInfo(String check){
if(check != null) {
if (check.equals("enable")) {
DELAY_TIMER_TIME = 0;
TIMER_START_TIME = 5000;
} else {
DELAY_TIMER_TIME = 60000;
TIMER_START_TIME = 600000;
}
}
}
the time setting working time only issue is it does not get killed instanlty and start new serviceTimer with new timer setting.
I also tried to kill timertask serviceMonitorAcc and serviceMonitorTrafficStats separatly, but did not work ...any suggestion or pointer for it...

Related

How can we call an api in every 2 minutes in android N or higher versions even when the app is closed or killed

I want a best consistent solution to call an api to update current location in every 2 minutes on Nougat and higher version. The process should not be terminated even when the app is killed or closed.
Thanks in advance
Create a services:
public class MyServices extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
startService(new Intent(this,MyServices.class));
Timer t = new Timer();
final Handler handler = new Handler();
// Timer task makes your service will repeat after every 20 Sec.
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
//Do network call here
}
});
}
};
//Starts after 20 sec and will repeat on every 20 sec of time interval.
t.schedule(doAsynchronousTask, 3000,3000); // 20 sec timer
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
}
Register the service in menifest
<service android:name=".MyServices" />
Start the service in your activity
Intent intent = new Intent(this, MyServices.class);
startService(intent);
if version > N use this
startForegroundService(intent);
Create a service and update from there.
Service will not stop after closing the application but it will get stopped if the application is force stopped.
And also if your app goes to doze mode your app cannot use Internet or GPS service from the background.
You should check out WorkManager to schedule any kind of work you want your app to do.

How to run a code repetitively after two seconds in background in android?

I want to run the given below piece of code repetitively after two seconds, I searched for the solutions many times and find this one.
The code given below is working only for sometime because Timer is destroyed by system after sometime. I also tried Alarm Manager which starts the service after 2 seconds but consumes too much memory even if the service doesn't contain any code.
Please( its been 3 continous days googling for the solution), suggest some other way to run that bunch of code repetitively in background every 2 second without being destroyed by the system.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();
final ActivityManager.RunningAppProcessInfo appProcess =runningAppProcessInfo.get(0) ;
currentAppName = appProcess.processName.toString();
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
if(temp.equalsIgnoreCase(currentAppName))
{
}
else
{ Toast.makeText(MyService.this.getApplicationContext(),appProcess.processName.toString(),Toast.LENGTH_SHORT).show();
temp=""+currentAppName;
}
}
});
for(int i=0;i<LockedApps.LockedAppsList.size();i++) {
if (appProcess.processName.toString().contains(LockedApps.preflist.get(i))) {
if (flag == 0) {
Intent x = new Intent(getApplicationContext(), LockActivity.class);
x.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(x);
}
}
}
You used Timer and AlarmManager seperately. Why don't you try combination of Timer and AlarmManager , I mean to say repet alarm every 30 seconds or 60 seconds to triger the service which in turn will start the timer.

PostDelayed not works correctly

I used postDelayed for delaying dynamic duration.
And I found it did not work correctly.
Here is my source code.
public Runnable service = new Runnable() {
public void run() {
endTimeHere = System.currentTimeMillis();
Log.d("Time",(endTimeHere-startTimeHere)/1000);
switch (step)
{
case 0:
delay = 0;
step = 1;
break;
case 1:
delay = 600; //delay 10 min = 600 sec
step = 2;
break;
case 2:
delay = 1200; //delay 20 min = 1200 sec
step = 3;
break;
case 3:
delay = 1800; //delay 30 min = 1800 secs
step = 0;
break;
default:
break;
}
startTimeHere = System.currentTimeMillis();
handler.postDelayed(service, delay*1000);
}
};
And I start and stop the handler in a BroadcastLintener.
public Handler handler = new Handler();
private BroadcastReceiver screenReceiver = new BroadcastReceiver()
{
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if(Intent.ACTION_SCREEN_ON.equals(action))
{
handler.removeCallbacks(service);
}
else if(Intent.ACTION_SCREEN_OFF.equals(action))
{
handler.post(service);
}
}
}
I'm sure that postDelayed is added in queue because the return value is true.
However, the time duration I recorded is not matching with the delay value I set.
For example, I set delay = 600 secs and the recorded duration = 958 secs.
Does anyone know why this happened?
Handlers are not perfect at firing exactly when they should. Other threads (like the UI thread) may take priority. Additionally, Android has overhead for processing the Intent. In other words, the handler may fire at 650ms due to delays in thread monitoring from the OS, but then the intent needs to be handled, the receiver instantiated, the intent processed, etc.
You may be better off sending the intent with the delay data and then have the service setup a queue and poll it frequently based on the expected delay. e.g. an event scheduled for 500ms in the future maybe should be polled every 50ms to see if the delay time has expired. While an event 10,000ms in the future can be polled at 5,000ms or 9,000ms and then increase the polling frequency as the time approaches.

What interval should I use between each WiFi scan on Android?

I need to perform Wifi scans at regular intervals. I am encountering a problem when the time interval is set to 1-2 seconds. It seems like I am not getting any ScanResult. Is there a minimum amount of time to set so that the WifiManager is able to perform a successful WiFi scan?
Here is the code. I am using a Service to do the Wifi scan:
public class WifiScanning extends Service{
private static final String TAG = "WifiScanning";
private Timer timer;
public int refreshRate, numberOfWifiScan, wifiScanGranularity;
WifiReceiver receiverWifi = new WifiReceiver();
WifiManager wifi;
StringBuilder sb;
List<ScanResult> wifiList;
List<APData> apdataList;
List<List<APData>>surveyData;
private TimerTask updateTask = new TimerTask() {
#Override
public void run() {
Log.i(TAG, "Timer task doing work");
wifi.startScan();
}
};
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "Service creating");
//retrieve the mapRefreshRate from config.xml
XMLOperations test = new XMLOperations();
Configuration config = new Configuration();
config = test.saxXmlParsing(this, 1);
if(config==null)
config = test.saxXmlParsing(this, 2);
refreshRate = Integer.parseInt(config.getMapRefreshRate());
numberOfWifiScan = Integer.parseInt(config.getNumberOfWifiScan_Positioning());
wifiScanGranularity = Integer.parseInt(config.getWifiScanGranularity_Positioning());
timer = new Timer();
Log.i(TAG, "Refresh Rate: "+ String.valueOf(refreshRate));
timer.schedule(updateTask, 0, refreshRate);
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
registerReceiver(receiverWifi, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
#Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "Service destroying");
unregisterReceiver(receiverWifi);
if (timer != null){
timer.cancel();
timer.purge();
timer = null;
}
}
class WifiReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
sb = new StringBuilder();
wifiList = wifi.getScanResults();
String ap_ssid;
String ap_mac;
Double ap_rssi;
for(int i = 0; i < wifiList.size(); i++){
ap_ssid = wifiList.get(i).SSID;
ap_mac = wifiList.get(i).BSSID;
ap_rssi = Double.valueOf(wifiList.get(i).level);
APData ap = new APData(ap_ssid,ap_mac,ap_rssi);
apdataList.add(ap);
sb.append(" " + (wifiList.get(i).SSID).toString());
sb.append(" " + (wifiList.get(i).BSSID).toString());
sb.append((" " + String.valueOf(wifiList.get(i).level)));
sb.append("\n");
}
Log.d(TAG, sb.toString());
for(int i=1; i<=numberOfWifiScan; i++){
surveyData.add(apdataList);
}
}
}
}
However, I seem to get Nullpointer at this line: apdataList.add(ap);. So I wonder whether the interval is too short, which causes ScanResult to be empty?
EDIT after you posted your code:
apdataList does not seem to be initialized in onCreate()
add this to onCreate():
apdataList = new List<APData>();
Minimum scanning delay
I think that there is no absolute minimum scanning delay. It depends too much on the hardware performances.
My advice is that you add a 'As Fast As Possible' option to your preferences then use an asynchronous loop that relaunch a scan as soon as new results are found (see the code snippet below, it was updated to suit your needs). This way, it will only be limited by hardware performances.
Also you can poll the ScanResults using WifiManager.getScanResults() The recommended way is to launch WifiManager.startScan() and set up a BroadcastReceiver for WifiManager.SCAN_RESULTS_AVAILABLE_ACTION to be notified as soon as the scan results are ready.
Here's a sample code (borrowed from here and adapted to your needs):
IntentFilter i = new IntentFilter();
i.addAction (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(new BroadcastReceiver(){
public void onReceive(Context c, Intent i){
// Code to execute when SCAN_RESULTS_AVAILABLE_ACTION event occurs
WifiManager w = (WifiManager) c.getApplicationContext().getSystemService(Context.WIFI_SERVICE); //Use getApplicationContext to prevent memory leak
myScanResultHandler(w.getScanResults()); // your method to handle Scan results
if (ScanAsFastAsPossible) w.startScan(); // relaunch scan immediately
else { /* Schedule the scan to be run later here */}
}
}, i );
// Launch wifiscanner the first time here (it will call the broadcast receiver above)
WifiManager wm = (WifiManager)getApplicationContext.getSystemService(Context.WIFI_SERVICE);
boolean a = wm.startScan();
From Android 8 and higher, the limit is 4 times in 2 minutes.
So you could scan 4 times with 1 second of delay in between. But you would not get any further scan results for the next 126 seconds.
So the fastest interval where every scan is successful would be every 30 seconds.

Timer and TimerTask in Android

I need a timer for my program. I have written it and it works fine on PC in emulalator program (Android 1.5/2.2). But it doesn't work on the real device (Android 1.5). What am I doing wrong?
TimerTask task = new TimerTask() {
public void run() {
if (condition) {
myFunc();
} else {
this.cancel();
}
}
};
Timer timer = new Timer();
timer.schedule(task, 500, 85);
You need to cancel() timer not the timer task.

Categories

Resources