android – 我想从GSM网络获得时间

我正在使用这个代码,但它通过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>

最佳答案

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

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

locMan.requestSingleUpdate()并传递网络提供者

点赞