How to find out screen status: active/dark?

by JP » Fri, 29 May 2009 10:28:32 GMT


Sponsored Links
 I've dug through the documentation but couldn't find a call that would
fit: Is there a way to find out the screen status (Active/dark)?

Thanks in advance!

P.S.
Here's the situation: I've got an app that implements the relevant
data processing and backend server connectivity through a Service that
I keep running on a reduced activity level when the user puts the
device to sleep (using end button), or when the screen timeout hits.
In the app code, I can follow this status change, because it triggers
calls to overloaded onStop() and onPause() methods of the app's
subclassed Activity. This way, I control active/reduced ("sleep")
activity levels of that Service. Everything's cool and dandy in this
scenario.

Enter an AppWidget that's using this Service in a similar fashion,
with functionality implemented in a subclass of AppWidgetProvider.

This changes the game - I have found no way to determine when to set
the Service to the reduced ("sleep") activity level from that point.
The app's Activity has long paused, and I keep the Service at active
level, as the widget needs updates from the Service similar to what
the Activity needs when in front. With the AppWidget on the home
screen, I want, and should (re: battery life), drop the Service's
activity level when the device enters sleep mode. The most elegant way
to handle this would be to detect whether the screen is dark, right
inside the Service. Any way to do this?

JP


--~--~---------~--~----~------------~-------~--~----~



How to find out screen status: active/dark?

by Jeff Sharkey » Sat, 30 May 2009 02:38:27 GMT


 > I've dug through the documentation but couldn't find a call that would

You can listen for Intent.ACTION_SCREEN_OFF and ACTION_SCREEN_ON
broadcasts.  However, I would strongly recommend registering for these
dynamically at runtime using Context.registerReceiver().  This ensures
that you'll only receive the broadcasts if your Service is already
awake, which is what you want.  :)


-- 
Jeff Sharkey
jshar...@android.com

--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


How to find out screen status: active/dark?

by clemsongrad » Sat, 30 May 2009 04:37:51 GMT


 Jeff,

What would the behavior be if you registered the broadcast receiver
and listed these intents in the manifest?

Would the run time start the service to invoke the onReceive()
method?   If so this is a great tip for optimization.

Thanks



--~--~---------~--~----~------------~-------~--~----~



How to find out screen status: active/dark?

by JP » Sun, 31 May 2009 09:54:19 GMT


 Got it, thank you; works perfectly.




--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Does the camera app work on the emulator?

Hello
I'm writing an app that needs a camera. On the emulator I've added a
camera. The camera app that comes already installed on the emulator
does not work, it just shows a blank screen, without a title. Is this
normal or am I doing something wrong. I'm using R2. If this is normal,
can MY app work, or do I need to buy a real phone?
John
--~--~---------~--~----~------------~-------~--~----~

2. Struggling -- Getting exception when clicked on HelloItemizedOverlay

Hi friends
I am showing some locations in the map, when i click on the particulat
overlay, i am planning to show a dialog box to show the details info
of that particular overlay.
The detailed info of overlay is there in my locations in
Mylocations.java .
So onTap, i am calling a method in Mylocations.java and starting a new
intent which will show a new layout with all detailed info.

I am getting below issue, can you please help me?

public class Mylocations extends MapActivity {

private static List<LocationInfo> locations;
@Override
public void onCreate(Bundle savedInstanceState) {
------------------
------------------
itemizedOverlay = new HelloItemizedOverlay(this, drawable);
------------------
------------------
}

----------------
----------------
public void showDetailedInfo(Context mContext, int index){
Intent intent = new Intent(mContext, ShowDetailedtInfo.class);
startActivity(intent);
}
----------------
----------------

}

public class HelloItemizedOverlay extends ItemizedOverlay {

public HelloItemizedOverlay(Context context, Drawable defaultMarker)
{
super(boundCenterBottom(defaultMarker));
mContext = context;
}
@Override
protected boolean onTap(int index) {
Mylocations mylocations = new Mylocations();
mylocations .showDetailedInfo(mContext,index);
}

}

When starting new activity @ startActivity(intent);, i am getting the
below error:

08-13 23:30:03.758: WARN/dalvikvm(1528): threadid=3: thread exiting
with uncaught exception (group=0x4000fe70)
08-13 23:30:03.758: ERROR/AndroidRuntime(1528): Uncaught handler:
thread main exiting due to uncaught exception
08-13 23:30:03.878: ERROR/AndroidRuntime(1528):
java.lang.NullPointerException
08-13 23:30:03.878: ERROR/AndroidRuntime(1528): at
android.app.Activity.startActivityForResult(Activity.java:2656)
08-13 23:30:03.878: ERROR/AndroidRuntime(1528): at
android.app.Activity.startActivity(Activity.java:2700)
08-13 23:30:03.878: ERROR/AndroidRuntime(1528): at
com.example.Mylocations.showDetailedInfo(Mylocations.java:284)
08-13 23:30:03.878: ERROR/AndroidRuntime(1528): at
com.example.HelloItemizedOverlay$1.onClick(HelloItemizedOverlay.java:
83)


Can You please help me?

Thanks & Regards
Venkat
--~--~---------~--~----~------------~-------~--~----~

3. Can we develop application for CECT G1(Clone g1)?

4. Align Bottom

5. GmapActivity Only marker Touch Listener

6. How to change ringtone volume to constant value

7. copy big size file crash