【发布时间】:2021-07-21 07:07:53
【问题描述】:
我正在尝试为在我的程序中启动的浏览器提供返回功能。
我的平板电脑没有底栏
所以我是这样实现的。
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;
}
}
}
下图使用 Instrumentation,但也需要系统权限。
mycode
我的程序在后台运行
在顶部屏幕上浮动图标。
myapp
但是这种方法需要系统权限。
我想在没有系统权限的情况下将其实现为应用程序
谢谢
【问题讨论】:
-
你不能只打电话给
onBackPressed()吗? -
我不知道如何在另一个活动上调用 onbackpressed()。 ??????
-
请在帖子中发布您的代码,而不是作为单独的链接。此外,您需要更好地描述您的问题。从您发送的应用程序屏幕截图来看,您似乎想用自定义布局替换系统的后退按钮,对吧?如果是这样,您的问题确实缺乏描述,您应该考虑进一步解释。
-
你理解的是正确的,谢谢我将代码添加到问题中
标签: android system onbackpressed