I am trying to get my Screen ON/OFF to work on my Service, but so far no luck.
The goal is: Get the LED lights ( softkeys ) to go out when the screen goes OFF and Softkey light to turn ON when the screen goes back ON with the values the user inputed on my app ( you will see the TextXLActivity.getledC() which gets the Int from the main activity )
On my main activity i can control the LED lights without any problem, with a SeekBar and so on. Only thing not working is really the Receiver/Service
Now when I go to settings, applications, running services, my App is not listed anywhere, I am affraid my Service isn't starting at all, and maybe that is the problem here.
Here is my Receiver:
package com.test.xl;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class ScreenReceiver extends BroadcastReceiver {
private boolean screenOff;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOff = false;
}
Intent i = new Intent(context, UpdateService.class);
i.putExtra("screen_state", screenOff);
context.startService(i);
}
}
And here my Service:
package com.test.xl;
import com.sonyericsson.illumination.IlluminationIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
public class UpdateService extends Service {
#Override
public void onCreate() {
super.onCreate();
// register receiver that handles screen on and screen off logic
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
}
#Override
public void onStart(Intent intent, int startId) {
boolean screenOn = intent.getBooleanExtra("screen_state", false);
if (!screenOn) {
{
Intent led = new Intent(IlluminationIntent.ACTION_START_LED);
led.putExtra(IlluminationIntent.EXTRA_PACKAGE_NAME, "com.test.xl");
led.putExtra(IlluminationIntent.EXTRA_LED_ID, IlluminationIntent.VALUE_BUTTON_2);
led.putExtra(IlluminationIntent.EXTRA_LED_COLOR, TestXLActivity.getledC());
startService(led);
}
} else {
Intent led = new Intent(IlluminationIntent.ACTION_STOP_LED);
led.putExtra(IlluminationIntent.EXTRA_PACKAGE_NAME, "com.test.xl");
led.putExtra(IlluminationIntent.EXTRA_LED_ID, IlluminationIntent.VALUE_BUTTON_2);
led.putExtra(IlluminationIntent.EXTRA_LED_COLOR, 0xFFFFFFFF);
startService(led);
}
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
This is a Test app I started to try and understand the Receiver/Service "relation", any help would be appreciated! ;)
I also noticed that my Service forces me to implement this lines:
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
I followed a tutorial on how to get my service to interact with the Receiver and the source code there had nothing like that at the end, any guesses?
Best regards
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.xl"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10"/>
<uses-permission android:name="com.sonyericsson.illumination.permission.ILLUMINATION"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TestXLActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<receiver android:name=".ScreenReceiver">
<intent-filter>
<action android:name="android.intent.action..ACTION_SCREEN_ON" />
<action android:name="android.intent.action.ACTION_SCREEN_OFF" />
</intent-filter>
</receiver>
</intent-filter>
</activity>
</application>
</manifest>
Edit:
Here is the source code where the error appears (startService(led)):
package com.test.xl;
import com.sonyericsson.illumination.IlluminationIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class ScreenReceiver extends BroadcastReceiver {
private boolean screenOff;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Intent led = new Intent(IlluminationIntent.ACTION_STOP_LED);
led.putExtra(IlluminationIntent.EXTRA_PACKAGE_NAME, "com.test.xl");
led.putExtra(IlluminationIntent.EXTRA_LED_ID, IlluminationIntent.VALUE_BUTTON_2);
led.putExtra(IlluminationIntent.EXTRA_LED_COLOR, 0xFFFFFFFF);
startService(led);
screenOff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Intent led = new Intent(IlluminationIntent.ACTION_START_LED);
led.putExtra(IlluminationIntent.EXTRA_PACKAGE_NAME, "com.test.xl");
led.putExtra(IlluminationIntent.EXTRA_LED_ID, IlluminationIntent.VALUE_BUTTON_2);
led.putExtra(IlluminationIntent.EXTRA_LED_COLOR, TestXLActivity.getledC());
startService(led);
screenOff = false;
}
}
}
NEW CODE
public class ScreenReceiver extends BroadcastReceiver {
private boolean screenOff;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Intent led = new Intent(IlluminationIntent.ACTION_STOP_LED);
led.putExtra(IlluminationIntent.EXTRA_PACKAGE_NAME, "com.test.xl");
led.putExtra(IlluminationIntent.EXTRA_LED_ID, IlluminationIntent.VALUE_BUTTON_2);
led.putExtra(IlluminationIntent.EXTRA_LED_COLOR, 0xFFFFFFFF);
context.startService(led);
screenOff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
int ledC = TestXLActivity.getledC();
Intent led = new Intent(IlluminationIntent.ACTION_START_LED);
led.putExtra(IlluminationIntent.EXTRA_PACKAGE_NAME, "com.test.xl");
led.putExtra(IlluminationIntent.EXTRA_LED_ID, IlluminationIntent.VALUE_BUTTON_2);
led.putExtra(IlluminationIntent.EXTRA_LED_COLOR, ledC);
context.startService(led);
screenOff = false;
}
}
.
public class UpdateService extends Service {
private ScreenReceiver mReceiver = null;
#Override
public void onCreate() {
super.onCreate();
// register receiver that handles screen on and screen off logic
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
if(mReceiver != null)mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
unregisterReceiver(mReceiver);
super.onDestroy();
}
}
I think you problem is that your BroadcastReceiver mReceiver does not survive after the onCreate method of your service. Furthermore you are not declare it right. If I was you I would do something like this:
public class UpdateService extends Service {
private ScreenReceiver mReceiver = null;
#Override
public void onCreate() {
super.onCreate();
// register receiver that handles screen on and screen off logic
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
if(mReceiver != null)mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
*** NOTE HERE THAT I AM OVERRIDING THE ONSTART AS AN INT NOT VOID***
.... do your stuff here....
return Service.START_STICKY;
}
Now there is another problem with your approach. You are declaring and instantiating your receiver in the service's onCreate method and then you are starting the service AGAIN from within onReceive method of your receiver. This is not the way android services work.
The correct approach is to instantiate and register your receiver from the onCreate method of your service and start the service from another point of your application, for example if it is an app-widget you can do this from the onUpdate method of the WidgetProvider or from some Activity's onCreate method.
So your code should be as follow:
Your receviver:
public class ScreenReceiver extends BroadcastReceiver {
private boolean screenOff;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOff = false;
}
... Do your stuff here for screen on or off ....
... AND DO NOT START A SERVICE FROM HERE ....
}
}
Your service:
public class UpdateService extends Service {
private ScreenReceiver mReceiver = null;
#Override
public void onCreate() {
super.onCreate();
// register receiver that handles screen on and screen off logic
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
if(mReceiver != null)mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
NOTE HERE THAT I AM OVERRIDING THE ONSTART AS AN INT NOT VOID
.... YOU DON'T HAVE TO DO SOMETHING HERE EXCEPT FROM SOME ....
.... INITIALIZATION CODE ...
return Service.START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
UnregisterReceiver(mReceiver);
super.onDestroy();
}
}
Your Activity or other code point:
startService(new Intent(context, UpdateService.class));
What happens here is this:
When the startService executes your service is instantiated and the onCreate method executes in which the receiver is registered and starts monitoring for Intents. Then the onStart method of the service is called and it returns START_STICKY which means that the service will be restarted if terminated from the system and thus the receiver will be registered again and so on. BTW whenever your service terminating the onDestroy method will be called and the receiver will be unregistered (which is a very goog thing to do).
Now you have a service run all the time ready to receive notifications from the system.
If you wish to terminate the service at some point, simply call the
context.stopService(new Intent(context, UpdateService.class));
I don't really know Android services in depth but this technique works very well with my service which hosts a BroadcastReceiver and a ContentObserver at the same time.
EDIT BASED ON YOUR NEW PROGRESS
Ok now it seems that we are getting somewhere. I can see in your onReceive method
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Intent led = new Intent(IlluminationIntent.ACTION_STOP_LED);
led.putExtra(IlluminationIntent.EXTRA_PACKAGE_NAME, "com.test.xl");
led.putExtra(IlluminationIntent.EXTRA_LED_ID, IlluminationIntent.VALUE_BUTTON_2);
led.putExtra(IlluminationIntent.EXTRA_LED_COLOR, 0xFFFFFFFF);
context.startService(led);
screenOff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
int ledC = TestXLActivity.getledC();
Intent led = new Intent(IlluminationIntent.ACTION_START_LED);
led.putExtra(IlluminationIntent.EXTRA_PACKAGE_NAME, "com.test.xl");
led.putExtra(IlluminationIntent.EXTRA_LED_ID, IlluminationIntent.VALUE_BUTTON_2);
led.putExtra(IlluminationIntent.EXTRA_LED_COLOR, ledC);
context.startService(led);
screenOff = false;
}
}
You are declaring an intent led and you are using it to start a service, but you are not specifying which service to start. I think you are missing something like led.setClass. But in order to clear things a little bit let's modify the onReceive like this:
#Override
public void onReceive(Context context, Intent intent) {
Intent led = new Intent(context, your_led_service_name.class);
led.putExtra(IlluminationIntent.EXTRA_PACKAGE_NAME, "com.test.xl");
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
led.addAction(IlluminationIntent.ACTION_STOP_LED);
led.putExtra(IlluminationIntent.EXTRA_LED_ID, IlluminationIntent.VALUE_BUTTON_2);
led.putExtra(IlluminationIntent.EXTRA_LED_COLOR, 0xFFFFFFFF);
context.startService(led);
screenOff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
int ledC = TestXLActivity.getledC();
led.addAction(IlluminationIntent.ACTION_START_LED);
led.putExtra(IlluminationIntent.EXTRA_LED_ID, IlluminationIntent.VALUE_BUTTON_2);
led.putExtra(IlluminationIntent.EXTRA_LED_COLOR, ledC);
context.startService(led);
screenOff = false;
}
}
and see how it goes now...
SECOND EDIT
Ok I don't know anything about Sony Ericsson Illumination API but based on your code you may need to change the onReceive as follows:
#Override
public void onReceive(Context context, Intent intent) {
IlluminationIntent led = new IlluminationIntent(??);
//Note: In place of ?? you may need your context on nothing at all
//it depends on sony's API.
led.putExtra(IlluminationIntent.EXTRA_PACKAGE_NAME, "com.test.xl");
led.putExtra(IlluminationIntent.EXTRA_LED_ID, IlluminationIntent.VALUE_BUTTON_2);
//This is only needed once...
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
led.addAction(IlluminationIntent.ACTION_STOP_LED);
led.putExtra(IlluminationIntent.EXTRA_LED_COLOR, 0xFFFFFFFF);
screenOff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
int ledC = TestXLActivity.getledC();
led.addAction(IlluminationIntent.ACTION_START_LED);
led.putExtra(IlluminationIntent.EXTRA_LED_COLOR, ledC);
screenOff = false;
}
context.startService(led);
}
Hope this helps...
Related
I have tried this code but it's not working. Does anybody have any different solution? I have tried many ways like the below one from Stack Overflow but none of them is working.
manifest.xml
<receiver android:name=".ScreenReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"/>
<action android:name="android.intent.action.SCREEN_ON"/>
</intent-filter>
</receiver>
screenreceiver
public class ScreenReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
{
Intent intent = new Intent();
intent.setClass(context, ScreenLockActivity.class);
startActivity(intent);
}
}
}
To listen to screen on/off, your app should run by time and register Broadcast receiver to OS programmatically.
ScreenOnOffService.java
public class ScreenOnOffService extends Service {
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate() {
super.onCreate();
Log.i("ScreenOnOffService", "onCreate: ");
IntentFilter intentFilter = new IntentFilter();
// intentFilter.addAction("android.intent.action.SCREEN_OFF");
intentFilter.addAction("android.intent.action.SCREEN_ON");
registerReceiver(ScreenOnReceiver.newInstance(), intentFilter);
}
public void onDestroy() {
super.onDestroy();
Log.i("ScreenOnOffService", "onDestroy: ");
unregisterReceiver(ScreenOnReceiver.newInstance());
// startService(new Intent(this,ScreenOnOffService.class));
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}
}
ScreenOnReceiver.java
public class ScreenOnReceiver extends BroadcastReceiver {
public static final String TAG = "ScreenOn";
public static volatile ScreenOnReceiver screenOn;
public static ScreenOnReceiver newInstance() {
if (screenOn == null) {
screenOn = new ScreenOnReceiver();
}
return screenOn;
}
#Override
public void onReceive(Context context, Intent intent) {
Log.i("hieuN", "intent: " + intent.getAction());
// do work. start activity.
}
}
Start service in activity
Intent service = new Intent(this, ScreenOnOffService.class);
startService(service);
I'm trying to catch when my device screen is turned off or on. I looked at this answer here. However I haven't quite figured it out. When I test it, I get a warning saying that the service wasn't able to be created: Unable to start service Intent... not found. I'm new to services so I was hoping someone could look over the code and see what I'm doing wrong. Here is my Receiver and Service:
public class MyReceiver extends BroadcastReceiver {
private boolean screenOff;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = true;
Home.locked = true;
Log.i("screenstate", "off");
} else if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.i("screenstate", "on");
}else if(intent.getAction().equals(Intent.ACTION_ANSWER)) {
}
Intent i = new Intent(context, UpdateService.class);
i.putExtra("screen_state", screenOff);
context.startService(i);
}}
Service:
public class UpdateService extends Service {
BroadcastReceiver mReceiver;
Boolean isRunning;
Context context;
Thread backgroundThread;
#Override
public void onCreate() {
super.onCreate();
context = this;
isRunning = false;
// register receiver that handles screen on and screen off logic
Log.i("UpdateService", "Started");
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_ANSWER);
mReceiver = new MyReceiver();
registerReceiver(mReceiver, filter);
}
#Override
public void onDestroy() {
unregisterReceiver(mReceiver);
isRunning = false;
super.onDestroy();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
boolean screenOn = intent.getBooleanExtra("screen_state", false);
if (!screenOn) {
Log.i("screenON", "Called");
Toast.makeText(getApplicationContext(), "Awake", Toast.LENGTH_LONG)
.show();
} else {
Log.i("screenOFF", "Called");
}
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}}
Here is my main activity:
public class Home extends Activity {
static boolean locked = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(Home.this, UpdateService.class));
if(locked)
setContentView(R.layout.activity_home2);
else
showApps(null);
}
public void showApps(View v){
locked = false;
Intent i = new Intent(this, AppsList.class);
startActivity(i);
}}
Thanks in advance.
It looks like the service hasn't been declared in the manifest file. Add its declaration within the <application> tag:
<service android:name=".UpdateService"/>
I want to create an app that will know when user is using the phone(start the screen and close the screen). After a period of time I need to call doSomething() method.
Question:
1.How can I know when user start using the phone and when he close the screen?
2.Should I use Service or IntentService? Which is better in my case?
You can try something like this using a BroadcastReceiver and a Service:
Your class using The BroadcastReceiver:
public class ScreenReceiver extends BroadcastReceiver {
private boolean screenOff;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOff = false;
}
Intent i = new Intent(context, UpdateService.class);
i.putExtra("screen_state", screenOff);
context.startService(i);
}
}
And the service:
public static class ScreenService extends Service {
#Override
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
}
#Override
public void onStart(Intent intent, int startId) {
boolean screenOn = intent.getBooleanExtra("screen_state", false);
if (!screenOn) {
//Implement here your code
} else {
//Implement here your code
}
}
}
please before you don't like my question , please read the details .. what i am trying to do is to use a broadcast receiver when screen-off , so i want my app to start if the screen goes off .. here is my broadcast receiver code :
public class BootReceiver extends BroadcastReceiver {
public boolean screenoff;
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenoff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenoff = false;
}
Intent intent1 = new Intent(context, ShakeService.class);
intent1.putExtra("screen_state", screenoff);
context.startService(intent1);
}
and here is the service code :
public class ShakeService extends Service{
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stu
return null;
}
#Override
public void onCreate() {
super.onCreate();
// REGISTER RECEIVER THAT HANDLES SCREEN ON AND SCREEN OFF LOGIC
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new BootReceiver();
registerReceiver(mReceiver, filter);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
try{
Intent intent1 = new Intent(getApplicationContext(), SplashScreen.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return START_STICKY;
}
}
and here is my splash screen activity that i call from the service :
public class SplashScreen extends Activity {
private static int SPLASH_TIME_OUT=3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash_screen);
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
new Handler().postDelayed(new Runnable(){
public void run(){
Intent i =new Intent(SplashScreen.this,HomeScreen.class);
startActivity(i);
finish();
}
},SPLASH_TIME_OUT);
}
#Override
protected void onResume() {
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
BroadcastReceiver mReceiver = new BootReceiver();
registerReceiver(mReceiver, filter);
super.onResume();
}
#Override
protected void onPause() {
// mSensorManager.unregisterListener(mShakeDetector);
BroadcastReceiver mReceiver = new BootReceiver();
unregisterReceiver(mReceiver);
super.onPause();
}
}
and as you can noticed i have unregistered my receiver but still i keep seeing this in the logcat :
leaked Intent Receiver are you missing a call to unregisterReceiver??
I agree with the method of registering the receiver in onResume() and unregistering it in onPause(). This helps when the app goes in and out of scope. I'm somewhat confused on what you're trying to do with your app, but initially I see that you create a new instance of the braodcast receiver in onPause() and unregister that. Try unregistering using the same instance . Also, if you want your service to remain running in the background, you may want to look into implementation of a partial wake lock, that keeps the CPU running for your app while the screen remains off. Could you provide one more details about the purpose of the app?
I want to open my MainActivity class when screen is off. In order to do that i make two class
ScreenReceiver.java to handle Screen OFF & Screen ON Intents:
public class ScreenReceiver extends BroadcastReceiver {
private boolean screenOff;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOff = false;
}
Intent i = new Intent(context, UpdateService.class);
i.putExtra("screen_state", screenOff);
context.startService(i);
}
}
And UpdateService for implementing ScreenReceiver:
public class UpdateService extends Service {
#Override
public void onCreate() {
super.onCreate();
// register receiver that handles screen on and screen off logic
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
}
public void onStart(Context context, Intent intent, int startId) {
boolean screenOn = intent.getBooleanExtra("screen_state", false);
if (!screenOn) {
// your code
Intent intent11 = new Intent(context,MainActivity.class);
intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent11);
} else {
// your code
Intent intent11 = new Intent(context,MainActivity.class);
intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
So, when i install my app, there are no event when screen is off. please show me the problem.
Did you start the UpdateService in foreground somewhere in your application??
Firstly, intents ACTION_SCREEN_OFF and ACTION_SCREEN_ON can only be handled by a receiver registered via function registerReceiver(). Defining an IntentFilter in manifest.xml does not work for these intents.
Then, you need to make sure UpdateService:onCreate() be called in your application, otherwise ScreenReceiver:onReceiver() will never be called. You may want to do this when get intent BOOT_COMPLETED.
You may change the code to this, and do not forget define the service in manifest:
public class UpdateService extends Service {
BroadcastReceiver mReceiver = new BroadcastReceiver {
private boolean screenOff;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOff = false;
}
handleScreenAction(screenOff);
}
private void handleScreenAction(boolean screenOff) {
if (screenOff) {
// your code
Intent intent11 = new Intent(context,MainActivity.class);
intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent11);
} else {
// your code
Intent intent11 = new Intent(context,MainActivity.class);
intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
}
#Override
public void onCreate() {
super.onCreate();
// register receiver that handles screen on and screen off logic
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(mReceiver, filter);
}
#Override
public void onDestory() {
super.onDestory();
unRegisterReceiver(mReceiver);
}
public void onStart(Context context, Intent intent, int startId) {
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}