導航:首頁 > 操作系統 > androidgpsservice

androidgpsservice

發布時間:2022-09-20 07:35:17

android怎樣自動打開gps

1.加入許可權
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

2. /**
* 判斷GPS是否開啟,GPS或者AGPS開啟一個就認為是開啟的
* @param context
* @return true 表示開啟
*/
public static final boolean isOPen(final Context context) {
LocationManager locationManager
= (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
// 通過GPS衛星定位,定位級別可以精確到街(通過24顆衛星定位,在室外和空曠的地方定位準確、速度快)
boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// 通過WLAN或移動網路(3G/2G)確定的位置(也稱作AGPS,輔助GPS定位。主要用於在室內或遮蓋物(建築群或茂密的深林等)密集的地方定位)
boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (gps || network) {
return true;
}

return false;
}

② 怎麼把android gps坐標位置上傳到伺服器

在配備Android系統的手機中,一般都配備了GPS設備。Android為我們獲取GPS數據提供了很好的介面。本文來說一下如何使用Android獲取GPS的經緯度。
1 從Service繼承一個類。
2 創建startService()方法。
3 創建endService()方法 重載onCreate方法和onDestroy方法,並在這兩個方法裡面來調用startService以及endService。
4 在startService中,通過getSystemService方法獲取Context.LOCATION_SERVICE。
5 基於LocationListener實現一個新類。默認將重載四個方法onLocationChanged、onProviderDisabled、onProviderEnabled、onStatusChanged。對於onLocationChanged方法是我們更新最新的GPS數據的方法。一般我們的操作都只需要在這里進行處理。
6 調用LocationManager的requestLocationUpdates方法,來定期觸發獲取GPS數據即可。在onLocationChanged函數裡面可以實現我們對得到的經緯度的最終操作。
7 最後在我們的Activity裡面通過按鈕來啟動Service,停止Service。
示意代碼如下:
package com.offbye.gpsservice;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class GPSService extends Service {
// 2000ms
private static final long minTime = 2000;
// 最小變更距離10m
private static final float minDistance = 10;
String tag = this.toString();
private LocationManager locationManager;
private LocationListener locationListener;
private final IBinder mBinder = new GPSServiceBinder();
public void startService() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSServiceListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance,
locationListener);
}
public void endService() {
if (locationManager != null && locationListener != null) {
locationManager.removeUpdates(locationListener);
}
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return mBinder;
}
@Override
public void onCreate() {
//
startService();
Log.v(tag, "GPSService Started.");
}
@Override
public void onDestroy() {
endService();
Log.v(tag, "GPSService Ended.");
}
public class GPSServiceBinder extends Binder {
GPSService getService() {
return GPSService.this;
}
}
}
GPSServiceListener的實現
package com.offbye.gpsservice;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationProvider;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class GPSServiceListener implements LocationListener {
private static final String tag = "GPSServiceListener";
private static final float minAccuracyMeters = 35;
private static final String hostUrl = "http://doandroid.info/gpsservice/position.php?";
private static final String user = "huzhangyou";
private static final String pass = "123456";
private static final int ration = 10;
private final DateFormat timestampFormat = new SimpleDateFormat("yyyyMMddHHmmss");
public int GPSCurrentStatus;
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if (location != null) {
if (location.hasAccuracy() && location.getAccuracy() <= minAccuracyMeters) {
// 獲取時間參數,將時間一並Post到伺服器端
GregorianCalendar greg = new GregorianCalendar();
TimeZone tz = greg.getTimeZone();
int ffset = tz.getOffset(System.currentTimeMillis());
greg.add(Calendar.SECOND, (offset / 1000) * -1);
StringBuffer strBuffer = new StringBuffer();
strBuffer.append(hostUrl);
strBuffer.append("user=");
strBuffer.append(user);
strBuffer.append("&pass=");
strBuffer.append(pass);
strBuffer.append("&Latitude=");
strBuffer.append(location.getLatitude());
strBuffer.append("&Longitude=");
strBuffer.append(location.getLongitude());
strBuffer.append("&Time=");
strBuffer.append(timestampFormat.format(greg.getTime()));
strBuffer.append("&Speed=");
strBuffer.append(location.hasSpeed());
doGet(strBuffer.toString());
Log.v(tag, strBuffer.toString());
}
}
}
// 將數據通過get的方式發送到伺服器,伺服器可以根據這個數據進行跟蹤用戶的行走狀態
private void doGet(String string) {
// TODO Auto-generated method stub
//
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
GPSCurrentStatus = status;
}
}
摘自 offbye的技術博客

