把GPS经纬度放入两个字符串,写入文件


location.getLatitude() ,,,,= Double.toString(value); http://t.cn/zOoe4vv

2012-04-04


location.getLatitude() + "," + location.getLongitude(); http://t.cn/zOogIVD

2012-04-04


Android 使用全局变量 使用静态类:调用就不再过写了,直接使用类名.变量名就可以调用! http://t.cn/zOIlVWa

2012-04-04


浏览:百度搜索_安卓 全局变量 http://t.cn/zOoe2sr

2012-04-04




1  经纬度数据是字符串类型吗? 如果不是, 怎样变为字符串类型?


2  怎样设置字符串的全局变量? 在本程序中有必要吗?


3  安卓 全局变量


package basic.android.lesson26;  

  

import android.app.Activity;  

import android.content.Context;  

import android.content.Intent;  

import android.location.Criteria;  

import android.location.Location;  

import android.location.LocationListener;  

import android.location.LocationManager;  

import android.os.Bundle;  

import android.provider.Settings;  

import android.util.Log;  

import android.view.View;  

import android.widget.Button;  

import android.widget.TextView;  

import android.widget.Toast;  

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.provider.Settings.System;

import android.widget.ImageView;



  

public class TestMyGPS extends Activity {  

  

    private static final String TAG = "TestMyGPS";  

    Button mButton;  

    TextView tv1;  

    TextView mStatus;  

    TextView mTemp;  

    LocationManager mlm;  

    LocationListener locationListener;  

    String mFilter;  

   // public  String str1 = "《Android/OPhone开发完全讲义》";

   // String str1 = "《Android/OPhone开发完全讲义》";  在前面加不加Public,效果都一样

    //public static String str1 = "《Android/OPhone开发完全讲义》";

    public static String str1 = "《Android/OPhone开发完全讲义》";

      @Override  

    public void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.main);  

  

        // 定义UI组件  

        mButton = (Button) findViewById(R.id.button1);  

        tv1 = (TextView) findViewById(R.id.textView1);  

        mStatus = (TextView) findViewById(R.id.show_status);  

        mTemp = (TextView) findViewById(R.id.temp_text);  

       

  

        mButton.setOnClickListener(new View.OnClickListener() {  

            public void onClick(android.view.View view) {  

                // 转至 GPS 设置界面  

                Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);  

                startActivityForResult(intent, 0);  

            }  

        });  

        // 获取LocationManager对象  

        mlm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);  

  

        // 定义Criteria对象  

  

        // 获取GPS信息提供者  

        Criteria filter = getFilter();  

        mFilter = mlm.getBestProvider(filter, true);  

  

//        try {  

//            mlm.setTestProviderEnabled(mFilter, true);  

//        } catch (IllegalArgumentException e) {  

//            String err = "IllegalArgumentException=" + e.getMessage();  

//            Log.e(TAG, err);  

//            Toast.makeText(this, err, Toast.LENGTH_LONG).show();  

//        }  

