我现在正在尝试制作一个应用程序,其中活动被覆盖在屏幕上(当按下主页按钮时)。这些活动仍然需要起作用而不仅仅是一种观点。
例如,此活动中有一个启动摄像头的按钮。通常它可以工作,但当活动被用作叠加时,按钮不起作用(因此我认为只有视图处于活动状态)。
提前致谢:)
ManageActivity:
private static final String TAG = "CallCamera";
private static final int CAPTURE_IMAGE_ACTIVITY_REQ = 0;
Uri fileUri = null;
@Override
protected void onCreate(Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manage);
Button callCameraButton = (Button) findViewById(R.id.cameraButton);
callCameraButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = getOutputPhotoFile();
fileUri = Uri.fromFile(getOutputPhotoFile());
i.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQ);
}
});
Button invite = (Button) findViewById(R.id.inviteButton);
invite.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
toggleService();
}
});
}
public void endEvent(View view) {
super.finish();
}
private File getOutputPhotoFile() {
File directory = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), getPackageName());
if (!directory.exists()) {
if (!directory.mkdirs()) {
Log.e(TAG, "Failed to create storage directory.");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyMMdd_HHmmss").format(new Date());
return new File(directory.getPath() + File.separator + "IMG_"
+ timeStamp + ".jpg");
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQ) {
if (resultCode == RESULT_OK) {
Uri photoUri = null;
if (data == null) {
// A known bug here! The image should have saved in fileUri
Toast.makeText(this, "Image saved successfully",
Toast.LENGTH_LONG).show();
photoUri = fileUri;
} else {
photoUri = data.getData();
Toast.makeText(this, "Image saved successfully in: " + data.getData(),
Toast.LENGTH_LONG).show();
}
// showPhoto(photoUri);
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Callout for image capture failed!",
Toast.LENGTH_LONG).show();
}
}
}
private void toggleService() {
Intent intent = new Intent(this, OverlayService.class);
// Try to stop the service if it is already running
// Otherwise start the service
if (!stopService(intent)) {
startService(intent);
}
}
}
OverlayService:
@Override
public IBinder onBind(Intent i) {
return null;
}
@Override
public void onCreate( ) {
super.onCreate();
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_PHONE,
0 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
ViewGroup oView = (ViewGroup) inflater.inflate(R.layout.activity_manage, null);
wm.addView(oView, params);
}
}
我已拥有用户权限,并使用OverlayService作为清单
中的服务