How to detect when my Activity has been obscured? - android

I would like to be able to detect if my Activity has been obscured by, say, a system alert or some other overlay (for example the power menu when I long press on the power button), or some malware that detects the launch of my Activity. I noticed that the foreground app in this case would still be my app, so I can't simply base it on what the foreground app is. I also notice that onPause() isn't called when my Activity is obscured, so I can't put any logic in onPause() either. Even if I can though, I would then have to differentiate between a system alert/overlay and the user pressing the back/home button.
Are there any other ways for me to accomplish this?

You can check if Activity, Fragment or View is Obscured.
For Activity you need override dispatchTouchEvent method and check if event has flag FLAG_WINDOW_IS_OBSCURED. There is example code:
public class OverlayTouchActivity extends Activity {
private boolean mObscuredTouch;
public boolean isObscuredTouch() {
return mObscuredTouch;
}
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
mObscuredTouch = (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0;
return super.dispatchTouchEvent(event);
}
}
This is a part of Android code, please check OverlayTouchActivity.java. In order to check if Fragment is obscured, execute the following piece of code in Fragment that belongs to the OverlayTouchActivity activity:
OverlayTouchActivity activity = (OverlayTouchActivity) getActivity();
if (activity.isObscuredTouch()) {
// Fragment is bbscured
}
Please see AppPermissionsFragment.java fragment (search for OverlayTouchActivity).
For View you should override onFilterTouchEventForSecurity method. For more information please see security section of View documentation.

You can use the PackageManager to query whose of the installed packages has suspect permissions like SYSTEM_ALERT_WINDOW, BIND_ACCESSIBILITY_SERVICE or BIND_DEVICE_ADMIN.

Related

Cancel Touch Eventbutt

I am writing an very simple application with following scenario:
1) Screen A have 3 button to move on other screen's.
2) Now if I hold one button(say Button 1) and perform rapid click on other button then it launch multiple instance of other screen. Which I think should not be happened. How can prevent this.
3) and it's more weird. After move on other screen if I don't release Button 1 which was on Screen A then it still allow to perform click for rest of two button of screen A even I can see second screen.
Here it's clear launch second screen but still first screen button event working.
Any idea how can avoid such scenario.
How you are going to disable other buttons while having 1 enabled, that's an algorhytmic problem. You can try creating a boolean or control variable in your activity (and then pass the final reference of the activity to wherever you need it), or in a static context. But to answer the title of the question - you can "Cancel Touch Event" either by adding an OnTouchListener, or if you're extending class Button, you can override onTouchEvent(MotionEvent ev) method.
Using OnTouchListener will disable any previously defined touch-event behavior. You can call the actual click event from the inside by calling performClick method from your button.
//in order to use button inside OnTouchEvent, its reference must be final
//if it's not, create a new final reference to your button, like this:
final finalButton = button;
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouchEvent(MotionEvent ev) {
// ... additional code if necessary
if(canBeClicked) {
finalButton.performClick();
return true;
}
else return false;
}
}
Overriding onTouchEvent in a class extending Button should look something like this.
#Override
public boolean onTouchEvent(MotionEvent ev) {
// ... additional code if necessary
//here we don't really need to call performClick(), although API recommends it
//we just send the touch event to the super-class and let it handle the situation.
if(activity.canBeClicked) return super.onTouchEvent(ev);
else return false;
}
One solution that I found is to disable click listener in onPause() and enable it in onResume() . Can we have better support for this?

Android, how to tell if user clicks a linkify link, or presses home button

I think I need to put some code within my onStop method. It pertains to a service that should be running only when the activity is finished()
but when the user follows some linkify'd text to the web browser, or when the user presses the homescreen, both call onStop() but these do not end the activity, I don't want to end the activity when a user follows a link, so I can't put finish() within onStop() unless I can detect and differentiate when this happens within onStop()
is there a way I can override Linkify() so that I can add a flag within it, or maybe make it run startActivityforResult() so that I can information back in a result?
similarly, is there a way I can set the activity to finish() when the user presses the home button?
Thanks
Is it possible for you to check isFinishing() in your onStop() to decide whether you need to run the service-related code or not?
#Override
protected void onStop() {
super.onStop();
if (isFinishing()) {
// Your service-related code here that should only run if finish()
// was called.
}
}
UPDATE: (after understanding the problem better)
Very similar to my suggested approach on another question, you can probably override startActivity() to intercept when the link is launching and set your flag if that's the case.
#Override
public void startActivity(Intent intent) {
final String action = intent.getAction();
if (action.equals(Intent.ACTION_VIEW)) {
// maybe also check if getScheme() is 'http' then set our flag
persist.saveToPrefs("linkifyClick", true);
}
// proceed with normal handling by the framework
super.startActivity(intent);
}
That other answer also show how you can call startActivityForResult() too instead if you want.
SOLUTION (with still a remaining problem)
I put an onClick: attribute on my textView in the xml
<TextView
android:id="#+id/body"
android:paddingTop="10dp"
android:onClick="onTextViewClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
and then saved a flag within my sharedpreferences within that new method
public void onTextViewClick(View v){
//write to sharedpreferences
persist.saveToPrefs("linkifyClick", true);
}
and then in my onStop I can detect whether that flag is set or not!
#Override
public void onStop(){
super.onStop();
if(persist.getFromPrefs("linkifyClick", false) == false)
{
finish();
}
else
persist.saveToPrefs("linkifyClick", false); //if it was true, then set to false
}
PROBLEM
It is possible to click on the linkified text without touching the actual link portion. This sets the flag to true and still mixes up the lifecycle I am going for. So I need to be able to detect when the home button is pressed
UPDATE: this helped clarify the problem for the other poster (with the accepted answer), that person updated the answer and I accepted it after I put it in my code. It works.

