I am trying to provide a back function to the browser launched in my program.
My tablet doesn't have a bottom bar
So I implemented it this way.
import android.app.Activity;
import android.app.ActivityManager;
import android.app.Instrumentation;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Toast;
import java.util.List;
import kr.co.green_.ConfirmDialog;
import kr.co.green_.ExampleAccessService;
import kr.co.green_.R;
import static android.content.ContentValues.TAG;
public class CloseBtnServiceForGallery extends Service {
private WindowManager manager;
private RelativeLayout parentlayout;
private View mViewClose;
private ImageButton btnClose;
private ImageButton btnBack;
private ExampleAccessService exService;
@Override
public IBinder onBind(Intent paramIntent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "CloseBtnServiceForGallery");
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
this.mViewClose = inflater.inflate(R.layout.close_btn_gallery, null);
this.exService = new ExampleAccessService();
this.exService.onServiceConnected();
btnClose = (ImageButton)this.mViewClose.findViewById(R.id.btnCloseGallery);
btnBack = (ImageButton)this.mViewClose.findViewById(R.id.btnBack);
btnClose.setOnClickListener(mCloseBtnClickListener);
btnBack.setOnClickListener(mCloseBtnClickListener);
this.manager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = null;
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
} else {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
}
params.gravity = Gravity.LEFT | Gravity.BOTTOM ;
params.verticalMargin = 0.1f ;
this.manager.addView(mViewClose, params);
}
Button.OnClickListener mCloseBtnClickListener = new Button.OnClickListener() {
public void onClick(View v) {
try{
Log.e("try close", "v? : " + v);
switch (v.getId()) {
case R.id.btnCloseGallery:
// something
break;
case R.id.btnBack:
try{
//exService.onServiceConnected(); This accessibility function requires system privileges so it can't be used
ActivityManager activity_manager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> task_info = activity_manager.getRunningTasks(9999);
ComponentName componentInfo = task_info.get(0).topActivity;
ActivityInfo activityInfo = tryGetActivity(componentInfo);
// What should I do?
// I want to tell the current topmost activity to go back when a button click is detected in my plotted icon service.
}
catch(Exception e){
Log.e("error", ":" + e);
}
break;
default:
break;
}
}catch(Exception e){
Log.e("try close click","error :" + e);
}
}
};
private ActivityInfo tryGetActivity(ComponentName componentName) {
try {
return getPackageManager().getActivityInfo(componentName, 0);
} catch (PackageManager.NameNotFoundException e) {
return null;
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (this.manager != null) {
if (this.mViewClose != null) {
this.manager.removeView(this.mViewClose);
this.mViewClose = null;
}
this.manager = null;
}
}
}
The picture below uses Instrumentation, but also requires system privileges.
mycode
My program runs in the background
Floats the icon on the top screen.
myapp
But this method requires system privileges.
I want to implement it as an app without system permission
Thanks
//Declare the Value private boolean Back = false;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (Back) {
setResult(RESULT_OK);
finish();
} else {
setResult(RESULT_CANCELED);
finish();
}
}
return super.onKeyDown(keyCode, event);
}
------------------ OR ------------------
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
startActivity(new Intent(SecActivity.this, FirstActivity.class));
}