0

An Android Java app developed in Android Studio should track the gps location and call a phone number to trigger a street barrier. The app uses SCREEN_DIM_WAKE_LOCK to keep the device running, which is running fine. I use a ACTION_CALL intent to call the barrier, which is also working. The problem to solve is: After the call is done, the wake lock is not working anymore and the device falls asleep after about 10-20 seconds. The device does NOT fall asleep when no call intent is made so I am sure it has to do with the phone call intent. The gps location tracking is done with an inner private class that implements a location listener. The location listener creates an instance of a new class derived from ASyncTask to post the gps data to a web interface and to check geofencing, that is: place a call.

I tried to add flags like FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TOP, I also put a launchMode singleTop in the Manifest for the MainActivity, but I could not find a solution yet.

public class MainActivity extends AppCompatActivity {

private LocationManager locationManager;
private LocationListener locationListener;
protected static PowerManager.WakeLock mWakeLock;
protected Handler stopCallHandler = new Handler();


@Override
protected void onCreate(Bundle savedInstanceState) {
  final PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
  mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "MyApp:KeepDeviceAwake");
  this.locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  this.locationListener = new MyLocationListener();
}

public void callPhoneNumber(String number) {
  Intent intent = new Intent(Intent.ACTION_CALL);
  intent.setData(Uri.parse("tel:" + number));
  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  this.startActivity(intent);
  // Kill the Call after x seconds
  stopCallHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
      stopCall();
    }
  }, 5000);
}

private class LocationChangedTasks extends AsyncTask<Location, String, Void> {
  @Override
  protected Void doInBackground(Location... locations) {
    // Post location data to internet api for geotracking vehicle (therefore, asynctask is used)
    // get location and check if current location is in geofence
    // if yes, place call
    callPhoneNumber(number);
  }
}

private class MyLocationListener implements LocationListener {
  @Override
  public void onLocationChanged(Location loc) {
    LocationChangedTasks locationchangedtask = new LocationChangedTasks();
    locationchangedtask.execute(loc);
  }
}

public void stopCall() {
  // Uses code from
  // https://stackoverflow.com/questions/599443/how-to-hang-up-outgoing-call-in-android
  // to kill the call
}

}

I have no error messages, I only see that the system falls asleep if a phone call is placed

Sharon Joshi
  • 498
  • 7
  • 19
Nexave
  • 3
  • 2

2 Answers2

0

You can try this to avoid falling your screen asleep:

1) In AndroidManifest.xml

<activity
     android:name=".MainActivity"
     android:turnScreenOn="true"
     ...>
.......

2) In MainActivity.java

@Override
protected void onResume() {
    super.onResume();
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
Saif
  • 397
  • 1
  • 9
  • I like android:turnScreenOn=true as it seems to prevent the display to dim. I cannot really say if this leads to solving the problem as our HTC testing device never ever had the issue of loosing the wake lock. The production device is from Samsung and I will have them test the new version of the app where your code is included. – Nexave Oct 14 '19 at 07:42
0

this happens because while your activity goes back ground, the Back stack will be cleared but when you came back to your activity, It is no recreated. so for that add these into your AndroidManifest.xml

 <activity
        android:name=".MainActivity"
        android:launchMode="singleInstance"
        android:autoRemoveFromRecents="true" />

this will help to keep your activity in background until you call finish();

and also include the functions that you need to recall while your activity is restarting inside

onResume()

@Override
protected void onResume() {
        super.onResume();
        otected void onCreate(Bundle savedInstanceState) {
        final PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK,"MyApp:KeepDeviceAwake");
        this.locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        this.locationListener = new MyLocationListener();
    } 
Sharon Joshi
  • 498
  • 7
  • 19
  • Thank you for your post. It seems as if this answer is the solution as it sounds clear and obvious to me - though I cannot clearly confirm (yet). The production device is from Samsung and my HTC testing device never ever hat the issue with the wake lock (I don't know why). I will have the new version of the app where your code is included being tested. If it works, I will let you know. – Nexave Oct 14 '19 at 07:45