How to make the back function and set the last contentView

I hawe many view's in my application and now the problem is how to go back from one view to another.
What I could do it by set back Buttons in every view but i would like to use the android back hard button.
I have tried something like this:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode==KeyEvent.KEYCODE_BACK)
{
finish();
}
return false;
}
But the problem is that this will close my application.
Could you please guide me for a proper solution, for example to memorize the last view was set and then to come back to this view or something like this.
Here is the code with which I am changing the view (it's a method in my main activity):
public void CheckIfUserIsLogedIn ()
{
int userPresence = localDatabase.CheckUserPresence();
if (userPresence == 0)
{
setContentView(R.layout.main);
}
else
{
setContentView(R.layout.userlogedon);
}
}
Thank you.
Look!
You are doing this wrong way..
An Activity class should only have on content View. (because it is recommended way and easy to use and implement).
And if you want to go to next View, show it under another separate Activity.
when you will finish it, you will be automatically redirected to previous Activity.
(and you don't need to memorize the Previous View :) )
See here, how to work with Activity Stacks.
I am not sure to understand your problem correctly because Android do all that for you automatically. Once a view is opened when you switch to another view it is paused (on screen but has not focus) or stopped (has no focus)
http://developer.android.com/reference/android/app/Activity.html
If the current view (activity) has been launched by the previous view (activity), pressing the back button will make you "close" the current view and go back to the previous one automatically.
Now two things :
Perhaps your are simply opening all views wihtin the same activity by showing on or off components which is a bad way of doing and is not recommended by android. What you should do is 1 view = 1 activity.
You are thinking like "iPhone/iPad" where you have to implements back buttons in the "views". In android you don't need to do so. Putting the "finish" command in your code at that point seem to close the application which make me think you have programmed as explained in point 1.
Hope it helps
EDIT:
To start a new activity do it like this
startActivity(new Intent(this, MyOtherActivity.class));
you put this in your code where you want to load the new view (activity)
Now if you want to transfer some information between activities you must do something like this :
Intent myIntent; //intent declaration
int aNumber = 10; // info to send to other activity
String aString = "abcd"; // info to send to other activity
// link Intent to the other activity
myIntent = new Intent(view.getContext(), MyOtherActivity.class)
//put the extra info
myIntent.putExtra("myNumber", aNumber);
myIntent.putExtra("myString", aString);
//start the new view/activity
startActivity(myIntent);
and in the new opened activity you retrieve the infos like this (in the oncreate usually)
int aNumber;
String aString;
#Override
public void onCreate(Bundle savedInstanceState) {
aNumber= getIntent().getExtras().getInt("myNumber");
aString= getIntent().getExtras().getString("myString");
}
Actually i m not sure that understand exactly but..
take a map or a shared preference and at the back button set last View on map or Shared preference .
At the calling or at start activity fetch the data which have stored you.
this will helps you.

savedInstanceState is always null

This is my savedInstaceState code:
#Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
savedInstanceState.putStringArrayList("todo_arraylist", Altodo);
Log.v("bundle", "Saved");
super.onSaveInstanceState(savedInstanceState);
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (savedInstanceState != null)
{
Altodo = savedInstanceState.getStringArrayList("todo_arraylist");
Log.v("bundle", "Restored");
}
else
{
Log.v("bundle", "null");
}
setContentView(R.layout.main);
}
The logs always show the "bundle save" tag.
But in onCreate method, SavedInstanceState is always null.
I observed the exact same symptoms (reported as issue 133394) in a project with two Activities A and B that extend ActionBarActivity. Activity A is the main activity, and I always receive null for savedInstanceState in onCreate of its list fragment when returning from a detail view activity B. After many hours, this problem exposed itself to me as a navigation issue in disguise.
The following may be relevant to my setup and come from other answers on this page:
Given this answer, I made sure that fragment and activity each have unique IDs set.
There is no override of onSaveInstanceState without super call.
Activity A is specified as acitivy B's parent in AndroidManifest.xml, using both the android:parentActivityName attribute and the corresponding meta-data tag for earlier versions of Android (see "Providing Up Navigation").
Already without any corresponding creation code such as getActionBar() .setHomeButtonEnabled(true), activity B has a functioning back button (<) in its action bar. When this button is tapped, activity A reappears but with (a) all previous instance state lost, (b) onCreate always called, and (c) savedInstanceState always null.
Interestingly, when I tap the back button provided at the bottom edge of the emulator display (an open triangle that points to the left), activity A reappears just as it was left (i.e. its instance state fully retained) without invoking onCreate. So maybe something is wrong with navigation?
After more reading, I implemented my own navigation instructions to run in response to a tap on the back-button in activity B:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home)
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
Nothing related to restoring instance state of activity A changed. NavUtils also provide a method getParentActivityIntent(Activity) and navigateUpTo(Activity, Intent) that allow us to modify the navigation intent to explicitly instruct that activity A is not started fresh (and thus without saved instance state provided) by setting the FLAG_ACTIVITY_CLEAR_TOP flag:
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.
In my hands, this solves problem of lost instance state and could look like:
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()== android.R.id.home) {
Intent intent = NavUtils.getParentActivityIntent(this);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
NavUtils.navigateUpTo(this, intent);
return true;
}
return super.onOptionsItemSelected(item);
}
Note that this may not be the complete solution in other cases where a user can switch directly to activity B from within a different task (see here). Also, a possibly identical solution in behavior that does not make use of NavUtils is to simply call finish():
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()== android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Both solutions work in my hands. I am only speculating that the original issue is a slightly incorrect default implementation of the back-button, and it may be related to that implementation invoking some kind of navigateUp that misses FLAG_ACTIVITY_CLEAR_TOP.
Did you check if you have an Id set for that view ( if a view it is/has...). onSaveInstanceState() is not called otherwise.
Check this link.
The state saved in this manner is not persisted. If the whole application is killed as you are doing during debugging, the bundle will always be null in onCreate.
This IMO is yet another example of awful Android documentation. It's also why most apps in the marketplace don't implement saving state properly (at all).
in Manifest add this line for activities
android:launchMode="singleTop"
for example:
<activity
android:name=".ActivityUniversity"
android:label="#string/university"
android:launchMode="singleTop"
android:parentActivityName="com.alkhorazmiy.dtm.ActivityChart">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.alkhorazmiy.dtm.ActivityChart" />
</activity>
How do you test it?
Imo the best way to test it is using the "Don't keep activities"-flag in Settings > Developer Options. If you don't have Developer Options in Settings, see Enabling On-device Developer Options.
Open your activity
Long-press home
Go to another application
Long-press home
Go back to your application
Shouldn't super.onSaveInstanceState(savedInstanceState); be the first line in your override?
Edit: War_Hero points out in the comments that the documentation on that topic indicates that no, it shouldn't be the first line.
Check your activity in AndroidManifest.xml and remove android:noHistory property if is true.
<activity
// ....
android:noHistory="false" />
To debug, consider implementing onRestoreInstanceState and placing a call to Log.d in this method. Then, in the emulator, hit ctrl-F11 or whatever to rotate the phone. Your call to Log.d should be hit.
Implement a method of onRestoreInstanceState
and put below code there
Altodo = savedInstanceState.getStringArrayList("todo_arraylist");
I found that when I override onSaveInstanceState() and actually save some data in the Bundle, instance state is restored. Otherwise it's not.
Ive managed same way arround. Instead of handling savedInstanceState Bundle on the onCreateView method, ive handled it on onCreate method and setting the passed value to a globar variable then acessing this variable on the onCreateView method.
Hope it helps.
https://developer.android.com/guide/topics/manifest/activity-element#lmode
From this you can see 'Similarly, if you navigate up to an activity on the current stack, the behavior is determined by the parent activity's launch mode.' Maybe you are in the 'standard' mode.
I was able to solve it with:
#Override public boolean onSupportNavigateUp()
{
onBackPressed();
return true;
}
still had parent set in the manifest. So when you press the up navigation button, now it acts like the back button.