③ Android 怎樣穩定的獲取原生GPS數據

定位了就有經緯度,沒定位,則一直閃的;
一般是4個星的時候 會定位。
所以可以根據是否獲取有效的經緯度信息來判斷是否定位。

衛星的個數 sv_status也是可以通過API獲取的。LOCATION類
給一段咱的代碼,看看就知道了。

locationManager.addGpsStatusListener(statusListener);//偵聽GPS狀態

private GpsStatus.Listener statusListener = new GpsStatus.Listener()

{

public void onGpsStatusChanged(int event)

{

// TODO Auto-generated method stub

GpsStatus gpsStatus= locationManager.getGpsStatus(null);

Log.v(TAG,"GPS status listener ");

//Utils.DisplayToastShort(GPSService.this, "GPS status listener ");

switch(event)

{

case GpsStatus.GPS_EVENT_FIRST_FIX:{

//第一次定位時間UTC gps可用

//Log.v(TAG,"GPS is usable");

int i=gpsStatus.getTimeToFirstFix();

Utils.DisplayToastShort(GPSService.this, "GPS 第一次可用 "+i);

Utils.setGPSStatus(Utils.GPS_STATUS.START);

break;

}

case GpsStatus.GPS_EVENT_SATELLITE_STATUS:{//周期的報告衛星狀態

//得到所有收到的衛星的信息,包括 衛星的高度角、方位角、信噪比、和偽隨機號(及衛星編號)

Iterable<GpsSatellite> allSatellites;

allSatellites = gpsStatus.getSatellites();

Iterator<GpsSatellite>iterator = allSatellites.iterator();

int numOfSatellites = 0;

int maxSatellites=gpsStatus.getMaxSatellites();

while(iterator.hasNext() && numOfSatellites<maxSatellites){

numOfSatellites++;

iterator.next();

}

Log.v(TAG,"GPS is **unusable** "+ numOfSatellites +" "+ maxSatellites);

if( numOfSatellites < 3){

// Utils.DisplayToastShort(GPSService.this, "***衛星少於3顆***");

Utils.setGPSStatus(Utils.GPS_STATUS.STOP);

} else if(numOfSatellites > 7){

Utils.setGPSStatus(Utils.GPS_STATUS.START);

}

break;

}

case GpsStatus.GPS_EVENT_STARTED:{

//Utils.DisplayToastShort(GPSService.this, "GPS start Event");

break;

}

case GpsStatus.GPS_EVENT_STOPPED:{

//Utils.DisplayToastShort(GPSService.this, "GPS **stop*** Event");

Utils.setGPSStatus(Utils.GPS_STATUS.STOP);

break;

}

default :

break;

}

}

};

④ 如何刷新Android手機的GPS狀態

locationManager = (LocationManager)LocationActivity.this.getSystemService(Context.LOCATION_SERVICE);
locationManager.addGpsStatusListener(statusListener);
/**
* 衛星狀態監聽器
*/
private List<GpsSatellite> numSatelliteList = new ArrayList<GpsSatellite>(); // 衛星信號
private final GpsStatus.Listener statusListener = new GpsStatus.Listener() {
public void onGpsStatusChanged(int event) { // GPS狀態變化時的回調,如衛星數
LocationManager locationManager = (LocationManager) LocationActivity.this.getSystemService(Context.LOCATION_SERVICE);
GpsStatus status = locationManager.getGpsStatus(null); //取當前狀態
String satelliteInfo = updateGpsStatus(event, status);
if("".equals(satelliteInfo)){
Toast.makeText(LocationActivity.this, "GPS works bad !", Toast.LENGTH_LONG).show();
isGpsEnable=false;
}
}
};
private String updateGpsStatus(int event, GpsStatus status) {
StringBuilder sb2 = new StringBuilder("");
if (status == null) {
sb2.append("搜索到衛星個數:" +0);
} else if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
int maxSatellites = status.getMaxSatellites();
Iterator<GpsSatellite> it = status.getSatellites().iterator();
numSatelliteList.clear();
int count = 0;
while (it.hasNext() && count <= maxSatellites) {
GpsSatellite s = it.next();
numSatelliteList.add(s);
count++;
}
sb2.append("搜索到衛星個數:" + numSatelliteList.size());
}

