FLAG_SECURE Not preventing Screen shot in Android 12 - android

FLAG_SECURE Not Working with Android 12.
Code is given below which is used in my APP. It work till Android 11 but on Android 12 I can Take screen shots(when app is either in foreground or while App is in the(displayed in Task list) background . Please Suggest If there is any other work around available.
No one should be able to take screen shot while App is Background but displayed in open Apps List.
public class MyApplicationContext extends Application {
private Context context;
public void onCreate() {
super.onCreate();
context = getApplicationContext();
setupActivityListener();
}
private void setupActivityListener() {
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
#Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE); }
#Override
public void onActivityStarted(Activity activity) {
}
#Override
public void onActivityResumed(Activity activity) {
}
#Override
public void onActivityPaused(Activity activity) {
}
#Override
public void onActivityStopped(Activity activity) {
}
#Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
#Override
public void onActivityDestroyed(Activity activity) {
}
});
}
}

Related

Music player - activity lifecycle

I have really hard time with background music in my app. I just want to play music in all activities - when I press home button I want to stop music. I want "stop or play" music button in all activities, but couldnĀ“t make it work.
So I decided to make embarassing choice - play it only in 1 activitiy by
onCreate
backgroundmusic = MediaPlayer.create(StoryActivity.this, R.raw.creepy_music);
backgroundmusic.start();
onPause
#Override
protected void onPause() {
super.onPause();
backgroundmusic.release();
finish();
}
Can you please help me with easy activity lifecycle? So when a user presses home button - music will stop. When he will come back to app - music will be restored and this activity too (it is not MainActivity)
Thank you, guys
Here are the different LifeCycle states. Now to your answer,
#Override
protected void onStop() {
super.onStop();
backgroundmusic.pause();
length = backgroundmusic.getCurrentPosition();
}
#Override
protected void onResume() {
super.onResume();
backgroundmusic.seekTo(length);
backgroundmusic.start();
}
In public class MainActivity extends AppCompatActivity, It's the AppCompatActivity that is the main source of an Activity's functionality, hence in the above methods like super.onResume(); and super.onStop(); super refers to the AppCompatActivity class
assume that you can get the music service in the Application ,you may be looking for this :
public class MyApp extends Application{
MusicService musicService;
#Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
#Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
}
#Override
public void onActivityStarted(Activity activity) {
}
#Override
public void onActivityResumed(Activity activity) {
if(musicService==null) return;
if(!musicService.isPlaying()){
musicService.play();
}
}
#Override
public void onActivityPaused(Activity activity) {
if(musicService==null) return;
if(musicService.isPlaying()){
musicService.pause();
}
}
#Override
public void onActivityStopped(Activity activity) {
}
#Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
#Override
public void onActivityDestroyed(Activity activity) {
}
});
}
}
Hope this helps

How to play music only in foreground

