Служба, которая обнаруживает блокировку экрана: «Невозможно запустить сервисные намерения»Android

Форум для тех, кто программирует под Android
Anonymous
Служба, которая обнаруживает блокировку экрана: «Невозможно запустить сервисные намерения»

Сообщение Anonymous »

Я пытаюсь поймать, когда экран моего устройства выключен или включен. Я посмотрел на этот ответ здесь. Однако я не совсем понял это. Когда я проверяю его, я получаю предупреждение о том, что услуга не смогла быть создана: невозможно начать намерение обслуживания ... не найдено. Вот мой приемник и 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);
}}
< /code>

Service: < /p>

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

Вот мое основное действие: < /p>

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);
}}
< /code>

Заранее спасибо. < /p>

Подробнее здесь: https://stackoverflow.com/questions/339 ... ice-intent

Вернуться в «Android»