开始新的传感器活动更改?

时间:2013-08-07 11:34:02

标签: android eclipse android-intent android-service

如何在Accelererometer(On Shake)上启动新活动:当我摇动手机时应用程序崩溃          - 加速度计也在后台运行

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 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:

公共类Message_Note扩展了Activity {

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

}

错误图像(LogCat) https://mega.co.nz/#!SUpTAbAC!WC9y_Xlh5GEW9AY9_5WbpXwkYA4Xk-o9WgaXvN6jpLk

1 个答案:

答案 0 :(得分:1)

尝试使用:

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

Intent intent = new Intent(this, theActivityYouWantToStart.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);
这是从服务内部启动Activity的正确方法。