我正在使用这个代码,但是它通过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>
long networkTS = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getTime();
如果你想使用当前时间,getLastKnowLocation返回的时间可以是旧的
locMan.requestSingleUpdate()
并传递网络提供者