androidandroid-activitywakelock

Wakelock lost after phone call intent


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


Solution

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