您的位置:首页 > 编程语言

11-30 论文总结 无法关闭 GPS的问题 currenlocation相关代码

2011-11-30 17:51 671 查看
1.写了 安全登录算法,实现 登录 clientBean 和 LoginSecurity ,serverBean

2.将原来的 CurrentLocation 的方法 分出是 GPS 还是 Net

3.等到问题,是无法关闭 GPS

http://www.eoeandroid.com/archiver/tid-52190.html?page=1

还是没有解决方法



1.主代码 逻辑层

package com.studio.android.chp08.ex01;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class CurrentLocation3 extends Activity {
/** Called when the activity is first created. */

private static String Location_GPS = "GPS";
private static String Location_NET = "NET";
private static String Location_SHUT_GPS ="SHUT";

private String type=Location_GPS;

private Location local=null;
private Location localBackup=null;
private int i=0;

Button LocationType ;

/**
* LocationListener 是一个接口
* new LocationListener() 去实例化这个接口
* new LocationListener() {}对接口的方法进行实现
*
* 整体而言是个 成员变量 复制语句,可以放到 前面去
* 这个成员变量 是 LocationListener 类型
*/
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);//地址改变
//	local=location;
System.out.println("the onLocationChanged");
}
public void onProviderDisabled(String provider){
updateWithNewLocation(null);//地址失效
System.out.println("the onProviderDisabled");
}
public void onProviderEnabled(String provider){
System.out.println("the onProviderEnabled");

}

public void onStatusChanged(String provider, int status,Bundle extras){
System.out.println("the onStatusChanged");

}
};

public Location getLocal() {
return local;
}

public void setLocal(Location local) {
this.local = local;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);  //选择布局,用 main 里面的布局方式

LocationType = (Button)findViewById(R.id.LocationType);

Button btv=(Button)findViewById(R.id.GoSecond);

Button btshut = (Button)findViewById(R.id.SHUT);

final LocationManager locationManager;
String serviceName = Context.LOCATION_SERVICE;

locationManager = (LocationManager)getSystemService(serviceName);

LocationType.setText("用GPS获取位置");

LocationType.setOnClickListener(new View.OnClickListener(){

public void onClick(View v) {
// TODO 实现这个接口的方法
String provider=null;
i=0;

if(type.equals(Location_GPS))

provider = LocationManager.GPS_PROVIDER;
else {
provider = LocationManager.NETWORK_PROVIDER;

}

if(provider.equals(LocationManager.GPS_PROVIDER)){
type=Location_NET;
CurrentLocation3.this.LocationType.setText("用NET获取位置");
}
else{
type=Location_GPS;
CurrentLocation3.this.LocationType.setText("用GPS获取位置");
}

localBackup = locationManager.getLastKnownLocation(provider);

if(localBackup!=null)
updateWithNewLocation(localBackup);

locationManager.requestLocationUpdates(provider, 2000, 0,
locationListener);//注册了监听器

}

});

btv.setOnClickListener(new View.OnClickListener(){

public void onClick(View v) {
// TODO 实现这个接口的方法

Intent intent = new Intent();

Bundle bundle=new Bundle();

if(CurrentLocation3.this.local!=null){

double lati = CurrentLocation3.this.local.getLatitude();

double logti=CurrentLocation3.this.local.getLongitude();

bundle.putString("Lati",String.valueOf(lati));

bundle.putString("Lnti",String.valueOf(logti));

intent.putExtra("loca", bundle);

//						intent.putExtra("Lati",lati);
//						intent.putExtra("Lnti", logti);
}
intent.setClass(CurrentLocation3.this, LocationName.class);
startActivity(intent);

}

});

btshut.setOnClickListener(new View.OnClickListener(){

public void onClick(View v) {
// TODO 实现这个接口的方法

toggleGps(CurrentLocation3.this);

}

});
}

private void updateWithNewLocation(Location location) {

String latLongString;
TextView myLocationText;

myLocationText = (TextView)findViewById(R.id.myLocationText);
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();

this.setLocal(location);//更新 本类的location
System.out.println(" the lat is "+lat+"  the lng is "+lng);

latLongString = "第  "+ ++i+" 次  更新    纬度是:" + lat + "\n经度:" + lng;

} else {
latLongString = "无法获取地理信息";
}

myLocationText.setText("您当前的位置是:\n" +latLongString);
}

/**
* Gets the state of GPS location.
*
* @param context
* @return true if enabled.
*/
private static boolean getGpsState(Context context) {
ContentResolver resolver = context.getContentResolver();
boolean open = Settings.Secure.isLocationProviderEnabled(resolver,
LocationManager.GPS_PROVIDER);
System.out.println("getGpsState:" + open);
return open;
}

/**
* Toggles the state of GPS.
*
* @param context
*/
private void toggleGps(Context context) {
ContentResolver resolver = context.getContentResolver();
boolean enabled = getGpsState(context);
Settings.Secure.setLocationProviderEnabled(resolver,
LocationManager.GPS_PROVIDER, !enabled);
}

}


2.显示解析后 的地址名称

package com.studio.android.chp08.ex01;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.widget.TextView;

/**
*
* 得到具体的 地名
* @author Administrator
*
*/

public class LocationName extends Activity {
private   String  latLongString=null;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addressname);

List<Address> addList = null;
Geocoder ge = new Geocoder(this,Locale.CHINA);

TextView  myLocationText=(TextView) findViewById(R.id.CityName);
Intent GetIntent=this.getIntent();

Bundle bd = GetIntent.getBundleExtra("loca");// 根据bundle的key得到对应的对象

if(bd==null){

System.out.println(" it is  null");
}

else if (bd!=null){
System.out.println("it is not null");

//
//
double  lati2 = Double.valueOf(bd.getString("Lati"));
double  longti2 = Double.valueOf(bd.getString("Lnti"));

System.out.println(lati2);
System.out.println(longti2);

//        	double lati2=GetIntent.getDoubleExtra("Lati", 30.2743244);
//        	double longti2=GetIntent.getDoubleExtra("Lnti", 120.1162423);

try {
addList = ge.getFromLocation(lati2,longti2, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

if(addList!=null && addList.size()>0){
latLongString=new String();
for(int i=0; i<addList.size(); i++){
Address ad = addList.get(i);
latLongString += "\n";
latLongString +=ad.getAddressLine(0)+ad.getAddressLine(1)+ad.getAddressLine(2);

}

}

myLocationText.setText("您当前的位置是:\n" +latLongString);

}

}


addressxml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView

android:id="@+id/CityName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"

/>
<ListView

android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>


mainxml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!--  开始放组建  -->
<TextView
android:id="@+id/myLocationText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>

<Button
android:id="@+id/LocationType"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/GetLocation"

/>

<Button
android:id="@+id/GoSecond"

android:layout_width="fill_parent"
android:layout_height="wrap_content"

android:text="@string/GoSecond"
/>

<Button
android:id="@+id/SHUT"

android:layout_width="fill_parent"
android:layout_height="wrap_content"

android:text="@string/SHUT"
/>
</LinearLayout>


3.stringxml

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<resources>
<string name="hello">Hello World, CurrentLocation</string>
<string name="app_name">CurrentLocation</string>
<string name="GetLocation">SO I get ,go</string>
<string name="GoSecond">go second page</string>
<string name="SHUT">SHUT GPS</string>
</resources>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: