1

我有一个应用程序将系统时间与从 GPS 锁定获取的时间进行比较。如果我在我的 HTC One X 上运行该应用程序就可以正常工作。一个 X 正在运行 4.1。如果我在 HTC Desire C(2.3) 上运行该应用程序,则 Gps 的时间与系统时间相同。

例如,如果我进入手机设置并将手机的系统时间更改为向前 1 小时,那么来自 GPS 的时间也反映了这一点。谁能告诉我为什么会这样。它不是 Android 版本,因为它也适用于运行 2.3 的三星 Ace2。我认为这是特定设备的问题。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        nfcscannerapplication = (NfcScannerApplication) getApplication();
        appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(NfcBaseActivity.this.getApplicationContext());
        prefsEditor = appSharedPrefs.edit();
        mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        //setTitle(getCarername() + " is logged in");
    }




     @Override
    protected void onResume() {
         super.onResume();

         Cursor c = nfcscannerapplication.loginValidate.queryAllFromCompanyIdTable();

            if (c != null && c.getCount() > 0) {

                 mlocListener = new MyLocationListener();

                 if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
                     mlocManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, mlocListener, null);


                }else{
                    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
                }

            }






    }




    private class MyLocationListener implements LocationListener {

            @Override
            public void onLocationChanged(Location loc) {

                if(mlocListener != null){

                 mlocManager.removeUpdates(mlocListener);
                 mlocListener = null;

                }

                DateTime dt = new DateTime(loc.getTime());
                DateTimeFormatter df3 = DateTimeFormat.forPattern("yyyy-MM-dd H:mm:ss.SSS");
                String formattedExternalTime = df3.print(dt);
                Log.e(TAG, "formatted ext time = " + formattedExternalTime);
                nfcscannerapplication.setExternalTime(dt);



                String systemExternalTimeTolerance = appSharedPrefs.getString("180", null);
                Log.e(TAG, " comp opt 180 = " + systemExternalTimeTolerance);
                long toleranceInMillis = Long.parseLong(systemExternalTimeTolerance) * 1000 * 60;
                Log.e(TAG, "toleranceInMillis = " + toleranceInMillis);



                long differenceBetweenTimes = nfcscannerapplication.getInternalTime().getMillis() - 
                     nfcscannerapplication.getExternalTime().getMillis();


                Log.e(TAG, "About to make comparison1 differenceBetweenTimes = " + differenceBetweenTimes);
                if(differenceBetweenTimes > toleranceInMillis){

                     DateTimeFormatter df4 = DateTimeFormat.forPattern("yyyy-MM-dd H:mm");
                        String formattedExternalTime2 = df4.print(dt);

                    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                            NfcBaseActivity.this);

                        // set title
                        alertDialogBuilder.setTitle("Please set your phone's system time to " + formattedExternalTime2);

                        // set dialog message
                        alertDialogBuilder
                            .setMessage("Click Ok to exit app")
                            .setCancelable(false)
                            .setPositiveButton("Ok",new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,int id) {
                                    dialog.cancel();
                                    System.exit(0);
                                }
                              });

                            // create alert dialog
                            AlertDialog alertDialog = alertDialogBuilder.create();

                            // show it
                            alertDialog.show();

                }

                long differenceBetweenTimes2 = nfcscannerapplication.getExternalTime().getMillis() - 
                     nfcscannerapplication.getInternalTime().getMillis();
                Log.e(TAG, "About to make comparison2 differenceBetweenTimes2 = " + differenceBetweenTimes2);
              if(differenceBetweenTimes2 > toleranceInMillis){

                 DateTimeFormatter df4 = DateTimeFormat.forPattern("yyyy-MM-dd H:mm");
                        String formattedExternalTime2 = df4.print(dt);

                 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                            NfcBaseActivity.this);

                        // set title
                 alertDialogBuilder.setTitle("Please set your phone's system time to " + formattedExternalTime2);

                        // set dialog message
                        alertDialogBuilder
                            .setMessage("Click Ok to exit app")
                            .setCancelable(false)
                            .setPositiveButton("Ok",new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,int id) {
                                    dialog.cancel();
                                    System.exit(0);
                                }
                              });

                            // create alert dialog
                            AlertDialog alertDialog = alertDialogBuilder.create();

                            // show it
                            alertDialog.show();

                }

            }
4

0 回答 0