Технически это возможно, но для этого потребуется также сохранить все выделенные системой ресурсы состояния - например, дескрипторы файлов, а затем восстановить их. Так что это сложная задача.
Самый простой способ добиться того, чего вы хотите - это использовать виртуальную машину, такую как VMWare. Когда вы приостанавливаете его, вы фактически сохраняете все состояние машины вместе со всеми запущенными программами.
Вот измененная версия ответа Siddharth Garg. Это 100% работы времени.
import android.content.Context;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
public class SoftInputService implements Runnable {
private static final String TAG = SoftInputService.class.getSimpleName();
private static final int INTERVAL_MS = 100;
private Context context;
private View targetView;
private Handler handler;
public SoftInputService(Context context, View targetView) {
this.context = context;
this.targetView = targetView;
handler = new Handler(Looper.getMainLooper());
}
@Override
public void run() {
if (context == null || targetView == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (!targetView.isFocusable() || !targetView.isFocusableInTouchMode()) {
Log.d(TAG,"focusable = " + targetView.isFocusable() + ", focusableInTouchMode = " + targetView.isFocusableInTouchMode());
return;
} else if (!targetView.requestFocus()) {
Log.d(TAG,"Cannot focus on view");
post();
} else if (!imm.showSoftInput(targetView, InputMethodManager.SHOW_IMPLICIT)) {
Log.d(TAG,"Unable to show keyboard");
post();
}
}
public void show() {
handler.post(this);
}
public static void hide(Context context, IBinder windowToekn) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(windowToekn, 0);
}
protected void post() {
handler.postDelayed(this, INTERVAL_MS);
}
}
Использование:
// To show the soft input
new SoftInputService(context, theEditText).show();
// To hide the soft input
SoftInputService.hide(context, theEditText.getWindowToken());