Pregunta

I am trying to start a Service that implements SensorEventListener. I am getting an error in my logcat claiming:

    android.content.ActivityNotFoundException:Unable to find explicit activity class 
{com.devicemoved/com.devicemoved.ShakeWakeupService}; 
have you declared this activity in your AndroidManifest.xml?

My Service is declared in my manifest as shown:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.devicemoved"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.devicemoved.launcherGo"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name="com.devicemoved.ShakeWakeupService" />
</application>

The Activity

public class ShakeWakeupService extends Service implements SensorEventListener {

    private Context mContext;

    SensorManager mSensorEventManager;

    Sensor mSensor;
    // BroadcastReceiver for handling ACTION_SCREEN_OFF.
    public BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Check action just to be on the safe side.
            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                Log.v("shake mediator screen off", "trying re-registration");
                // Unregisters the listener and registers it again.
                mSensorEventManager.unregisterListener(ShakeWakeupService.this);
                mSensorEventManager.registerListener(ShakeWakeupService.this,
                        mSensor, SensorManager.SENSOR_DELAY_NORMAL);
            }
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        Log.v("shake service startup", "registering for shake");

        mContext = getApplicationContext();
        // Obtain a reference to system-wide sensor event manager.
        mSensorEventManager = (SensorManager) mContext
                .getSystemService(Context.SENSOR_SERVICE);

        // Get the default sensor for accel
        mSensor = mSensorEventManager
                .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        // Register for events.
        mSensorEventManager.registerListener(this, mSensor,
                SensorManager.SENSOR_DELAY_NORMAL);

        // Register our receiver for the ACTION_SCREEN_OFF action. This will
        // make our receiver
        // code be called whenever the phone enters standby mode.
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
        registerReceiver(mReceiver, filter);

    }

    @Override
    public void onDestroy() {
        // Unregister our receiver.
        unregisterReceiver(mReceiver);

        // Unregister from SensorManager.
        mSensorEventManager.unregisterListener(this);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // We don't need a IBinder interface.
        return null;
    }

    public void onShake() {
        // Poke a user activity to cause wake?

    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // not used right now
    }

    // Used to decide if it is a shake
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
            return;

        Log.v("sensor", "sensor change is verifying");

    }
}

The only thing I could possibly think of is that I am suppose to declare a broadcast receiver since I am using it in my class but I am not sure how to do that in this instance because I am not extending Broadcastreceiver.

I am calling this class from an activity with a button.

Any help will be great Thank You

¿Fue útil?

Solución

You can already declared the package in the manifest tag, you should only need the .ShakeWakeupService part. Also make sure there is an empty constructor available for the system to construct your service

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top