I have trouble to play music across all activity, I had implement service and handle onPause to stop the music when going to background (not visible to user).
The problem is when i navigate to another activity, the onPause method is called that make my music stop. How to fix this issue? I need to play my music across all my activity in foreground only and dont wan to play it when the application in the background. Appeciate anyone know how to solve this.
This is my base activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startIntent = new Intent(getApplicationContext(), MusicService.class);
if(binder==null){
bindService(startIntent,this, Context.BIND_AUTO_CREATE);
}
}
#Override
protected void onDestroy() {
super.onDestroy();
if(binder.getMyService()!=null){
stopService(startIntent);
unbindService(this);
}
}
#Override
protected void onStart() {
super.onStart();
}
#Override
protected void onPause() {
super.onPause();
if(binder!=null) {
binder.getMyService().pauseMusic();
}
}
#Override
protected void onResume() {
super.onResume();
if(binder!=null){
binder.getMyService().resumeMusic();
}
}
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
binder = (MusicService.Binder) service;
}
#Override
public void onServiceDisconnected(ComponentName name) {
}
this is my mainactivity extends base activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startIntent = new Intent(MainActivity.this, MusicService.class);
startService(startIntent);
}
#Override
protected void onDestroy() {
super.onDestroy();
}
public void how(View view) {
Intent intent = new Intent(this, AboutActivity.class);
this.startActivity(intent);
}
You could implement code that will track current activity in your app. (Good example: How to get current activity)
And stop music only when "current activity" is null.
PS: depending on your implementation of tracking current activity you might want to check current activity not onPause() right away but with some delay .
Don't stop the audio play in onPause() as Harin Kaklotar suggested. You can use his method of onDestroy() or you can have it in an asynchronous task and turn off the sound by using surfaceDestroyed(). You can refer the android documentation of AudioManager if you need anything else of the sort.
https://developer.android.com/reference/android/media/AudioManager.html
Hope I Helped :D
UPDATE
You can create a system to check if your app is in the foreground or background. This involves counting how many activities are paused. Add this to all your activities:
#Override
protected void onPause() {
super.onPause();
MainActivity.Pause++;
}
#Override
protected void onResume() {
super.onResume();
MainActivity.Pause--;
}
And in your MainActivty,
if (Pause == NUMBER_OF_ACTIVITIES) {
//PAUSE MUSIC HERE
}
You have to use ActivityLifecycleCallbacks to tell if you app is in the foreground or background.
For example:
public class DummyActivityLifecycleCallbacks implements ActivityLifecycleCallbacks {
#Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { }
#Override public void onActivityStarted(Activity activity) { }
#Override public void onActivityResumed(Activity activity) { }
#Override public void onActivityPaused(Activity activity) { }
#Override public void onActivityStopped(Activity activity) { }
#Override public void onActivityDestroyed(Activity activity) { }
#Override public void onActivitySaveInstanceState(Activity activity, Bundle savedInstanceState) { }
}
public class OnApplicationForegroundBackgroundEnterCallbacks extends DummyActivityLifecycleCallbacks {
private Listener listener;
private int activityStartStopCounter = 0;
public interface Listener {
public void onEnterForeground();
public void onEnterBackground();
}
public OnApplicationForegroundBackgroundEnterCallbacks(Listener listener) {
this.listener = listener;
}
#Override public void onActivityStarted(Activity activity) {
if(++activityStartStopCounter == 1) listener.onEnterForeground();
}
#Override public void onActivityStopped(Activity activity) {
if(--activityStartStopCounter == 0) listener.onEnterBackground();
}
}
Then in Application.onCreate call:
registerActivityLifecycleCallbacks(new OnApplicationForegroundBackgroundEnterCallbacks(this));

Start a service when activity is opened

How do i start a service when an activity is opened, I am using intent but the service isn't starting at all. Music is meant to be played when a certain page is opened, but no service is running at all.
This is the code i am using to start the service on the MainPage class.
public class BackgroundMusic extends Service implements Application.ActivityLifecycleCallbacks, Runnable {
MediaPlayer player;
private static final Handler uiHandler = new Handler(Looper.getMainLooper());
#Override
public void onCreate() {
super.onCreate();
player = MediaPlayer.create(this, R.raw.backgroundmusic);
player.setLooping(true);
player.setVolume(100, 100);
player.start();
Intent BackgrndMusic = new Intent(this, MainPage.class);
startService(BackgrndMusic);
getApplication().registerActivityLifecycleCallbacks(this);
}
#Override
public void onDestroy() {
super.onDestroy();
// stop listening for activities
getApplication().unregisterActivityLifecycleCallbacks(this);
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onActivityStarted(Activity activity) {
// do not destroy this service
uiHandler.removeCallbacks(this);
}
#Override
public void onActivityStopped(Activity activity) {
// if an activity don't show up in 1 second, destroy this service
uiHandler.postDelayed(this, 1000);
}
#Override
public void run() {
// no more activity, destroy this service
stopSelf();
}
#Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
Intent BackgrndMusic = new Intent(BackgroundMusic.this, MainPage.class);
startService(BackgrndMusic);
}
#Override
public void onActivityResumed(Activity activity) {
Intent BackgrndMusic = new Intent(BackgroundMusic.this, MainPage.class);
startService(BackgrndMusic);
}
#Override
public void onActivityPaused(Activity activity) {
Intent BackgrndMusic = new Intent(BackgroundMusic.this, MainPage.class);
stopService(BackgrndMusic);
}
#Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
#Override
public void onActivityDestroyed(Activity activity) {
Intent BackgrndMusic = new Intent(BackgroundMusic.this, MainPage.class);
stopService(BackgrndMusic);
}
}
Use
#Override
protected void onResume()
{
super.onResume();
startService(foo);
}
#Override
protected void onDestroy()
{
super.onDestroy();
StopService(foo);
}

Detect Dimming on Android Watch

