NPLS
NPLS

Reputation: 529

Start new Activity On Sensor Changed?

How can i start new Activity on Accelererometer (On Shake): when i shake my phone the app crashes - the accelerometer run also in background

public class Shaker_Service extends Service implements SensorEventListener{
    private static final String TAG = "MyService";
    private SensorManager sensorManager;



 AppPreferences appPrefs;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        Toast.makeText(this, "My Service CREATED", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onCreate");

    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "My Service STOP", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy");    
    }

    @Override
    public void onStart(Intent intent, int startid) {

        Toast.makeText(this, "My Service START", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onStart");

        sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
        // add listener. The listener will be HelloAndroid (this) class
        sensorManager.registerListener(this, 
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);

    }



    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // check sensor type
                if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){

                    // assign directions
                    float x=event.values[0];
                    float y=event.values[1];
                    float z=event.values[2];

                    if (x>10){

startActivity(newIntent("com.examles.MESSAGE"));


                    }
                }

        }
}

Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.examples"
      android:versionCode="1"
      android:versionName="1.0">

        <activity android:name=".Message_Note"
                 android:label="@string/app_name"
                  >
            <intent-filter>
                <action android:name="com.examples.MESSAGE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

 <service android:enabled="true" android:name=".Shaker_Service" />
    </application>

</manifest> 

Message_Note.java :

public class Message_Note extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.message);
}

}

image of error (LogCat) https://mega.co.nz/#!SUpTAbAC!WC9y_Xlh5GEW9AY9_5WbpXwkYA4Xk-o9WgaXvN6jpLk

Upvotes: 0

Views: 2229

Answers (1)

Levente Kurusa
Levente Kurusa

Reputation: 1866

Try using:

Intent intent = new Intent(this, theActivityYouWantToStart.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);


This is the correct way to start an Activity from inside a service.

Upvotes: 1

Related Questions