return sb2.toString();
}

⑤ mqtt 怎麼實現android以後台service的方式獲取gps數據,並定時發送到伺服器

1 從Service繼承一個類。
2 創建startService()方法。
3 創建endService()方法 重載onCreate方法和onDestroy方法,並在這兩個方法裡面來調用startService以及endService。
4 在startService中,通過getSystemService方法獲取Context.LOCATION_SERVICE。
5 基於LocationListener實現一個新類。默認將重載四個方法onLocationChanged、onProviderDisabled、onProviderEnabled、onStatusChanged。對於onLocationChanged方法是我們更新最新的GPS數據的方法。一般我們的操作都只需要在這里進行處理。
6 調用LocationManager的requestLocationUpdates方法,來定期觸發獲取GPS數據即可。在onLocationChanged函數裡面可以實現我們對得到的經緯度的最終操作。
7 最後在我們的Activity裡面通過按鈕來啟動Service,停止Service。
示意代碼如下:
package com.offbye.gpsservice;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class GPSService extends Service {
// 2000ms
private static final long minTime = 2000;
// 最小變更距離10m
private static final float minDistance = 10;
String tag = this.toString();
private LocationManager locationManager;
private LocationListener locationListener;
private final IBinder mBinder = new GPSServiceBinder();
public void startService() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSServiceListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance,
locationListener);
}
public void endService() {
if (locationManager != null && locationListener != null) {
locationManager.removeUpdates(locationListener);
}
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return mBinder;
}

⑥ 現時android平台上實現gps獲取經緯度有什麼好方法

GPS獲取經緯度可以參考以下方法:

  1. manifest中添加許可權:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

2. 實例化一個locationmanager:

LocationManger
locationmanager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

3. 判斷GPS是否打開,未打開提示打開GPS:

if (!locationmanager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

Toast.makeText(this, "請開啟GPS導航...", Toast.LENGTH_SHORT).show();

return;

}

4. 監聽位置信息變化:

private LocationListener locationListener=new LocationListener() {

/**

* 位置信息變化時觸發

*/

public void onLocationChanged(Location location) {

updateView(location);

Log.i(TAG, "時間:"+location.getTime());

Log.i(TAG, "經度:"+location.getLongitude());

Log.i(TAG, "緯度:"+location.getLatitude());

Log.i(TAG, "海拔:"+location.getAltitude());

}

5. 載入監聽器:

locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1,
locationListener);

⑦ 如何利用Android編程實現GPS定位

您好,很高興為您解答。


一、准備工作
需要如下三種軟體:
1. Eclipse
2. Android SDK
3. 開發Android程序的Eclipse 插件

為了開始我們的工作,首先要安裝Eclipse,然後從Google的網站獲得Android SDK,並且安裝Eclipse插件。

二、Activity類
每一種移動開發環境都有自己的基類。如J2ME應用程序的基類是midlets,BREW的基類是applets,而Android程序的基類是 Activity。這個activity為我們提供了對移動操作系統的基本功能和事件的訪問。這個類包含了基本的構造方法,鍵盤處理,掛起來恢復功能,以 及其他底層的手持設備的訪問。實質上,我們的應用程序將是一個Activity類的擴展。在本文中讀者將會通過例子學習到如何使用Activity類來編 寫Android程序。下面是一個簡單的繼承Activity的例子。

