Back to project page ShareSDK-for-Android.
The source code is released under:
MIT License
If you think the Android project ShareSDK-for-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Offical Website:http://www.mob.com//ww w. j a va 2 s . com * Support QQ: 4006852216 * Offical Wechat Account:ShareSDK (We will inform you our updated news at the first time by Wechat, if we release a new version. If you get any problem, you can also contact us with Wechat, we will reply you within 24 hours.) * * Copyright (c) 2013 mob.com. All rights reserved. */ package cn.sharesdk.onekeyshare; import static cn.sharesdk.framework.utils.R.*; 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.util.FloatMath; import android.view.Window; import android.widget.ImageView; import android.widget.Toast; import android.widget.ImageView.ScaleType; import cn.sharesdk.framework.FakeActivity; /** a demo for shake-to-share */ public class Shake2Share extends FakeActivity implements SensorEventListener { // sensor state update interval private static final int UPDATE_INTERVAL = 100; // shaking detecting threshold, the smaller the more sensitive private static final int SHAKE_THRESHOLD = 1500; private OnShakeListener listener; private SensorManager mSensorManager; private long mLastUpdateTime; private float mLastX; private float mLastY; private float mLastZ; private boolean shaken; public void setOnShakeListener(OnShakeListener listener) { this.listener = listener; } public void setActivity(Activity activity) { super.setActivity(activity); int resId = getBitmapRes(activity, "ssdk_oks_shake_to_share_back"); if (resId > 0) { activity.setTheme(android.R.style.Theme_Dialog); activity.requestWindowFeature(Window.FEATURE_NO_TITLE); Window win = activity.getWindow(); win.setBackgroundDrawableResource(resId); } } public void onCreate() { startSensor(); int resId = getBitmapRes(activity, "ssdk_oks_yaoyiyao"); if (resId > 0) { ImageView iv = new ImageView(activity); iv.setScaleType(ScaleType.CENTER_INSIDE); iv.setImageResource(resId); activity.setContentView(iv); } resId = getStringRes(activity, "shake2share"); if (resId > 0) { Toast.makeText(activity, resId, Toast.LENGTH_SHORT).show(); } } private void startSensor() { mSensorManager = (SensorManager) activity.getSystemService(Context.SENSOR_SERVICE); if (mSensorManager == null) { throw new UnsupportedOperationException(); } Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); if (sensor == null) { throw new UnsupportedOperationException(); } boolean success = mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME); if (!success) { throw new UnsupportedOperationException(); } } public void onDestroy() { stopSensor(); } private void stopSensor() { if (mSensorManager != null) { mSensorManager.unregisterListener(this); mSensorManager = null; } } public void onSensorChanged(SensorEvent event) { long currentTime = System.currentTimeMillis(); long diffTime = currentTime - mLastUpdateTime; if (diffTime > UPDATE_INTERVAL) { if(mLastUpdateTime != 0) { float x = event.values[0]; float y = event.values[1]; float z = event.values[2]; float deltaX = x - mLastX; float deltaY = y - mLastY; float deltaZ = z - mLastZ; float delta = FloatMath.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ) / diffTime * 10000; if (delta > SHAKE_THRESHOLD) { if (!shaken) { shaken = true; finish(); } if (listener != null) { listener.onShake(); } } mLastX = x; mLastY = y; mLastZ = z; } mLastUpdateTime = currentTime; } } public void onAccuracyChanged(Sensor sensor, int accuracy) { } public static interface OnShakeListener { public void onShake(); } }