2012-05-15 51 views
2

我搜索了很多關於這個仍然不連續,我想獲得當地時間我的應用程序正在運行。我可以使用日曆,但我的問題是,當用戶登錄到應用程序時,我想要正確讀取時間。這意味着實際上用戶必須在9:30登錄,但是如果他遲到了30分鐘。即使他將設備的本地時間更改回到9:30,他也會登錄該應用。 (可能性)我想讀正確的時間(即10:00)。由於這個問題,我想要獲得全球時間,而不考慮設備時間。我也可以使用服務器,但也有一些應用程序。將沒有互聯網設施。如何獲取當地時間的應用程序。正在運行,而不考慮設備時間?

我該如何做到這一點?

+4

沒有互聯網你就沒有機會。否則,從ntp服務器請求正確的時間... – WarrenFaith

回答

2

如果您的設備配備了GPS,您可以從GPS獲取正確的本地時間。用戶不能更改GPS時間。 ;)

您可以強制您的用戶打開它的GPS,否則關閉您的應用程序。

location.getTime() 

public long getTime() 
Since: API Level 1 
Returns the UTC time of this fix, in milliseconds since January 1, 1970. 

更新:

在活動

protected void btnGetPoint_onClick() { 
     try { 
      Intent intentToFire = new Intent(
        ReceiverPositioningAlarm.ACTION_REFRESH_SCHEDULE_ALARM); 
      intentToFire.putExtra(ReceiverPositioningAlarm.COMMAND, 
        ReceiverPositioningAlarm.SENDER_ACT_DOCUMENT); 

      sendBroadcast(intentToFire); 

      OnNewLocationListener onNewLocationListener = new OnNewLocationListener() { 
       @Override 
       public void onNewLocationReceived(Location location) { 
        try { 
// do what you want 

        } catch (Exception e) { 
         MessageBox.showException(ActDocument.this, e); 
        } 
       } 
      }; 

      // start listening for new location 
      ReceiverPositioningAlarm.setOnNewLocationListener(
        onNewLocationListener); 
     } catch (Exception e) { 
//... 
     } 
    } 

它的接口:

import android.location.Location; 

public interface OnNewLocationListener { 
    public abstract void onNewLocationReceived(Location location); 
} 

GPS接收器:

package org.mabna.order.receivers; 

import java.util.ArrayList; 

import org.mabna.order.utils.Farsi; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.location.GpsStatus; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.widget.Toast; 


public class ReceiverPositioningAlarm extends BroadcastReceiver { 

    public static final String COMMAND = "SENDER"; 
    public static final int SENDER_ACT_DOCUMENT = 0; 
    public static final int SENDER_SRV_POSITIONING = 1; 
    public static final int MIN_TIME_REQUEST = 5 * 1000; 
    public static final int MIN_DISTANCE = 10;// in meters 

    public static final String ACTION_REFRESH_SCHEDULE_ALARM = 
      "org.mabna.order.ACTION_REFRESH_SCHEDULE_ALARM"; 

    private static Location currentLocation; 
    private static Location prevLocation; 
    private static Context _context; 
    private String provider = LocationManager.GPS_PROVIDER; 
    private static LocationManager locationManager; 
    private static LocationListener locationListener = new LocationListener() { 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
      try { 
       String strStatus = ""; 
       switch (status) { 
       case GpsStatus.GPS_EVENT_FIRST_FIX: 
        strStatus = "GPS_EVENT_FIRST_FIX"; 
        break; 
       case GpsStatus.GPS_EVENT_SATELLITE_STATUS: 
        strStatus = "GPS_EVENT_SATELLITE_STATUS"; 
        break; 
       case GpsStatus.GPS_EVENT_STARTED: 
        strStatus = "GPS_EVENT_STARTED"; 
        break; 
       case GpsStatus.GPS_EVENT_STOPPED: 
        strStatus = "GPS_EVENT_STOPPED"; 
        break; 

       default: 
        strStatus = String.valueOf(status); 
        break; 
       } 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

     @Override 
     public void onProviderEnabled(String provider) { 
     } 

     @Override 
     public void onProviderDisabled(String provider) { 
     } 

     @Override 
     public void onLocationChanged(Location location) { 
      try { 
       gotLocation(location); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    }; 

    @Override 
    public void onReceive(final Context context, Intent intent) { 

     _context = context; 

     locationManager = (LocationManager) context 
       .getSystemService(Context.LOCATION_SERVICE); 

     if (locationManager.isProviderEnabled(provider)) { 
      locationManager.requestLocationUpdates(provider, MIN_TIME_REQUEST, 
        MIN_DISTANCE, locationListener); 

      Location gotLoc = locationManager.getLastKnownLocation(provider); 
      gotLocation(gotLoc); 
     } else { 
      Toast t = Toast.makeText(context, 
        Farsi.Convert("please turn the GPS on"), 
        Toast.LENGTH_LONG); 
      t.setGravity(Gravity.CENTER, 0, 0); 
      t.show(); 

      Intent settinsIntent = new Intent(
        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      settinsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      _context.startActivity(settinsIntent); 
     } 
    } 

    private static void gotLocation(Location location) { 
     try { 
      prevLocation = currentLocation == null ? 
        null : new Location(currentLocation); 
      currentLocation = location; 

      if (isLocationNew()) { 
       // saveLocation(location); 

       // informing the classes outside of this class that e new point 
       // received 
       OnNewLocationReceived(location); 

       stopLocationListener(); 
      } 
     } catch (Exception e) { 
      Toast.makeText(_context, 
        "error" + e.getMessage(), 
        Toast.LENGTH_SHORT).show(); 
     } 
    } 

    private static boolean isLocationNew() { 
     if (currentLocation == null) { 
      return false; 
     } else if (prevLocation == null) { 
      return true; 
     } else if (currentLocation.getTime() == prevLocation.getTime()) { 
      return false; 
     } else { 
      return true; 
     } 
    } 

    public static void stopLocationListener() { 
     if (locationManager != null) 
     { 
      locationManager.removeUpdates(locationListener); 
     } 
    } 

    // listener ---------------------------------------------------- 

    static ArrayList<OnNewLocationListener> arrOnNewLocationListener = 
      new ArrayList<OnNewLocationListener>(); 

    // Allows the user to set a OnNewLocationListener outside of this class and 
    // react to the event. 
    // A sample is provided in ActDocument.java in method: startStopTryGetPoint 
    public static void setOnNewLocationListener(
      OnNewLocationListener listener) { 
     arrOnNewLocationListener.add(listener); 
    } 

    public static void clearOnNewLocationListener(
      OnNewLocationListener listener) { 
     arrOnNewLocationListener.remove(listener); 
    } 

    // This function is called after the new point received 
    private static void OnNewLocationReceived(Location location) { 
     // Check if the Listener was set, otherwise we'll get an Exception when 
     // we try to call it 
     if (arrOnNewLocationListener != null) { 
      // Only trigger the event, when we have any listener 

      for (int i = arrOnNewLocationListener.size() - 1; i >= 0; i--) { 
       arrOnNewLocationListener.get(i).onNewLocationReceived(
         location); 
      } 
     } 
    } 
} 

在清單:

<receiver android:name=".ReceiverPositioningAlarm" > 

      <!-- this Broadcast Receiver only listens to the following intent --> 
      <intent-filter > 
       <action android:name="org.mabna.order.ACTION_REFRESH_SCHEDULE_ALARM" /> 
      </intent-filter> 
     </receiver> 
+0

我該如何實現這一點。使用GPS –

+0

這裏的位置意味着.. –

+0

我更新了我的答案 – breceivemail

相關問題