{
publicvoidonCreate(Bundleparams){
super.onCreate(params);
setContentView(R.layout.main);
}
publicbooleanonKeyDown(intkeyCode,KeyEventevent){
returntrue;
}
}

三 View類
View類是Android的一個超類,這個類幾乎包含了所有的屏幕類型。但它們之間有一些不同。每一個view都有一個用於繪畫的畫布。這個畫布可以用 來進行任意擴展。本文為了方便起見,只涉及到了兩個主要的View類型:定義View和Android的XML內容View。在上面的代碼中,使用的是 「Hello World」 XML View,它是以非常自然的方式開始的。
如果我們查看一下新的Android工程,就會發現一個叫main.xml的文件。在這個文件中,通過一個簡單的XML文件,描述了一個屏幕的布局。這個 簡單的xml文件的內容如下:

<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHoriz
android:text=""
/>
</RelativeLayout>

上面的內容的功能看起來非常明顯。這個特殊文件定義了一個相關的布局,這就意味著通過一個元素到另一個元素的關系或是它們父元素的關系來描述。對於視圖來 說,有一些用於布局的方法,但是在本文中只關注於上述的xml文件。
RealtiveLayout中包含了一個填充整個屏幕的文本框(也就是我們的LocateMe activity)。這個LocateMe activity在默認情況下是全屏的,因此,文本框將繼承這個屬性,並且文本框將在屏幕的左上角顯示。另外,必須為這個XML文件設置一個引用數,以便 Android可以在源代碼中找到它。在默認情況下,這些引用數被保存在R.java中,代碼如下:

publicfinalclassR{
publicstaticfinalclasslayout{
publicstaticfinalintmain=0x7f030001;
}
}

視圖也可以被嵌套,但和J2ME不同,我們可以將定製的視圖和Android團隊發布的Widgets一起使用。在J2ME中,開發人員被迫選擇 GameCanvas和J2ME應用程序畫布。這就意味著如果我們想要一個定製的效果,就必須在GameCanvas上重新設計我們所有的widget。 Android還不僅僅是這些,視圖類型也可以混合使用。Android還帶了一個 widget庫,這個類庫包括了滾動條,文本實體,進度條以及其他很多控制項。這些標準的widget可以被重載或被按著我們的習慣定製。現在讓我們來進入 我們的例子。


四、Android實例

這個演示應用程序將演示了用戶的當前的經度和緯度(在文本框中顯示)。onCreate構造方法將和上面的例子基本相同,除了在其中加入了鍵盤處理,現在 讓我們看一下onKeyDown的代碼。

publicbooleanonKeyDown(intkeyCode,KeyEventevent){
if(keyCode!=KeyEvent.KEYCODE_DPAD_CENTER||m_bLoading)
{
returntrue;
}
m_bLoading=true;
getLocation();
returntrue;
}

下面讓我們來解釋一下這段代碼,首先,這段代碼檢查了當前被按下的鍵,但還沒有開始處理。而是在getLocation方法中處理這一切的。然後,將裝載 flag標志以及調用getLocation方法,下面是getLocation方法的代碼。

privatevoidgetLocation(){
Locationloc;
LocationManagerlocMan;
LocationProviderlocPro;
List<LocationProvider>proList;
setContentView(R.layout.laoding);
locMan=(LocationManager)getSystemService(LOCATION_SERVICE);
proList=locMan.getProviders();
locPro=proList.get(0);
loc=locMan.getCurrentLocation(locPro.getName());
Lat=(float)loc.getLatitude();
Lon=(float)loc.getLongitude();
CreateView();
setContentView(customView);
}

