Android Compass无法在少数设备上运行

时间:2017-03-13 07:22:02

标签: android gps sensor compass motion

Android指南针仅显示视图,但未在少数设备上按预期移动。

现在这令人沮丧。我刚刚完成了一个显示指南针的应用程序。它在我的三星s4和其他一些设备上工作正常,但是当我发送 apk 文件进行测试时,最糟糕的情况就是罗盘不能用于少数设备,包括客户端设备。直到那时,我尝试了2种不同类型的代码,我在stackoverflow和其他一些网站上找到了这些代码。所有这些都在我的s4上工作正常但在客户端的设备上却没有。

我正在分享此代码。

我已经在一些Android 6.0设备上测试了这段代码,它运行得很好。我完全没有得到错误的地方。

任何帮助都将受到高度赞赏

public class QiblaActivity extends AppCompatActivity implements SensorEventListener {

    private ImageView qiblaNSEW, qiblaIcon;

    private float currentDegree = 0f;
    private float currentDegree1 = 0f;

    RelativeLayout back_Rel, bulb_Rel;

    // device sensor manager
    private SensorManager mSensorManager;
    TextView degreeTV;

    AdRequest adRequest;

    double angle;

    GPSTracker gpsTracker;
    Location location;
    private static final int PERMS_REQUEST_CODE = 123;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qibla);
        Utils.statusBarColor(getWindow(), QiblaActivity.this);

        back_Rel = (RelativeLayout) findViewById(R.id.back_Rel);
        bulb_Rel = (RelativeLayout) findViewById(R.id.bulb_Rel);
        bulb_Rel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(QiblaActivity.this, QuranBulbActivity.class);
                startActivity(intent);
            }
        });
        back_Rel.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            finish();
                                        }
                                    }
        );


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            if (hasPermissions())
            {
                gpsTracker = new GPSTracker(this);
                if (gpsTracker.canGetLocation()) {
                    startQibla();

                }
            }
            else
            {
                requestPerms();
            }


        } else {

            gpsTracker = new GPSTracker(this);
            if (gpsTracker.canGetLocation()) {
                startQibla();

            } else {
                gpsTracker.showSettingsAlert();
            }

        }


    }

    private void startQibla() {
        qiblaNSEW = (ImageView) findViewById(R.id.qibla_n_s_e_w);
        qiblaIcon = (ImageView) findViewById(R.id.qibla_icon);
        degreeTV = (TextView) findViewById(R.id.degree_TV);
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        setAds();
        angle = getAngle();
        gpsTracker = new GPSTracker(this);
        location = gpsTracker.getLocation();
        Log.d("angle", angle + "");

    }

    @Override
    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_GAME);
     /*   mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE),
                SensorManager.SENSOR_DELAY_NORMAL);*/
    }

    @Override
    protected void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(this);

    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        float degree = Math.round(sensorEvent.values[0]);
        degreeTV.setText((Float.toString(Math.round(degree - angle)) + "").replace(".0", "").replace("-", ""));
        // create a rotation animation (reverse turn degree degrees)
        RotateAnimation ra = new RotateAnimation(
                currentDegree,
                -degree,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF,
                0.5f);

        // how long the animation will take place
        ra.setDuration(210);
        // set the animation after the end of the reservation status
        ra.setFillAfter(true);

        // Start the animation
        qiblaNSEW.startAnimation(ra);
        currentDegree = -degree;


        float tempDegreeNorth = degree;


        if (tempDegreeNorth > 180) {
            tempDegreeNorth = 360 - tempDegreeNorth;
        } else {
            tempDegreeNorth = 0 - tempDegreeNorth;
        }


        Log.d(" north - angle - total", tempDegreeNorth + " - " + angle + " - " + Math.round(tempDegreeNorth + angle));
        RotateAnimation raa = new RotateAnimation(
                Math.round(tempDegreeNorth + angle),
                0.0f,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF,
                0.5f);

        // how long the animation will take place
        raa.setDuration(210);
        // set the animation after the end of the reservation status
        raa.setFillAfter(true);
        qiblaIcon.startAnimation(raa);
        currentDegree1 = -tempDegreeNorth;


    }

    private double getAngle() {
        GPSTracker gpsTracker = new GPSTracker(this);
        double lat1 = toRad(gpsTracker.getLocation().getLatitude());
        double lat2 = toRad(21.4266700);
        double lon1 = toRad(gpsTracker.getLocation().getLongitude());
        double lon2 = toRad(39.8261100);

        double dLon = (lon2 - lon1);

        double y = sin(dLon) * cos(lat2);
        double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
        double brng = Math.toDegrees(atan2(y, x));

        brng = (brng + 360);
        brng = (brng > 360) ? (brng - 360) : brng;
        return brng;

    }

    double toRad(double value) {
        value = value * Math.PI / 180;
        return value;
    }

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

    }

    private void setAds() {
        final AdView adViews = (AdView) this.findViewById(R.id.adView);

        adRequest = new AdRequest.Builder()
                .addTestDevice("253A8ECF2C80597926D6578953E69959").build();

        adViews.loadAd(adRequest);


    }

    private boolean hasPermissions() {
        int res = 0;
        //string array of permissions,
        String[] permissions = new String[]{Manifest.permission.ACCESS_FINE_LOCATION};

        for (String perms : permissions) {
            res = checkCallingOrSelfPermission(perms);
            if (!(res == PackageManager.PERMISSION_GRANTED)) {
                return false;
            }
        }
        return true;
    }

    private void requestPerms() {
        String[] permissions = new String[]{Manifest.permission.ACCESS_FINE_LOCATION};
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(permissions, PERMS_REQUEST_CODE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        boolean allowed = true;

        switch (requestCode) {
            case PERMS_REQUEST_CODE:

                for (int res : grantResults) {
                    // if user granted all permissions.
                    allowed = allowed && (res == PackageManager.PERMISSION_GRANTED);
                }

                break;
            default:
                // if user not granted permissions.
                allowed = false;
                break;
        }

        if (allowed) {
            //user granted all permissions we can perform our task.
        } else {
            // we will give warning to user that they haven't granted permissions.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)) {
                    Toast.makeText(this, "GPS Permissions denied.", Toast.LENGTH_SHORT).show();
                }
            }
        }

    }

}

0 个答案:

没有答案