【问题标题】:I want to get time from GSM network我想从 GSM 网络获取时间
【发布时间】:2013-04-09 18:05:40
【问题描述】:

我正在使用此代码,但它通过 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();

主要是,

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

【问题讨论】:

    标签: android time timezone android-wifi


    【解决方案1】:
     long networkTS = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getTime();
    

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

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

    【讨论】:

      猜你喜欢
      • 2011-03-11
      • 1970-01-01
      • 2015-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      • 2023-03-09
      相关资源
      最近更新 更多