Android: How to detect when phone is active and idle? - android

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
}
}
}

Related

The service does not run if the app is killed

So I have a service that checks if the Screen is ON or OFF (from here).
This is the main activity:
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onCreate();
}
public void onCreate() {
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
}
#Override
protected void onPause() {
if (ScreenReceiver.screenOff) {
System.out.println("SCREEN TURNED OFF");
} else {
System.out.println("this is when onPause() is called when the screen state has not changed ");
}
super.onPause();
}
#Override
protected void onResume() {
// only when screen turns on
if (!ScreenReceiver.screenOff) {
System.out.println("SCREEN TURNED ON");
} else {
System.out.println(" this is when onResume() is called when the screen state has not changed ");
}
super.onResume();
}
}
The Service class:
public class UpdateService extends Service {
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) {
System.out.println("Screen is off");
} else {
System.out.println("Screen is on");
}
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
Receiver:
public class ScreenReceiver extends BroadcastReceiver {
public static boolean screenOff;
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("onReceive ");
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = true;
System.out.println("SCREEN TURNED OFF on BroadcastReceiver");
Log.d("Check", "SCREEN TURNED OFF" );
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOff = false;
System.out.println("SCREEN TURNED ON on BroadcastReceiver");
Log.d("Check", "SCREEN TURNED ON" );
}
Intent i = new Intent(context, UpdateService.class);
i.putExtra("screen_state", screenOff);
context.startService(i);
}
}
The above service runs perfectly when the app is sent to the background. But when the app is killed using the task manager, the service does not work anymore. I need the service to work even when the app is killed.
What should I do to enable that? Any ideas?
The reason is that your service is running on the same process as the application. If you set android:process=":yourServiceName" in the manifest file, your service will start a new process and you can see your service.
Also, do check if your service is actually running using this method
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
To restart the service you can use an AlarmManager inside your onTaskRemoved method of the service.
#Override
public void onTaskRemoved(Intent rootIntent) {
Intent restartService = new Intent(getApplicationContext(), this.getClass());
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartService, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME, 5000, pendingIntent);
}
If they run in the same process, it is obviously that the service is terminated together with your desktop application.
Run your service as a Windows Service / Linux daemon, they have a different lifecycle compared to the processes created by the user. For example, your application will be terminated automatically on logout.

Screen Receiver Broadcast not working

i want to receive a broad cast when the screen is turned off in android. I have written the following code. This is the Receiver's code.
#Override
public void onReceive(Context context, Intent intent) {
helper = new FeedReaderDBHelper(context);
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
System.out.println("Screen Off Receiver just received something");
if(helper.getValue(FeedReaderContract.FeedEntry.onlock).equals("YES"))
{
helper.lockAll();
wasScreenOn = false;
}
else if(helper.getValue(FeedReaderContract.FeedEntry.onLock3).equals("YES"))
{
System.out.println("Running 3 minutes thread");
Runner runner = new Runner(context);
Thread t = new Thread(runner);
t.setName("LockThreeThread");
t.start();
}
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// and do whatever you need to do here
wasScreenOn = true;
}
}
I am registering and unregistering the Receiver in a Service. The code is as follows.
Registring Receiver in onStartCommand method
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
isScreenOn = pm.isScreenOn();
RegisterReceiver();
//Toast.makeText(getApplicationContext(), "Service Started", Toast.LENGTH_SHORT).show();
//System.out.println("StartRemove");
helper = new FeedReaderDBHelper(this);
names = helper.getNames();
Thread t = new Thread(new Runnable() {
#Override
public void run() {
startChecker();
}
});
t.start();
return START_STICKY;
}
And this is how i unregister in onDestroy of the Service method.
private void RegisterReceiver()
{
receiver = Factory.getScreeReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
registerReceiver(receiver, filter);
}
Please let me know what could be the problem. I have been digging into it for long time. Last two days were wasted on this. Nothing seems to work.
I think you're overly complicating this.
This is the activity you would need
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent startServiceIntent = new Intent(this, MyService.class);
startService(startServiceIntent);
}
This is the service
public class MyService extends Service {
public MyService() {
}
#Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("APP", "Service started");
ScreenOffReceiver receiver = new ScreenOffReceiver();
registerReceiver(receiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
return START_STICKY;
}
}
And this is the Broadcast Receiver
public class ScreenOffReceiver extends BroadcastReceiver {
public ScreenOffReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
Log.i("APP", "EVENT occured");
}
}
I just tried out this code and the logging worked fine.
When you start this via a service, and start a new thread to do the job in the onStartCommand() method, be noted that the thread will start every time you invoke the onCreate() of the activity.
Hope this helps

Service that detects screen lock: "Unable to start service Intent"

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"/>

Start app when screen off

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;
}
}

Screen receiver not working within my Service

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...

Categories

Resources