Created
February 1, 2013 07:27
-
-
Save jschmid/4689899 to your computer and use it in GitHub Desktop.
Secure Android Activity that does not allow clicks when in the background, and prevents screenshots.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pro.schmid.android.secureapp; | |
import android.app.Activity; | |
import android.graphics.Color; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.view.ViewGroup; | |
import android.view.ViewGroup.LayoutParams; | |
import android.view.WindowManager; | |
public class SecureActivity extends Activity { | |
private ViewGroup mRootView; | |
private View mBlackScreen; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
// Prevent screenshots | |
// (But not on Gingerbread) | |
if (android.os.Build.VERSION.SDK_INT != android.os.Build.VERSION_CODES.GINGERBREAD && android.os.Build.VERSION.SDK_INT != android.os.Build.VERSION_CODES.GINGERBREAD_MR1) { | |
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); | |
} | |
// The root view must have the flag android:filterTouchesWhenObscured set to true. | |
setContentView(R.layout.activity_main); | |
// Init the black screen | |
mBlackScreen = new View(this); | |
LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); | |
mBlackScreen.setLayoutParams(layoutParams); | |
mBlackScreen.setBackgroundColor(Color.BLACK); | |
mBlackScreen.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// Catch click and do nothing | |
} | |
}); | |
// Get the root view | |
mRootView = (ViewGroup) getWindow().findViewById(android.R.id.content); | |
} | |
@Override | |
protected void onPause() { | |
addBlackScreen(); | |
super.onPause(); | |
} | |
@Override | |
protected void onResume() { | |
removeBlackScreen(); | |
super.onResume(); | |
} | |
private void addBlackScreen() { | |
mRootView.addView(mBlackScreen); | |
} | |
private void removeBlackScreen() { | |
mRootView.removeView(mBlackScreen); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment