我正在尝试打开/关闭屏幕以在我的服务上工作,但到目前为止还没有运气。
目标是:当屏幕关闭时让 LED 灯(软键)熄灭,当屏幕重新打开时,使用用户在我的应用程序上输入的值打开软键灯(您将看到 TextXLActivity.getledC()从主要活动中获取 Int )
在我的主要活动中,我可以毫无问题地控制 LED 灯,使用 SeekBar 等。唯一不起作用的是接收器/服务
现在,当我转到设置、应用程序、运行服务时,我的应用程序没有在任何地方列出,我担心我的服务根本没有启动,也许这就是问题所在。
这是我的接收器:
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);
}
}
这里是我的服务:
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;
}
}
这是一个测试应用程序,我开始尝试了解接收器/服务“关系”,任何帮助将不胜感激!;)
我还注意到我的服务迫使我实施以下几行:
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
我遵循了一个关于如何让我的服务与接收器交互的教程,并且源代码最后没有类似的东西,有什么猜测吗?
此致
<?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>
编辑:这是出现错误的源代码(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;
}
}
}
新代码
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();
}
}