馬上加入Android 台灣中文網,立即免費下載應用遊戲。
您需要 登錄 才可以下載或查看,沒有帳號?註冊
x
我用Eclipse看程式碼都沒有驚嘆號跟X
但是都無法正常開啟 開啟就會馬上停止
這是我的程式碼
package com.example.gps123;
import android.app.Activity;
import android.location.Location; //可以在指定的時間提供location
import android.location.LocationListener; //為自訂億的class android library沒有這個class
import android.location.LocationManager; //提供可以存取系統location的service
import android.os.Bundle; //傳送參數會使用
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; //快顯訊息
import android.content.Context;
public class MainActivity extends Activity {
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1;
//設定超過一個距離(尺)他就會做更新location的動作
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000;
//設定超過一個時間(毫秒)他就會做更新location的動作
protected LocationManager locationManager;
protected Button retrieveLocationButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
retrieveLocationButton = (Button)findViewById(R.id.button1);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//get location service
//requestLocationUpdates(long minTime, float minDistance, Criteria criteria, PendingIntent intent)
//Criteria: 訂定尋找location provider的標準
//PendingIntent: 處理即將發生的事件
//PendingIntent與Intent的差異:Intent會隨著Activiy的消失而消失,PendingIntent不會
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, //Name of the GPS provider
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
retrieveLocationButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showCurrentLocation();
}
});
}
protected void showCurrentLocation() {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//取得最後得知道provider資訊
if (location != null) {
String message = String.format(
"Current Location
Longitude: %1$s
Latitude: %2$s",
location.getLongitude(), location.getLatitude());
Toast.makeText(MainActivity.this, message,Toast.LENGTH_LONG).show();
}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location
Longitude: %1$s
Latitude: %2$s",
location.getLongitude(), location.getLatitude());
//將想要印出的資料用string.format的方法存入string
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
//Toast.LENGTH_LONG:以最長的時間週期呈現文字
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(MainActivity.this, "Provider status changed",
Toast.LENGTH_LONG).show();
//當provider的時候會顯示
}
public void onProviderDisabled(String s) {
Toast.makeText(MainActivity.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
//當device的GPS沒有開啟的時候他會顯示
}
public void onProviderEnabled(String s) {
Toast.makeText(MainActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
//當device將GPS打開的時候他會顯示
}
}
}
AndroidManifest.xml 中的設定
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gps123"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="com.google.android.maps"/>
<meta-data android:name="com.google.android.maps.v3.API_KEY" android:value="AIzaSyApRB2VEXrF9mZUgDDTMWcDfJDyKlBHJME"/>
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
//允許一個程式存取精確位置
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
//允許程式建立模組使用
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
//允許程式存取CellID或WiFi熱點來取得粗略的位置
</manifest> |

|