在Android Studio中开发的Android Java应用程序应跟踪GPS位置并拨打电话号码以触发路障。该应用程序使用SCREEN_DIM_WAKE_LOCK来保持设备正常运行。我使用ACTION_CALL意图调用该障碍,该障碍也正在起作用。要解决的问题是:呼叫完成后,唤醒锁不再起作用,并且设备在大约10到20秒后进入睡眠状态。当没有呼叫意图时,设备不会进入睡眠状态,因此我确定它与电话意图有关。gps位置跟踪是通过实现位置侦听器的内部私有类完成的。位置侦听器创建一个从ASyncTask派生的新类的实例,以将gps数据发布到Web界面并检查地理围栏,即:发出呼叫。
我试图添加诸如FLAG_ACTIVITY_NEW_TASK和FLAG_ACTIVITY_CLEAR_TOP之类的标志,我还在MainActivity的清单中添加了一个launchMode singleTop,但我找不到解决方案。
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
}
}
我没有错误消息,如果打了电话,我只会看到系统进入睡眠状态
您可以尝试执行此操作以避免屏幕进入睡眠状态:
之所以这样,是因为当您的活动退后时,将清除“后退”堆栈,但是当您返回活动时,不会重新创建它。所以为此将它们添加到您的AndroidManifest.xml