2017-04-27 144 views
-2

我是Android開發領域的新手。我正在開發一個應用程序,我需要通過搖動設備來啓動。我怎樣才能完成這件事?我閱讀了很多主題並嘗試了幾個代碼。但他們中沒有人工作。請提供一些特定文件(或文件)的完整代碼(從上到下)。這樣我就能夠明白我在代碼中需要更改的位置。謝謝!Android - 用搖晃啓動應用程序

+1

你有什麼做了什麼?在這裏發佈一些代碼 –

+1

如果我這樣做了,我會實現一種服務,它始終在設備上工作,由BOOT_COMPLETE啓動,並且在服務中,我會聽加速計數據,確定* shake *。然後在確定 –

+0

「我閱讀了很多主題並嘗試了幾個代碼,但其中沒有一個能夠工作」 - 然後提供一個[mcve]來準確地展示您嘗試的內容並準確解釋哪些內容無效。 「請提供一些特殊文件(或文件)的完整代碼(從上到下)」 - 這不是Stack Overflow的工作方式。如果您想爲您編寫自定義代碼,請僱用某人。 – CommonsWare

回答

1

試試這個,首先創建您的服務

public class ShakeService extends Service implements SensorEventListener { 

private SensorManager sensorMgr; 
private Sensor acc; 
private long lastUpdate = -1; 
private float x, y, z; 
private float last_x, last_y, last_z; 
private static final int SHAKE_THRESHOLD = 1100; 



@Override 
public void onCreate() { 


    Toast.makeText(this, 
      "Service Started", Toast.LENGTH_SHORT).show(); 
    sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE); 
    acc=sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 

    boolean accelSupported= sensorMgr.registerListener((SensorEventListener) this, acc, SensorManager.SENSOR_DELAY_GAME); 
    long curTime11 = System.currentTimeMillis(); 

    if (!accelSupported) { 
     // on accelerometer on this device 
     sensorMgr.unregisterListener((SensorEventListener) this,acc); 
    } 

    super.onCreate(); 
} 


protected void onPause() { 
    if (sensorMgr != null) { 
     sensorMgr.unregisterListener((SensorEventListener) this,acc); 
     sensorMgr = null; 
    } 
    return; 
} 


@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 


    return super.onStartCommand(intent, flags, startId); 
} 

@Override 
public void onDestroy() { 

    if (sensorMgr != null) { 
     sensorMgr.unregisterListener((SensorEventListener) this,acc); 
     sensorMgr = null; 
    } 
    stopSelf(); 
    super.onDestroy(); 
} 

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

@Override 
public void onSensorChanged(SensorEvent sensorEvent) { 

    if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { 
     long curTime = System.currentTimeMillis(); 
     // only allow one update every 100ms. 
     if ((curTime - lastUpdate) > 100) { 
      long diffTime = (curTime - lastUpdate); 
      lastUpdate = curTime; 

      x = sensorEvent.values[SensorManager.DATA_X]; 
      y = sensorEvent.values[SensorManager.DATA_Y]; 
      z = sensorEvent.values[SensorManager.DATA_Z]; 

      float speed = Math.abs(x+y+z - last_x - last_y - last_z)/diffTime * 10000; 

      if (speed > SHAKE_THRESHOLD) { 
       Log.d("sensor", "shake detected w/ speed: " + speed); 
       Toast.makeText(this, "shake detected w/ speed: " + speed, Toast.LENGTH_SHORT).show(); 

         Intent myIntent= new Intent(this, MyActivity.class); 
         myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    /    startActivity(myIntent); 
       ////Here start your activity and your application will be started 
      } 
      last_x = x; 
      last_y = y; 
      last_z = z; 
     } 

    } 

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

} 

} 

並確保您的活動啓動服務來聲明你的服務清單中

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

現在,作爲

startService(new Intent(MainActivity.this,ShakeService.class)); 
+0

'ShakeActivity extends Service'看起來很奇怪,但總的來說回答足夠好 –

+0

其實我的代碼有活動,因爲問題只是改了它的服務所以名字可以忽略:) –

+1

現在編輯,ShakeService –