ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

在android中的活动之间传递字符串

2019-09-17 05:25:47  阅读:167  来源: 互联网

标签:android android-activity location latitude-longitude


我已搜索了不少地方,但我还没有找到任何有效的解决方案,我真的需要帮助!
我正在创建一个需要将经度和纬度字符串从一个活动传递到另一个活动的应用程序.
我怎样才能做到这一点???看看我的代码:LocationActivity.java需要将字符串传递给另一个活动,另一个我没有粘贴到这里.需要传递的字符串名为:“latLongString”

LocationActivity.java:

import android.R.string;
import android.app.Activity;
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.view.View;
import android.widget.TextView;

public class LocationActivity extends Activity {
private String Location;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LocationManager locManager;
    locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,500.0f, locationListener);
    Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    updateWithNewLocation(location);
    if(location != null)                                
    {
        double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    }  
  }


private void updateWithNewLocation(Location location) {
    TextView myLocationText = (TextView)findViewById(R.id.LocationWord);
    String latLongString = "";
    if (location != null) {
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        latLongString = "Lat:" + lat + "\nLong:" + lng;
        //This is where i need to pass the string to the other activity

    } else {
        latLongString = "No location found";
    }
     myLocationText.setText("Your Current Position is:\n" +
            latLongString);
}

private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        updateWithNewLocation(location);
    }

    public void onProviderDisabled(String provider) {
        updateWithNewLocation(null);
    }

    public void onProviderEnabled(String provider) {
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
};


}

其他活动:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.Locale;

import android.R.string;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.os.Environment;
import android.telephony.gsm.SmsMessage;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.location.LocationManager;
import android.location.LocationListener;

public class FindAndroidActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button Nextbutton1, Nextbutton2, Nextbutton3, TestLocationService, EditSettings;
TextView Cordinates, Adress, FindAndroid, TextView;
EditText LocationWord;
private LocationManager locManager;
private LocationListener locListener;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
       setContentView(R.layout.firsttimescreen);
       Nextbutton1 = (Button)findViewById(R.id.Nextbutton1);
       Nextbutton1.setOnClickListener(this);
}
public void onClick(View src) {
    switch(src.getId()) {
    case R.id.Nextbutton1:
        setContentView(R.layout.setup);
        Nextbutton2 = (Button)findViewById(R.id.Nextbutton2);
        TestLocationService = (Button)findViewById(R.id.TestLocationService);
        TestLocationService.setOnClickListener(this);
        Nextbutton2.setOnClickListener(this);
        break;
    case R.id.Nextbutton2:
        setContentView(R.layout.setup1);
        Nextbutton3 = (Button)findViewById(R.id.Nextbutton3);
        LocationWord = (EditText)findViewById(R.id.LocationWord);
        LocationWord.requestFocus(View.FOCUS_DOWN);
        Nextbutton3.setOnClickListener(this);
        break;
    case R.id.Nextbutton3:
        setContentView(R.layout.main);
        EditSettings = (Button)findViewById(R.id.EditSettings);
        EditSettings.setOnClickListener(this);
        break;
    case R.id.TestLocationService:
        Bundle extras = getIntent().getExtras();
        if(extras !=null) {
            String value = extras.getString("KEY");             
        }
        Cordinates = (TextView)findViewById(R.id.Cordinates);
        Cordinates.setText(value);
            //This does not work because the string "value" isn't availible outside the braces,
            //obviously. How do i make it availible there???
        break;
    case R.id.EditSettings:
        setContentView(R.layout.setup1);
        Nextbutton3 = (Button)findViewById(R.id.Nextbutton3);
        LocationWord = (EditText)findViewById(R.id.LocationWord);
        LocationWord.requestFocus(View.FOCUS_DOWN);
        Nextbutton3.setOnClickListener(this);
        break;
    }
}

}

解决方法:

在您的LocationActivity类中:

Intent i = new Intent(this, FindAndroidActivity.class);
i.putExtra("KEY",YourData);

在FindAndroidActivity类中

Bundle extras = getIntent().getExtras();
if(extras !=null) {
    String value = extras.getString("KEY");
}

标签:android,android-activity,location,latitude-longitude
来源: https://codeday.me/bug/20190917/1808796.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有