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