2

私はこれがまだ成功していないことをよくグーグルで調べました。アプリが実行されている現地時間を取得したいと思います。calenderを使用してそれを取得できますが、私の問題は、ユーザーがアプリにログインしたときに正しい時刻を読みたいことです。これは、実際にはユーザーが 9:30 にログインする必要があることを意味しますが、30 分遅刻した場合です。デバイスの現地時間を 9:30 に戻しても、アプリにログインします。(可能性) 正確な時刻 (つまり 10:00 ) を読みたい。この問題のため、デバイスの時間に関係なくグローバル時間を取得したいと考えています。これにもサーバーを使用できますが、アプリの場合もあります。インターネット機能はありません。

どうすればこれを達成できますか?

4

1 に答える 1

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>
于 2012-05-15T05:00:34.800 に答える