sadelbrid
sadelbrid

Reputation: 531

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.

Upvotes: 1

Views: 125

Answers (1)

Onik
Onik

Reputation: 19959

It looks like the service hasn't been declared in the manifest file. Add its declaration within the <application> tag:

<service android:name=".UpdateService"/>

Upvotes: 1

Related Questions