Android: How to detect if current stack of activities (task) moves to background?

The official documentation describes tasks as follows:
*All the activities in a task move together as a unit. The entire task (the entire activity stack) can be brought to the foreground or sent to the background. Suppose, for instance, that the current task has four activities in its stack — three under the current activity. The user presses the HOME key, goes to the application launcher, and selects a new application (actually, a new task). The current task goes into the background and the root activity for the new task is displayed. Then, after a short period, the user goes back to the home screen and again selects the previous application (the previous task). That task, with all four activities in the stack, comes forward.
Is there a way to programmatically detect when the task of the current Activity moves into and out of the background? I would like to know when the user has switched switched to another application, vs. when the user navigated to another Activity in the current app.
Note: I've always had this design in my head but never got around to concrete tests (reasonably sure it works though), so if you end up trying this, tell me how it goes. :)
I don't think there's an official API method that'll give you this functionality (I may be wrong), but you can certainly construct your own. This is just my way of doing it; there may, of course, be better implementations.
In your application, you always know when you're starting another activity. With that said, you can create a global class that handles starting all of your application activities and keeps track of a boolean that represents when you're starting an activity:
public class GlobalActivityManager {
private static boolean isActivityStarting = true;
public static boolean isActivityStarting() {
return isActivityStarting;
}
public static void setIsActivityStarting(boolean newValue) {
isActivityStarting = newValue;
}
public static void startActivity(Activity previous, Class activityClass) {
isActivityStarting = true;
// Do stuff to start your activity
isActivityStarting = false;
}
}
Then in each of your activities (maybe make this a superclass), override the onPause() method to check if it's another one of your application's activities starting or a foreign task's application coming in front of your application. This works because onPause is executed when anything comes in front of the activity, meaning it's called when you start your activity in GlobalActivityManager where isActivityStarting is true.
public void onPause() {
super.onPause();
if (GlobalActivityManager.isActivityStarting()) {
// It's one of your application's activities. Everything's normal.
} else {
// Some other task has come to the foreground. Do what needs to be done.
}
}
public boolean isCurrentTaskInBackground() {
List<RunningTaskInfo> taskInfoList = mActivityManager.getRunningTasks(1);
if (taskInfoList != null && taskInfoList.size() > 0) {
RunningTaskInfo info = taskInfoList.get(0);
ComponentName componentName = info.baseActivity;
MobileTvLog.debug("App#isCurrentTaskInBackground -> baseActivity = " + componentName);
String packageName = componentName.getPackageName();
return !YOUR_PKG_NAME.equals(packageName);
} else {
return true;
}
}
Maybe this can be helpfull, tell me if it worked for you.
only when you return from background the value of activities would be 0 (zero)
the rest of the time would be a number higher than 0(zero) when the onRestart()
is executed.
public class FatherClass extends Activity {
private static int activities = 0;
public void onCreate(Bundle savedInstanceState, String clase) {
super.onCreate(savedInstanceState);
}
protected void onRestart()
{
super.onRestart();
if(activities == 0){
Log.i("APP","BACK FROM BACKGROUND");
}
}
protected void onStop(){
super.onStop();
activities -= 1;
}
protected void onStart(){
super.onStart();
activities += 1;
}
}
All of your classes must extend from this class for this to work.
That will not work. Your activity could get an onPause for reasons other than your app explicitly starting another activity. For instance, the user could have hit the back button, taking you back to a previous Activity in your stack.
Try to read about android:finishOnTaskLaunch
http://developer.android.com/guide/topics/manifest/activity-element.html#finish
It solved my similiar problem ;-)
I've been looking for a good answer to this question. It seems Andy Zhang had a good idea, but it may need a bit more work to handle other things, like the Back button.
Another approach which seems promising is to use the ActivityManager.getRunningAppProcesses() method, which will give you a list of RunningAppProcessInfo which each have an "importance" variable which can be used to detect if your process is in the foreground or other states.
The problem of course is there is no Detection technique here, just a way to tell what is running and its state, but no way to get informed when the state changes.
So possibly just run a background thread to poll this info. Seems a bit kludgy but it might work.
My app uses location services (GPS) so it automatically gets called when the location changes, and I can check the RunningAppProcessInfo list when this happens to determine if my App is still in the foreground. Seems like a lot of work, so maybe Andy's approach can be improved on.
I have been using the method I describe here:
Simple check for Android application backgrounding
It has been working wonderfully for me.
you might be able to use the isFinishing() method in onPause(). That will tell you if the application is actually finishing or is just being paused. When your task stack goes to the background, the activities aren't finished, they are just paused. This will also get called if you start another activity from this one though, so you would have to check for that.
#Override
public void onPause() {
super.onPause();
if(!isFinishing()) {
//app going to background
}
}

Categories

Resources