2015-10-17 16 views
5

Ho aggiornato il mio Nexus 5 a Marshmallow con OTA. Poiché l'attività basata su sensore semplice di aggiornamento non funziona più. Il seguente codice fa ciò che dovrebbe fare su altri dispositivi (Galaxy S4 Lolipop, AVD, ...)Dal momento che l'aggiornamento dell'OTA a Marshmallow la mia app basata sul giroscopio non funziona più

Qualcuno ha sperimentato anche questo? Mi manca qualcosa?

Ecco il codice:

build.gradle

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 22 
    buildToolsVersion "23.0.1" 

    defaultConfig { 
     applicationId "fr.rouk1.test" 
     minSdkVersion 16 
     targetSdkVersion 23 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    testCompile 'junit:junit:4.12' 
} 

AndroidManifest.xml

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

    <uses-feature 
     android:name="android.hardware.sensor.gyroscope" 
     android:required="true"/> 
    <uses-feature 
     android:name="android.hardware.sensor.accelerometer" 
     android:required="true"/> 
    <uses-feature 
     android:name="android.hardware.sensor.compass" 
     android:required="true"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN"/> 

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

</manifest> 

MainActivity.java

package fr.rouk1; 

import android.app.Activity; 
import android.content.Context; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 


public class MainActivity extends Activity implements SensorEventListener { 

    private TextView mText; 

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

     mText = (TextView) findViewById(R.id.text); 

     initSensor(); 
    } 

    private void initSensor() { 

     SensorManager sm = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE); 

     if (sm.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR) == null) { 
      sm.registerListener(this, 
        sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
        SensorManager.SENSOR_DELAY_NORMAL); 

      sm.registerListener(this, 
        sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), 
        SensorManager.SENSOR_DELAY_NORMAL); 

      sm.registerListener(this, 
        sm.getDefaultSensor(Sensor.TYPE_GYROSCOPE), 
        SensorManager.SENSOR_DELAY_FASTEST); 
     } else { 
      sm.registerListener(this, 
        sm.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR), 
        SensorManager.SENSOR_DELAY_FASTEST); 
     } 
    } 

    @Override 
    public void onSensorChanged(SensorEvent event) { 
     // this is never called 

     String s = ""; 
     for (int i = 0; i < event.values.length; i++) { 
      s = s.concat(String.format("%.4f, ", event.values[i])); 
     } 

     mText.setText(s); 
    } 

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

      case SensorManager.SENSOR_STATUS_ACCURACY_HIGH: 
       Log.d("rouk1", "SENSOR_STATUS_ACCURACY_HIGH"); 
       break; 

      case SensorManager.SENSOR_STATUS_ACCURACY_LOW: 
       Log.d("rouk1", "SENSOR_STATUS_ACCURACY_LOW"); 
       break; 

      case SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM: 
       Log.d("rouk1", "SENSOR_STATUS_ACCURACY_MEDIUM"); 
       break; 

      case SensorManager.SENSOR_STATUS_NO_CONTACT: 
       Log.d("rouk1", "SENSOR_STATUS_NO_CONTACT"); 
       break; 

      case SensorManager.SENSOR_STATUS_UNRELIABLE: 
       Log.d("rouk1", "SENSOR_STATUS_UNRELIABLE"); 
       break; 
     } 
    } 
} 
+0

Quando si verifica un arresto anomalo dell'app, è possibile controllare logcat per motivo di arresto anomalo. probabilmente ti darà un suggerimento sul perché si è schiantato. condividere il logcat può aiutare a capire il problema. guardare un codice in crash non è così utile senza sapere perché è andato in crash. –

+1

L'app non si arresta, la funzione 'onSensorChanged' non viene mai chiamata. – rouk1

+0

come fai a sapere che "onSensorChanged" non viene chiamato. https://code.google.com/p/android/issues/detail?id=3708 correlati? –

risposta

1

Finalmente trovato questo issue. Soluzione temporanea utilizza SENSOR_DELAY_GAME anziché SENSOR_DELAY_FASTEST.