到這為止,程序開始變得更有趣了。但是不幸的是,Google關於之方面的文檔還是比較少了。在程序的變數聲明之後,我們需要演示一些裝載信息。 R.layout.loading符合了另一個簡單的XML布局視圖。通過簡單地調用setContentView方法可以使用轉載信息重繪屏幕。
讀者要注意的是:在編譯時,Android會預先將所有的XML布局數據包裝起來。如果我們想在編譯後變化布局屬性,按著規定,我們必須在源程序中做這些 事。
獲得LocationManager的唯一方法是通過getSystemService()方法的調用。通過使用LocationManager, 我們可以獲得一個位置提供者的列表。在一個真實的手持設備中,這個列表包含了一些GPS服務。實際上,我們希望選擇更強大,更精確,最後不帶有其他附加服 務的GPS。現在,在模擬器中提供了一個用於測試的GPS,這個GPS來自San Francisco。定製的GPS文件可以可以被上傳,並進行測試。如果我們要測試更復雜的應用,來自San Francisco的GPS可能並不適合。
目前我們可以使用位置管理器和位置提供者進行getCurrentLocation的調用。這個方法返回本機的當前位置的一個快照,這個快照將以 Location對象形式提供。在手持設備中,我們可以獲得當前位置的經度和緯度。現在,使用這個虛擬的手持設備,我們可以獲得這個例子程序的最終結果: 建立了顯示一個定製的視圖。

五、使用定製視圖
在最簡單的窗體中,一個Android中的視圖僅僅需要重載一個onDraw方法。定製視圖可以是復雜的3D實現或是非常簡單的文本形式。下面的 CreateView方法列出了上面看到的內容。

publicvoidCreateView(){
customView=newCustomView(this);
}

這個方法簡單地調用了CustomView對象的構造方法。CustomView類的定義如下:

{
LocateMeoverlord;
publicCustomView(LocateMepCtx){
super(pCtx);
overlord=pCtx;
}
publicvoidonDraw(Canvascvs){
Paintp=newPaint();
StringsLat="Latitude:"+overlord.getLat();
StringsLon="Longitude:"+overlord.getLon();
cvs.drawText(sLat,32,32,p);
cvs.drawText(sLon,32,44,p);
}
}

這個定製的Android視圖獲得了經度和違度的測試數據,並將這些數據顯示在屏幕上。這要求一個指向LocateMe的指針,Activity類是整 個應用程序的核心。它的兩個方法是構造方法和onDraw方法。這個構造方法調用了超類的構造方法以及引起了Activity指針的中斷。onDraw方 法將建立一個新的Paint對象(這個對象封裝了顏色、透明度以及其他的主題信息),這個對象將會訪問顏色主題。在本程序中,安裝了用於顯示的字元串,並 使用畫布指針將它們畫到屏幕上。這個和我們了解的J2ME游戲的畫布看起來非常類似。

六、Android展望
從純粹的開發觀點看,Android是一個非常強大的SDK。它使用基於XML的布局和定製視圖聯合了起來。並可以使用滾動條、地圖以及其他的組件。所以 的這一切都可以被重載,或由開發人員來定製。但它所提供的文檔非常粗糙。在文檔中並沒有象SMS等技術,但是從整體上來看Android SDK,還是非常有希望的。也非常符合Google承諾的「First Look」SDK。現在我們要做的就是等待Google發布第一個基於Android的手機,並使用它。


如若滿意,請點擊右側【採納答案】,如若還有問題,請點擊【追問】


希望我的回答對您有所幫助,望採納!


~O(∩_∩)O~

⑧ 如何刷新Android手機的GPS狀態

locationManager = (LocationManager)LocationActivity.this.getSystemService(Context.LOCATION_SERVICE);
locationManager.addGpsStatusListener(statusListener);
/**
* 衛星狀態監聽器
*/
private List<GpsSatellite> numSatelliteList = new ArrayList<GpsSatellite>(); // 衛星信號
private final GpsStatus.Listener statusListener = new GpsStatus.Listener() {
public void onGpsStatusChanged(int event) { // GPS狀態變化時的回調,如衛星數
LocationManager locationManager = (LocationManager) LocationActivity.this.getSystemService(Context.LOCATION_SERVICE);
GpsStatus status = locationManager.getGpsStatus(null); //取當前狀態
String satelliteInfo = updateGpsStatus(event, status);
if("".equals(satelliteInfo)){
Toast.makeText(LocationActivity.this, "GPS works bad !", Toast.LENGTH_LONG).show();
isGpsEnable=false;
}
}
};
private String updateGpsStatus(int event, GpsStatus status) {
StringBuilder sb2 = new StringBuilder("");
if (status == null) {
sb2.append("搜索到衛星個數:" +0);
} else if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
int maxSatellites = status.getMaxSatellites();
Iterator<GpsSatellite> it = status.getSatellites().iterator();
numSatelliteList.clear();
int count = 0;
while (it.hasNext() && count <= maxSatellites) {
GpsSatellite s = it.next();
numSatelliteList.add(s);
count++;
}
sb2.append("搜索到衛星個數:" + numSatelliteList.size());
}

