Search code examples
androidandroid-serviceandroid-sensors

My Application Crashes when I Register a Sensor Listener


In Android I am creating a Service, which will listen for sensor value change. But my application keeps crashing whenever I open it. If I remove the Lines

 sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);

It works fine

I have created the Service class as following:

public class CustomSensorService extends Service implements SensorEventListener{

    static SensorManager sensorManager;
    static Sensor mAccelerometer;
    static Context mContext;
    static final String LOG_TAG = "SimpleService";

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this,"Service Created",Toast.LENGTH_SHORT).show();
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        Log.d( LOG_TAG, "onSensorChanged" );
        Toast.makeText(mContext,"Heelooo" , Toast.LENGTH_SHORT).show();

        stopSelf();


    }

}

And my Activity as:

public class MainActivity extends Activity {

    Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        intent = new Intent(this, CustomSensorService.class);
    }
    @Override 
    public void onResume(){
        super.onResume();
        startService(intent);  

    }
}

This is the Manifest

 <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.learnandroidservice.MainActivity"
            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=".CustomSensorService"/>
    </application>

Trace: enter image description here


Solution

  • mContext is null - you need to initialise it before you pass it to Toast.makeText().

    Service is a context so you can just call

    Toast.makeText(this,"Heelooo" , Toast.LENGTH_SHORT).show(); 
    

    It works when you remove the lines you mention because then the SensorListener is never registered, and onSensorChanged is never called.