How to access the Window when not an activity? - android

I want to access the Window class so I can set the screen brightness on my phone. The problem is that the class I want to do this from is not an activity. Is it possible to do this without being an activity? I have a context and a content resolver, if that helps.
Thanks in advance!

You have two options here.
Start a new transparent Activity, adjust the brightness value (of both the Window and system settings), then call finish() on the Activity. This will steal focus from the user in some cases, no matter what flags you use.
Create a persistent transparent system-wide overlay using a Dialog and the flag WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY. This can cause odd issues like blocking the installation of apps.
In other words, there's no clean way of doing it.

Related

Pass through touch events to app beneath

I'm working on an Android Activity which should not be full screen and thus uses Dialog theme. This can be achieved by adding
android:theme="#android:style/Theme.Dialog"
to the activity definition in Manifest file.
Visually that does what I expect. However, when the user interacts with the device in an area outside of this activity, the app in the background does not receive this input.
Is there a possibility to achieve this?
that is not possible with your solution as there is only one Activity possible to be active. You could try that with Fragments but I doubt that you could open your App and navigate the Homescreen / whatever in the meantime.

Starting a View from a Service?

Already asked a similar question, yet without much luck.
Suppose I have a service and I need a view to pop up above it. In the same time, they both should be intractable, i.e. the user should be able to both click buttons within the view, as well as ones on the service in the background.
Is this in theory possible? If yes, how should I initialize that view?
Thanks!
Yes it's possible, what you need to do is call the WindowManager service and add your view via the same.
WindowManager windowManager=(WindowManager)getSystemService(WINDOW_SERVICE);
LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
RelativeLayout layout=(RelativeLayout) inflater.inflate(R.layout.box,null);
You need a WindowManager.LayoutParams object which should contain the parameters for the layout
windowManager.addView(layout,params);
Well, adds the view
What you want is to add a view from your running service instance. This way you can persist the view across all activities - and from anywhere else. See this great example:
http://www.piwai.info/chatheads-basics/
Services most definitely can have a user interface: Input methods are an obvious example. See http://developer.android.com/resources/samples/SoftKeyboard/index.html for an example.
I guess you are misusing the word "Service".
Service is invisible, Activities are visible.
There are no buttons in an Service!
So you have no choice! You should put both views in one Activity, and I would use a RelativeLayout and set the visibility of your chidren to GONE/Visible.
http://developer.android.com/reference/android/widget/RelativeLayout.html
Also using a popup and making the layout under it clickable will disturb the user. You are completely changing User experience. I strongly suggest too make your popup appear at the top/bottom of your initial layout
Services run in the background and do not have an UI. So you can not show something over a Service.
If you need a Service to notify user about something, then use Notification.
Ayou could use a Toast, but I advise against it, as it can confuse users since it can pop-out over another app's activity.
What you want is an Activity instead of a Service and a Dialog instead View. I suggest you read this document by google: http://developer.android.com/guide/topics/fundamentals.html
However to answer your question about both being interactable. This isn't possible. At any given time 1 and only 1 activity is on the top of the activity stack. The user can only interact with that activity. If you want something like a floating window then will have to create it yourself. Although keep in mind that goes against the android design principles.

Transparent, floating Android Activity doesn't allow updates to content behind it

I have tried and tried to get a transparent, floating Activity to show up (as an overlay), but allow whatever is behind it to still show AND update. Right now it seems that if the Activity behind mine is closed or a new one opens (could be either in this case), the new underneath Activity does not shine through my Activity to the user.
I have tried every combination of Flags I can come up with, and at this point I'm assuming Flags are not the answer. Can anyone help me find the proper code to do such a thing?
Before anyone asks, I have a valid use case for this type of Activity; no, I don't plan to annoy the user with it.
As far as I know, this is not possible. It should be possible to create an activity using the theme Theme.Dialog or Theme.Translucent (see http://developer.android.com/guide/topics/ui/themes.html) to have whatever activity is beneath it still show at least partially. The problem is, is that the Activity below will be Paused (it's onPause will have fired, but it's onStop will not have) and I don't believe it is possible in any way to have it run any code.
I have not investigated in making a transparent Activity but I don't think it's possible in an Activity way. This seems to be logical since even if you have a transparent Activity it's still relying on the View inside it - the View makes the transparent part, not the Activity. This means you're probably gonna end up with a transparent View instead.
If you have a "front" Activity with a transparent View and then a "back" Activity, the "back" Activity would not be visible to the user - and that's because you're in another Activity.
So, the correct way is to use a transparent View.
It is possible to update the activity below by implementing a Broadcast receiver on it, and sending Broadcasts from whenever you want.

Disable keep screen on

I used:
getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
How do I resume to Default state (no-keep-on)?
I think this should do it:
getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
See API for details.
If you instead set a flag android:keepScreenOn="true" (documentation) only on the views that need to keep the screen on, you wouldn't need to reset the flag manually.
Another approach
getWindow().setFlags(this.getWindow().getFlags() & ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Also read this
and you can also set android:keepScreenOn="true" in the root View in xml.
Directly from documentation:
Note: You don't need to clear the FLAG_KEEP_SCREEN_ON flag unless you no longer want the screen to stay on in your running application (for example, if you want the screen to time out after a certain period of inactivity). The window manager takes care of ensuring that the right things happen when the app goes into the background or returns to the foreground. But if you want to explicitly clear the flag and thereby allow the screen to turn off again, use clearFlags(): getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON).

How can I keep current screen (contentView) when l Iaunch a new Activity

I have an NoContentViewActivity which has no Content View (i.e. I did not call setContentView() in the onCreate of my Activity).
My question is how can I keep the content view of the launching activity on the screen? Right now, I am getting a blank screen whenever I launch NoContentViewActivity? I want the content view of the launching activity (the activity which start the NoContentViewActivity) to show on the screen.
Thank you for any help.
Excuse me, people, but my guess is that hap497 wants exactly the thing he wants. There is a bunch of situations where invisible activity would fit while Service will not.
Imaging you want to show a set of dialogs, each next of them is shown after the previous one based on the user choices. And imaging you want to have this (exactly the same) functionality to be available when pressing different buttons on different (lots of them) activities.
To write the same dialog processing logic would be an overkill whether the transparent activity will deal nicely...
Anyway, as stated above, all you need to do is to specify:
android:theme="#android:style/Theme.Translucent"
or
android:theme="#android:style/Theme.Translucent.NoTitleBar"
(if you do not want a titlebar either)
It sounds like you'd be better off using a Service than an Activity. Activities are for code that is to be viewed; Services are for code that runs without a UI, which is what it sounds like you want.
An Activity with no Views assigned is an empty black screen, so it will still obscure the calling Activity. You could make your Activity transparent by assigning it a transparent theme:
android:theme="#style/Theme.Translucent"
Keep in mind though, that your invisible Activity will have focus, so the user won't be able to interact with the Activity underneath.
Why do you want to create a fully transparent Activity? As Daniel suggests, a Service might be a better solution to your problem if you genuinely don't want any user interaction.

Categories

Resources