return sb2.toString();
}

⑨ android在service中能不能用gps定位

原因: 1丶檢查手機是否有明顯的碰撞痕跡,可能是由於手機進行了碰撞而導致手機的GPS損壞從而無法進行定位。有的人手機摔了之後無線網就不能用了,或者藍牙不能開,都是因為手機碰撞造成的。 2丶進入網路地圖的APP,檢查是否是最新版本的APP手機應用,如果不是,請及時進行更新,如果是經常處在wifi環境下,可以勾選wifi環境下自動更新的選項。 3丶在手機的安全中心中,查看是否給了網路地圖APP的許可權,如果沒有的話,可以打開,這樣一來,手機授權網路地圖GPS定位服務,從而使網路地圖可以定位自己的位置。 4丶檢查一下自己下的網路地圖APP是否是真正的官方授權的正版軟體。

⑩ Android中的GPS如何判斷是否定位

我們在做手機開發的時候,往往需要獲取用戶當前的位置,以使用戶獲得更好的體驗。這就需要我們在程序中寫出判斷用戶是否打開GPS定位系統,並對用戶做出提示。
判斷用戶是否打開GPS代碼如下:

12345678910111213

public static final boolean isOPen(final Context context) { LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); // 通過GPS衛星定位,定位級別可以精確到街(通過24顆衛星定位,在室外和空曠的地方定位準確、速度快) boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); // 通過WLAN或移動網路(3G/2G)確定的位置(也稱作AGPS,輔助GPS定位。主要用於在室內或遮蓋物(建築群或茂密的深林等)密集的地方定位) boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (gps || network) { return true; } return false; }

而有些時候用戶並不能配合我們打開GPS系統,如果我的應用必須打開GPS(比如說一些租車、送餐類型APP需要獲取用戶的位置信息),就需要強制用戶打開GPS定位。代碼如下:

123456789101112

public static final void openGPS(Context context) { Intent GPSIntent = new Intent(); GPSIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); GPSIntent.addCategory("android.intent.category.ALTERNATIVE"); GPSIntent.setData(Uri.parse("custom:3")); try { PendingIntent.getBroadcast(context, 0, GPSIntent, 0).send(); } catch (CanceledException e) { e.printStackTrace(); } }

附錄:需要在Mainfast.xml中添加的許可權

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

閱讀全文

與androidgpsservice相關的資料

熱點內容
中軸線趨勢指標源碼 瀏覽:721
labview源碼代碼 瀏覽:61
15匹谷輪壓縮機圖片 瀏覽:818
bsp分割演算法 瀏覽:22
高手選股公式源碼 瀏覽:682
脆皮軟心球解壓視頻十分鍾 瀏覽:404
如何關閉蘋果app同步充值 瀏覽:766
視頻壓縮發送微信 瀏覽:856
程序員段子大全圖片 瀏覽:760
javaudp心跳 瀏覽:429
阿里賣家app如何分配詢盤 瀏覽:828
如何拔打中國移動人工伺服器 瀏覽:505
傳奇歸來為什麼連不上伺服器 瀏覽:555
壓縮機潤滑油在哪個位置工作 瀏覽:141
張翰解壓減幾 瀏覽:775
你好法語1教師用書pdf 瀏覽:486
手機解壓縮後文件在哪 瀏覽:472
linux內核框架 瀏覽:720
程序員的秘密通關攻略 瀏覽:201
怎麼下載索米app 瀏覽:307