How can I know if the user took screenshot of my app? - android

My app represents funny sentences that my users upload, and in order to know which sentences are better I want to know if the user has taken a screenshot. The only related thing I found is from Google Maps. Anyone know how to make a "screenshot listener" or which method it invokes? Thanks.

One way to do this is by creating a class which is implementing FileObserver. Then add listener when you are loading activity ( by using onStart() activity's method) and remove listener when you are going out of your activity (by using onStop() activity's method).
Once you get FileObserver event, you have to be sure that is a picture creation.
Note that this method is not 100% safe but it's simple to make it works !

Related

How to correctly use onVisibilityChanged(), onWindowVisibilityChanged(), onAttached/Detached()?

I've a custom class whose parent is SurfaceView. I have the class working correctly, drawing to the screen from its own thread etc. However I want to understand how the methods in the question title should be correctly handled.
For example, if my app is running (on a mobile phone) and a call is received, which method is called? Another example is if I implement a dialog box to be displayed if the user 'long-presses' on my custom SurfaceView, what method is called then? I assume the canvas is 'safe' to write to provided surfaceDestroyed() has not been called.
Basically I want to understand how I should handle eventualities as I've described (and possibly more?) and for what events each method is called.
on...-methods are called when something happened.
You probably don't have to implement them because the super class does the correct things in most cases.
If you put log in the methods in question you can see what happens / when they are called. You can also read the API doc and have a look at the source code to see what is done there.

How do I refresh Activity like it is being loaded for first time?

I am trying to figure out how, when a user lands on my activity screen, it is "reloaded" as if it were being loaded for the first time.
I don't want my user hitting the back arrow and coming back to my activity with old information.
As it is now, when a user "comes back" to my page, the database list isn't being repopulated, and information they typed into EditText fields remains there.
I want the page, everytime the user comes to it, to be like it's their first time there.
Have you ever tried using recreate() in your current activity? Try using it after your new values are populated.
Or you can just put everything that's in the onCreate() on the onResume. A bit ugly but it works.
I have to disagree with Kartik on this, as I understand, android:noHistory='true' will remove activity from application stack. So when user hits back, user will not be able to see the activity at all.
About activity not retaining its value, I would not recommend you this, as user expects that all values would be retained when back is hit, unless there is some specific requirement that you are trying to meet.
So I guess solution to your problem is, as others have suggested do your initialization on views in onResume(). But just doing this may not be sufficient, as views like EditText will by default cache the values anyways. So you might have to manually clear those in your on onResume(). Will keep looking to find any 'perfect' solution if any to this problem.
I had solving similar problem like yours and I solve it whit lunch mode.
I think the best is to take a look of this set the lunch mode in single instance and then
in you onCreate and onResume you can code to refresh you view just the way you want it.
copy requred code into onResume() from onCreate() method

How do I capture changes of a CheckBoxPreference in android development?

I'm trying to learn how to do live wallpapers for android. I've got it working and now I want to add a couple preferences. So, I've created a preference activity and followed all the examples I could find.
I can capture ListBoxPreferences just fine, but the 'onSharedPreferenceChanged' method is never called when a checkbox has been changed.
Is there extra code that has to be added for capturing checkbox changes? Is there a best practices way of doing check boxes in preferences?
I've been banging my head against this issue for two days, any help would be GREATLY appreciated!
Thanks!
#Josh - Good answer.
I'd like to add for the sake of completeness is that you should set things up so you register for changes in your PreferenceActivity's onResume() method and unregister in the onPause() method.
The answer to this question has a good sample of code taken from on the the stock Android example programs.
Alternatively, you can also register listeners for each of the preferences in your activity's onCreate() and handle events on them separately .
Did you register to receive onSharedPreferenceChanged callbacks?
Add this to your main activity's onCreate method (and make sure to implement onSharedPreferenceChanged):
PreferenceManager.getDefaultSharedPreferences(getBaseContext()).registerOnSharedPreferenceChangeListener(this);

Android - Simple View Refresh

I don't think my future lies in Android development, as I am consistently failing at the simplest things...
I've got a button with the label "Game Slot 1". When the user clicks it, I succesfully take them through a couple of activities in which they create a new character. I save the game name in an SQLite database, and the next time I launch my app, I can quite happily dynamically change "Game Slot 1" to the name of that slot's game from the database. Perfect.
But how on earth do I get it to update the view in the same manner when I return to that activity via the back button, or having called finish() on all the subsequent activities?
I'm sure it's something to do with onResume(), and maybe invalidate(), but I just can't seem to find an example.
Is invalidate overkill just to refresh a few UI elements onResume()?
How the heck do you use it, anyway, even if it is?
Is there a better way?
Many thanks in advance for any help offered...just try not to laugh at how simple this probably is! :)
Cheers,
James
That sounds like something you could do in onResume, just query the database and call setText on the element of interest. You should not need to explicitly call invalidate.

Confused about activity lifecycle usage in the notepad example

I am confused about activity lifecycle usage in the notepad example,notepad example use "edit in place" user model,inserting new record in onCreate method,
saving persistent state in onPause method,and save away the original text in onSaveInstanceState method.
I am a J2EE programer,I can not understand the logic described above. why not make things simple as following:
1.Not inserting new record in onCreate method.
2.When user pressing BACK,it is equal as pressing save button in the editorform,so execute inserting or updating in onPause method if activity.isFinishing() is true.
don't persiste use data if activity.isFinishing() is not true.
3.Not save the original text in onSaveInstanceState method,It is no necessary.If the activity is killed and back,restore user inputing data in the editorForm is adequately.
I think this logic is more traditional and natural.
Maybe I not understand the essence of the activity lifecycle.Please air your's opinion.
Thanks
L.J.W
the lifecycle of an adroid app under various conditions (e.g. switching screens, freezing, stopping etc.) is described in an excellent video tutorial by google. You may also want to refer to the slides of that talk, in particular, slide 16ff may be of great interest for you.
In any case you are right in thinking that understanding the lifecycle of an android app is the key to coding for android.

Categories

Resources