//        openGPS();  

        gpsStatus();  

        // 位置监听器  

        locationListener = new LocationListener() {  

  

            // 当位置改变时触发  

            public void onLocationChanged(Location location) {  

                updateLocation(location);  

                Toast.makeText(TestMyGPS.this, "onLocationChanged=" + location, Toast.LENGTH_LONG).show();  

                gpsStatus();  

                mTemp.setText("onLocationChanged="+location);  

            }  

  

            // Provider失效时触发  

            public void onProviderDisabled(String arg0) {  

                gpsStatus();  

                mTemp.setText("onProviderDisabled=" + arg0);  

            }  

  

            // Provider可用时触发  

            public void onProviderEnabled(String arg0) {  

                gpsStatus();  

                mTemp.setText("onProviderEnabled=" + arg0);  

            }  

  

            // Provider状态改变时触发  

            public void onStatusChanged(String arg0, int arg1, Bundle arg2) {  

                mTemp.setText("onStatusChanged=" + arg0);  

            }  

        };  

  

        // 500毫秒更新一次,忽略位置变化  

        mlm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 3, locationListener);  

  

    }  

  

  

    private void openGPS() {  

//        if (mlm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {  

//            Toast.makeText(this, " 位置源已设置! ", Toast.LENGTH_SHORT).show();  

//            return;  

//        }  

//  

//        Toast.makeText(this, " 位置源未设置! ", Toast.LENGTH_SHORT).show();  

    }  

  

    private Criteria getFilter() {  

        Criteria criteria = new Criteria();  

        // 设置定位精确度 Criteria.ACCURACY_COARSE 比较粗略, Criteria.ACCURACY_FINE则比较精细  

        criteria.setAccuracy(Criteria.ACCURACY_FINE);  

        // 设置是否需要海拔信息 Altitude  

        criteria.setAltitudeRequired(false);  

        // 设置是否需要方位信息 Bearing  

        criteria.setBearingRequired(false);  

        // 设置是否允许运营商收费  

        criteria.setCostAllowed(true);  

        // 设置对电源的需求  

        criteria.setPowerRequirement(Criteria.POWER_LOW);  

        return criteria;  

    }  

  

    private void gpsStatus() {  

        if (mlm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {  

            mStatus.setText("GPS开启");  

        } else {  

            mStatus.setText("GPS未开启");  

            Toast.makeText(this, " 位置源未设置! ", Toast.LENGTH_SHORT).show();  

            // 转至 GPS 设置界面  

            Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);  

            startActivityForResult(intent, 0);  

        }  

    }  

  

    // 更新位置信息  

    private void updateLocation(Location location) {  

   

        if (location != null) {  

            tv1.setText("更新位置:" + location.toString() + "\n\t其中经度:" + location.getLongitude() + "\n\t其中纬度:"  

                    + location.getLatitude()); 

            try

            {

            FileOutputStream fos = new FileOutputStream(

            android.os.Environment.getExternalStorageDirectory()

            + "/file.txt");

            InputStream is = getResources().getAssets().open("file.txt");


            byte[] buffer = new byte[8192];

            int count = 0;

            while ((count = is.read(buffer)) >= 0)

            {

            fos.write(buffer, 0, count);

          

            }

           // String str1 = "《Android/OPhone开发完全讲义》";这个语句放在这个地方,只能在这个函数内部使用,

            //但是放到主Activity里, 我们就可以在所有地方使用。 类似全局变量的使用。

            str1=Double.toString(location.getLatitude());

            Toast.makeText(this,Double.toString(location.getLatitude()), Toast.LENGTH_LONG).show();

            

            fos.write(str1.getBytes("utf-8"));

                  fos.close();

            is.close();

            Toast.makeText(this, "已成功将文件写到SD卡上.", Toast.LENGTH_LONG).show();

            }

            catch (Exception e)

            {

            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();

            }

        } else {  

            tv1.setText("更新位置失败");  

        }  

    }  

    public void onClick_SaveImageToSDCard(View view)

    {

    try

    {

    FileOutputStream fos = new FileOutputStream(

    android.os.Environment.getExternalStorageDirectory()

    + "/file.txt");

    InputStream is = getResources().getAssets().open("file.txt");


    byte[] buffer = new byte[8192];

    int count = 0;

    while ((count = is.read(buffer)) >= 0)

    {

    fos.write(buffer, 0, count);

  

    }

   // String str1 = "《Android/OPhone开发完全讲义》";这个语句放在这个地方,只能在这个函数内部使用,

    //但是放到主Activity里, 我们就可以在所有地方使用。 类似全局变量的使用。

    fos.write(str1.getBytes("utf-8"));

          fos.close();

    is.close();

    Toast.makeText(this, "已成功将文件写到SD卡上.", Toast.LENGTH_LONG).show();

    }

    catch (Exception e)

    {

    Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();

    }

    }


    public void onClick_ReadImageFromSDCard(View view)

    {

    String filename = android.os.Environment.getExternalStorageDirectory()

    + "/file.txt";

    if (!new File(filename).exists())

    {

    Toast.makeText(this, "还没有往SD卡写入文件", Toast.LENGTH_LONG).show();

    return;

    }

    //ImageView imageView = (ImageView) findViewById(R.id.imageview);

    TextView textView = (TextView)findViewById(R.id.textview);

    try

    {

    FileInputStream fis = new FileInputStream(filename);

    //Bitmap bitmap = BitmapFactory.decodeStream(fis);

    //imageView.setImageBitmap(bitmap);


    //fis.close();

    //?InputStream is = openFileInput("file.txt");

    byte[] buffer = new byte[8192];

    int byteCount = fis.read(buffer);

    String str2 = new String(buffer, 0, byteCount, "utf-8");

    //TextView textView = (TextView)findViewById(R.id.textview);

    textView.setText(str2);

    fis.close();

    }

    catch (Exception e)

    {

    // TODO: handle exception

    }

    }

    private String getLocationInfo() { 

        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        Log.i("location: ",location.getLatitude() + "," + location.getLongitude()); 

        return location.getLatitude() + "," + location.getLongitude(); 

    }

    @Override  

    protected void onDestroy() {  

        mlm.removeUpdates(locationListener);  

//        mlm.setTestProviderEnabled(mFilter, false);  

        super.onDestroy();  

    }  

}  


、、、、、、、、、、、、、、、、


在本程序中成功将经纬度数据的double变成字符串, 然后写入SD卡的文件中。


转载于:https://my.oschina.net/kutengshe/blog/417677

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值