I want to get time from GSM network

∥☆過路亽.° 提交于 2019-12-10 09:32:07

问题


I am using this code but it gives time by wifi..when i turn off wifi,it is not giving me the time... I want to get the time from my GSM service provider... The code which I am using..

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();

The Mainifest is,

<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>

回答1:


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

Time returned by getLastKnowLocation could be old if you want current time use

locMan.requestSingleUpdate() and pass the network provider



来源:https://stackoverflow.com/questions/16052222/i-want-to-get-time-from-gsm-network

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!