I have searched around and looked for a way to detect the watch dimming. I want to write some code to change a custom watch faces color when the screen dims. How can I do this?
I'm using this abstract Activity to detect the watch dimming (implementing DisplayManager.DisplayListener).
public abstract class WatchFaceActivity extends Activity implements DisplayManager.DisplayListener {
private DisplayManager displayManager;
public abstract void onScreenDim();
public abstract void onScreenAwake();
public void onWatchFaceRemoved() {
}
public void onScreenOff() {
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
displayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
displayManager.registerDisplayListener(this, null);
}
#Override
protected void onDestroy() {
super.onDestroy();
displayManager.unregisterDisplayListener(this);
}
#Override
public void onDisplayRemoved(int displayId) {
onWatchFaceRemoved();
}
#Override
public void onDisplayAdded(int displayId) {
}
#Override
public void onDisplayChanged(int displayId) {
switch (displayManager.getDisplay(displayId).getState()) {
case Display.STATE_DOZING:
onScreenDim();
break;
case Display.STATE_OFF:
onScreenOff();
break;
default:
onScreenAwake();
break;
}
}}
More infos here: http://www.binpress.com/tutorial/how-to-create-a-custom-android-wear-watch-face/120

How to implement phonegap/cordova in android webview?

I need just a few minutes for someone to tell me if these steps are correct for implementing cordova in a android webview:
EDIT: Ok I finally got it working these are the right steps:
1) I create project: cordova create hello com.example.hello HelloWorld and enter the folder
2) cordova platform add android, cordova run android (cordova.jar is created) => the app is launched => device is ready is shown
3) I create a cordova_layout.xml in "/res/layout" with this code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<org.apache.cordova.CordovaWebView
android:id="#+id/cordova_web_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
4)Import the project (as an "existing project" in eclipse) and add to the main java file after imports:
public class HelloWorld extends Activity implements CordovaInterface {
private CordovaWebView cordova_webview;
private String TAG = "CORDOVA_ACTIVITY";
private final ExecutorService threadPool = Executors.newCachedThreadPool();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cordova_layout);
cordova_webview = (CordovaWebView) findViewById(R.id.cordova_web_view);
// Config.init(this);
String url = "file:///android_asset/www/index.html";
cordova_webview.loadUrl(url, 10000);
}
#Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
}
#Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
}
#Override
protected void onDestroy() {
super.onDestroy();
if (this.cordova_webview != null) {
this.cordova_webview
.loadUrl("javascript:try{cordova.require('cordova/channel').onDestroy.fire();}catch(e){console.log('exception firing destroy event from native');};");
this.cordova_webview.loadUrl("about:blank");
cordova_webview.handleDestroy();
}
}
#Override
public Activity getActivity() {
return this;
}
#Override
public ExecutorService getThreadPool() {
return threadPool;
}
#Override
public Object onMessage(String message, Object obj) {
Log.d(TAG, message);
if (message.equalsIgnoreCase("exit")) {
super.finish();
}
return null;
}
#Override
public void setActivityResultCallback(CordovaPlugin cordovaPlugin) {
Log.d(TAG, "setActivityResultCallback is unimplemented");
}
#Override
public void startActivityForResult(CordovaPlugin cordovaPlugin,
Intent intent, int resultCode) {
Log.d(TAG, "startActivityForResult is unimplemented");
}
}
NOTE: the activity name must match the one in manifest.xml
Hope it will help you.
Have a nice day!
If you want to load an url in a phonegap app then you may use the below code to load your first url from asset
public class MyPhoneGapActivity extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html", 10000);
}
For embedding a cordova webview in native android application and loading an url use the below code
public class CordovaActivity extends Activity implements CordovaInterface {
private CordovaWebView cordova_webview;
private String TAG = "CORDOVA_ACTIVITY";
private final ExecutorService threadPool = Executors.newCachedThreadPool();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cordova_layout);
cordova_webview = (CordovaWebView) findViewById(R.id.cordova_web_view);
// Config.init(this);
String url = "file:///android_asset/www/index.html";
cordova_webview.loadUrl(url, 10000);
}
#Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
}
#Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
}
#Override
protected void onDestroy() {
super.onDestroy();
if (this.cordova_webview != null) {
this.cordova_webview
.loadUrl("javascript:try{cordova.require('cordova/channel').onDestroy.fire();}catch(e){console.log('exception firing destroy event from native');};");
this.cordova_webview.loadUrl("about:blank");
cordova_webview.handleDestroy();
}
}
#Override
public Activity getActivity() {
return this;
}
#Override
public ExecutorService getThreadPool() {
return threadPool;
}
#Override
public Object onMessage(String message, Object obj) {
Log.d(TAG, message);
if (message.equalsIgnoreCase("exit")) {
super.finish();
}
return null;
}
#Override
public void setActivityResultCallback(CordovaPlugin cordovaPlugin) {
Log.d(TAG, "setActivityResultCallback is unimplemented");
}
#Override
public void startActivityForResult(CordovaPlugin cordovaPlugin,
Intent intent, int resultCode) {
Log.d(TAG, "startActivityForResult is unimplemented");
}
}

Categories

Resources