android 7.0快捷瓷块指南针代码

Author Avatar ZTFtrue 发表于 • 2017年09月27日 15:55 • 共 • 278 • 次浏览
public class CompassSetting extends TileService {
    private SensorEventListener mOrientationSensorEventListener;
    private SensorManager mSensorManager;
    private Handler handler;
    private int degrees = 0;
    private Bitmap drawable;
    private int state;


    // 点击的时候
    @Override
    public void onClick() {
        if (state == 0) {
            getQsTile().setState(Tile.STATE_ACTIVE);
            handler = new Handler();
            Icon icon = Icon.createWithResource(getApplicationContext(), R.drawable.compass);
            getQsTile().setIcon(icon);//设置图标
            getQsTile().updateTile();//更新Tile
            drawable = BitmapFactory.decodeResource(getResources(), R.drawable.compass);

            handler.postDelayed(mCompassViewUpdater, 30);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
                    Sensor magneticSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
                    Sensor accelerometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

                    mOrientationSensorEventListener = new SensorEventListener() {
                        float[] accelerometerValues = new float[3];
                        float[] magneticValues = new float[3];

                        @Override
                        public void onSensorChanged(SensorEvent event) {
                            if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                                accelerometerValues = event.values.clone();
                            } else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
                                magneticValues = event.values.clone();
                            }
                            float[] R = new float[9];
                            float[] values = new float[3];
                            SensorManager.getRotationMatrix(R, null, accelerometerValues, magneticValues);
                            SensorManager.getOrientation(R, values);
                            degrees = -(int) Math.toDegrees(values[0]);
                        }

                        @Override
                        public void onAccuracyChanged(Sensor sensor, int accuracy) {
                        }
                    };
                    mSensorManager.registerListener(mOrientationSensorEventListener, magneticSensor,
                            SensorManager.SENSOR_DELAY_GAME);
                    mSensorManager.registerListener(mOrientationSensorEventListener, accelerometerSensor, SensorManager.SENSOR_DELAY_GAME);
                }
            }).start();
            state = 1;
        } else {
            state = 0;
            if (getQsTile().getState() == Tile.STATE_ACTIVE) {
                Icon icon = Icon.createWithResource(getApplicationContext(), R.drawable.icon_face);
                getQsTile().setIcon(icon);//设置图标
                getQsTile().setLabel(getString(R.string.app_label));
                getQsTile().setState(Tile.STATE_INACTIVE);
                getQsTile().updateTile();

                if (mSensorManager != null) {
                    mSensorManager.unregisterListener(mOrientationSensorEventListener);
                    mOrientationSensorEventListener = null;
                }
                if (handler != null) {
                    handler.removeCallbacks(mCompassViewUpdater);
                    handler = null;
                }
                if (drawable != null) {
                    drawable.recycle();
                    drawable = null;
                }
            }
        }
    }

    // 打开下拉菜单的时候调用,当快速设置按钮并没有在编辑栏拖到设置栏中不会调用
    //在TleAdded之后会调用一次
    @Override
    public void onStartListening() {
        state = 0;
    }

    // 关闭下拉菜单的时候调用,当快速设置按钮并没有在编辑栏拖到设置栏中不会调用
    // 在onTileRemoved移除之前也会调用移除
    @Override
    public void onStopListening() {
        state = 0;
        if (getQsTile().getState() == Tile.STATE_ACTIVE) {
            Icon icon = Icon.createWithResource(getApplicationContext(), R.drawable.icon_face);
            getQsTile().setIcon(icon);//设置图标
            getQsTile().setLabel(getString(R.string.app_label));
            getQsTile().setState(Tile.STATE_INACTIVE);
            getQsTile().updateTile();

            if (mSensorManager != null) {
                mSensorManager.unregisterListener(mOrientationSensorEventListener);
                mOrientationSensorEventListener = null;
            }
            if (handler != null) {
                handler.removeCallbacks(mCompassViewUpdater);
                handler = null;
            }
            if (drawable != null) {
                drawable.recycle();
                drawable = null;
            }
        }
    }

    protected Runnable mCompassViewUpdater = new Runnable() {
        @Override
        public void run() {
            Matrix matrix = new Matrix();
            matrix.postRotate(degrees);
            Bitmap bm1 = Bitmap.createBitmap(drawable, 0, 0, drawable.getWidth(), drawable.getHeight(), matrix, true);
            Icon icon = Icon.createWithBitmap(bm1);
            getQsTile().setIcon(icon);//设置图标
            getQsTile().setLabel(String.valueOf(degrees));
            getQsTile().updateTile();//更新Tile
            handler.postDelayed(mCompassViewUpdater, 20);
        }
    };
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ztftrue.compass"
    android:versionCode="1"
    android:versionName="0.1">


    <application
        android:icon="@drawable/icon_face"
        android:label="@string/app_label"
        android:taskAffinity="net.micode.compass"
        android:allowBackup="true"
        android:fullBackupContent="@xml/backup_descriptor">

        <service
            android:name=".CompassSetting"
            android:icon="@drawable/icon_face"
            android:label="@string/app_label"
            android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
            <intent-filter>
                <action android:name="android.service.quicksettings.action.QS_TILE" />
            </intent-filter>
        </service>


    </application>

</manifest>

image

image

最后编辑于 • 2017年09月27日 16:00 •  

你尚未登录,无法进行回复。