Android - 随机启动应用程序

时间:2017-04-27 11:10:07

标签: android android-sensors shake sensormanager

我是这个Android开发领域的新手。我正在开发一个应用程序,我需要通过摇动设备来启动它。我怎样才能完成这件事?我读了很多线程并尝试了几个代码。但他们没有工作。请善意提出特定文件(或文件)的完整代码(从上到下)。这样我就能理解我需要在代码中改变的确切位置。谢谢!

1 个答案:

答案 0 :(得分: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));