4

我正在使用此代码,但它通过 wifi 提供时间……当我关闭 wifi 时,它没有给我时间……我想从我的 GSM 服务提供商那里获得时间……我正在使用的代码。 .

LocationManager locMan = (LocationManager) arg0.getSystemService(Context.LOCATION_SERVICE);
        long networkTS = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getTime();
        Toast.makeText(arg0,""+networkTS,Toast.LENGTH_LONG ).show();
        Date date = new Date(networkTS);
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        format.setTimeZone(TimeZone.getTimeZone("GMT+5:30"));
        String formatted = format.format(date);
        Toast.makeText(arg0,""+formatted,Toast.LENGTH_LONG ).show();

Mainifest 是,

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.block_time"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.block_time.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".Tim">
            <intent-filter>
                <action android:name="android.intent.action.TIME_SET" />
            </intent-filter>
        </receiver>

    </application>

</manifest>
4

1 回答 1

4
 long networkTS = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getTime();

如果您想使用当前时间,getLastKnowLocation 返回的时间可能很旧

locMan.requestSingleUpdate()并通过网络提供商

于 2013-04-17T05:39:44.260 回答