Note that there are some explanatory texts on larger screens.

plurals
  1. POScreen receiver not working within my Service
    text
    copied!<p>I am trying to get my Screen ON/OFF to work on my Service, but so far no luck.</p> <p>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 ) </p> <p>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</p> <p>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. </p> <p>Here is my Receiver:</p> <pre><code>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); } } </code></pre> <p>And here my Service:</p> <pre><code>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; } } </code></pre> <p>This is a Test app I started to try and understand the Receiver/Service "relation", any help would be appreciated! ;)</p> <p>I also noticed that my Service forces me to implement this lines:</p> <pre><code>@Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } </code></pre> <p>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? </p> <p>Best regards</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test.xl" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="10"/&gt; &lt;uses-permission android:name="com.sonyericsson.illumination.permission.ILLUMINATION"/&gt; &lt;application android:icon="@drawable/ic_launcher" android:label="@string/app_name" &gt; &lt;activity android:name=".TestXLActivity" android:label="@string/app_name" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;receiver android:name=".ScreenReceiver"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action..ACTION_SCREEN_ON" /&gt; &lt;action android:name="android.intent.action.ACTION_SCREEN_OFF" /&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p>Edit: Here is the source code where the error appears (startService(led)):</p> <pre><code>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; } } </code></pre> <p>}</p> <p><strong>NEW CODE</strong></p> <pre><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; } } </code></pre> <p>.</p> <pre><code>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(); } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload