2016-10-18 7 views
0

Я хочу обнаружить движение мобильного телефона в фоновом режиме через службы, но я ничего не получаю в своем сервисе.Android: Сенсор ACCELEROMETER Не работает в службе

Это MYSERVICE

public class MyService extends Service implements SensorEventListener { 

     private SensorManager mSensorManager; 
     static int count=0; 
     private ShakeEventListener mSensorListener; 
    /** OnShakeListener that is called when shake is detected. */ 


    public MyService() { 
    } 

    @Override// 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show(); 
     mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
     //registering Sensor 
     Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 

     mSensorManager.registerListener(mSensorListener, 
       mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
       SensorManager.SENSOR_DELAY_UI); 

     //then you should return sticky 
     return Service.START_STICKY; 
    } 
    @Override 
    public void onCreate() { 

    } 
    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO: Return the communication channel to the service. 
     throw new UnsupportedOperationException("Not yet implemented"); 
    } 


    @Override 
    public void onSensorChanged(SensorEvent se) { 
     // get sensor when Change 
     Toast.makeText(getApplicationContext(), "Detected", Toast.LENGTH_SHORT).show(); 


    } 


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

} 

manifest.xml

<?xml version="1.0" encoding="utf-8"?> 

<uses-permission android:name="android.permission.CAMERA" /> 
<uses-permission android:name="android.permission.VIBRATE" /> 
<uses-permission android:name="android.permission.CAMERA" /> 
<uses-permission android:name="android.permission.FLASHLIGHT" /> 

<uses-feature android:name="android.hardware.camera" /> 
<uses-feature android:name="android.hardware.camera.autofocus" /> 
<uses-feature android:name="android.hardware.camera.flash" /> 

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

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

На сенсорном смене Тост не отображается, помощь будет оценена.

+0

Позвонили вам свой сервис от деятельности? – Ramprasad

ответ

0

Он работал, когда я добавить следующие строки в OnCreate

 mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 

Отредактировано MyService

public class MyService extends Service implements SensorEventListener { 

     private SensorManager mSensorManager; 
     static int count=0; 
     private ShakeEventListener mSensorListener; 
    /** OnShakeListener that is called when shake is detected. */ 


     public MyService() { 
     } 

    @Override// 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show(); 
     mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
     //registering Sensor 
     Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 

     mSensorManager.registerListener(mSensorListener, 
       mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
       SensorManager.SENSOR_DELAY_UI); 

     //then you should return sticky 
     return Service.START_STICKY; 
    } 
    @Override 
    public void onCreate() { 
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
     //add this line only 
    } 
    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO: Return the communication channel to the service. 
     throw new UnsupportedOperationException("Not yet implemented"); 
    } 


    @Override 
    public void onSensorChanged(SensorEvent se) { 
     // get sensor when Change 
     Toast.makeText(getApplicationContext(), "Detected", Toast.LENGTH_SHORT).show(); 


    } 


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

} 

 Смежные вопросы

  • Нет связанных вопросов^_^