現在のソースコードでは毎回アプリ起動時にhomeとschoolの値を入力するようになっていますが、この入力を初回起動時のみにし、それ以降は初回に入力したhomeとschoolの値を使用したいです。
MainActivity.java (初回のみ表示したい)
package com.websarva.wings.android.testsample;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private final String preName = "MAIN_SETTING";
private final String dataIntPreTag ="dataIPT";
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor edit;
private int dataInt;
public static EditText hometext;
public static EditText schooltext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new ClickListener());
}
private class ClickListener implements View.OnClickListener{
@Override
public void onClick(View view) {
hometext = findViewById(R.id.home);
schooltext = findViewById(R.id.school);
Intent intent = new Intent(MainActivity.this,NewActivity.class);
intent.putExtra("home", hometext.getText().toString());
intent.putExtra("school", schooltext.getText().toString());
startActivity(intent);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自宅"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.215"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.15" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学校"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.214"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.301" />
<EditText
android:id="@+id/home"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.509"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text1"
tools:ignore="MissingConstraints" />
<EditText
android:id="@+id/school"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text2"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登録"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/school"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
NewActivity.java
package com.websarva.wings.android.testsample;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.AppLaunchChecker;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import static java.lang.Thread.sleep;
public class NewActivity extends AppCompatActivity implements OnMapReadyCallback {
public static String home;
public static String school;
private GetLatLng getLL;
private WeatherTask wt;
//private MapsActivity map;
private TextView homeIcon, schoolIcon, kasaInfo, homeDate, schoolDate, homeWeatherIcon, schoolWeatherIcon, homeWeatherTemp, schoolWeatherTemp;
private GoogleMap mMap;
private Marker Home;
MapFragment mf;
static double latitude;
static double longitude;
static double homelat,homelng,schoollat,schoollng;
static int rain=0;
private Context context;
public static String homeMarker,schoolMarker;
private CountDownLatch latch = new CountDownLatch(1);
private SharedPreferences pref;
private SharedPreferences.Editor edit;
public NewActivity(){
this.latch = latch;
}
@SuppressLint({"StaticFieldLeak", "SetTextI18n", "CommitPrefEdits"})
@Override
protected void onCreate(Bundle savedInstanceState) {
if(AppLaunchChecker.hasStartedFromLauncher(this)){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
Intent intent = this.getIntent();
home = intent.getStringExtra("home");
school = intent.getStringExtra("school");
homeIcon = (TextView) this.findViewById(R.id.home_icon);
kasaInfo = (TextView) this.findViewById(R.id.kasa_info);
schoolIcon = (TextView) this.findViewById(R.id.school_icon);
homeWeatherIcon = (TextView) this.findViewById(R.id.home_weather_icon);
schoolWeatherIcon = (TextView) this.findViewById(R.id.school_weather_icon);
homeWeatherTemp = (TextView) this.findViewById(R.id.home_weather_Temp);
schoolWeatherTemp = (TextView) this.findViewById(R.id.school_weather_Temp);
homeIcon.setText(home);
schoolIcon.setText(school);
//日時
Calendar cal = Calendar.getInstance();
int mm = cal.get(Calendar.MONTH) + 1;
int dd = cal.get(Calendar.DAY_OF_MONTH);
int hh = cal.get(Calendar.HOUR_OF_DAY);
homeDate = findViewById(R.id.home_date);
schoolDate = findViewById(R.id.school_date);
if(hh>0 && hh<=3){
homeDate.setText(" 3時 6時 9時 12時 15時 18時 21時 24時 ");
schoolDate.setText(" 3時 6時 9時 12時 15時 18時 21時 24時 ");
}
else if(hh>3 && hh<=6){
homeDate.setText(" 6時 9時 12時 15時 18時 21時 24時 3時" );
schoolDate.setText(" 6時 9時 12時 15時 18時 21時 24時 3時 ");
}
else if(hh>6 && hh<=9){
homeDate.setText(" 9時 12時 15時 18時 21時 24時 3時 6時 ");
schoolDate.setText(" 9時 12時 15時 18時 21時 24時 3時 6時 ");
}
else if(hh>9 && hh<=12){
homeDate.setText(" 12時 15時 18時 21時 24時 3時 6時 9時" );
schoolDate.setText(" 12時 15時 18時 21時 24時 3時 6時 9時 ");
}
else if(hh>12 && hh<=15){
homeDate.setText(" 15時 18時 21時 24時 3時 6時 9時 12時 ");
schoolDate.setText(" 15時 18時 21時 24時 3時 6時 9時 12時 ");
}
else if(hh>15 && hh<=18){
homeDate.setText(" 18時 21時 24時 3時 6時 9時 12時 15時 ");
schoolDate.setText(" 18時 21時 24時 3時 6時 9時 12時 15時 ");
}
else if(hh>18 && hh<=21){
homeDate.setText(" 21時 24時 3時 6時 9時 12時 15時 18時 ");
schoolDate.setText(" 21時 24時 3時 6時 9時 12時 15時 18時 ");
}
else if(hh>21){
homeDate.setText(" 24時 3時 6時 9時 12時 15時 18時 21時 ");
schoolDate.setText(" 24時 3時 6時 9時 12時 15時 18時 21時 ");
}
try{
Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addressList = gcd.getFromLocationName(home, 1);
Address address = addressList.get(0);
latitude = address.getLatitude();
longitude = address.getLongitude();
}catch(IOException e){
}
homelat = latitude;
homelng = longitude;
wt = new WeatherTask();
wt.setListener(homeCreateListener());
wt.onPostExecute("");
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
try{
Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addressList = gcd.getFromLocationName(school, 1);
Address address = addressList.get(0);
latitude = address.getLatitude();
longitude = address.getLongitude();
}catch(IOException e){
}
schoollat = latitude;
schoollng = longitude;
wt = new WeatherTask();
wt.setListener(schoolCreateListener());
wt.execute("");
//countDownLatch.countDown();
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
edit.putBoolean("Launched",true);
edit.commit();
}
else {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
Intent intent = this.getIntent();
home = intent.getStringExtra("home");
school = intent.getStringExtra("school");
homeIcon = (TextView) this.findViewById(R.id.home_icon);
kasaInfo = (TextView) this.findViewById(R.id.kasa_info);
schoolIcon = (TextView) this.findViewById(R.id.school_icon);
homeWeatherIcon = (TextView) this.findViewById(R.id.home_weather_icon);
schoolWeatherIcon = (TextView) this.findViewById(R.id.school_weather_icon);
homeWeatherTemp = (TextView) this.findViewById(R.id.home_weather_Temp);
schoolWeatherTemp = (TextView) this.findViewById(R.id.school_weather_Temp);
homeIcon.setText(home);
schoolIcon.setText(school);
//日時
Calendar cal = Calendar.getInstance();
int mm = cal.get(Calendar.MONTH) + 1;
int dd = cal.get(Calendar.DAY_OF_MONTH);
int hh = cal.get(Calendar.HOUR_OF_DAY);
homeDate = findViewById(R.id.home_date);
schoolDate = findViewById(R.id.school_date);
if(hh>0 && hh<=3){
homeDate.setText(" 3時 6時 9時 12時 15時 18時 21時 24時 ");
schoolDate.setText(" 3時 6時 9時 12時 15時 18時 21時 24時 ");
}
else if(hh>3 && hh<=6){
homeDate.setText(" 6時 9時 12時 15時 18時 21時 24時 3時" );
schoolDate.setText(" 6時 9時 12時 15時 18時 21時 24時 3時 ");
}
else if(hh>6 && hh<=9){
homeDate.setText(" 9時 12時 15時 18時 21時 24時 3時 6時 ");
schoolDate.setText(" 9時 12時 15時 18時 21時 24時 3時 6時 ");
}
else if(hh>9 && hh<=12){
homeDate.setText(" 12時 15時 18時 21時 24時 3時 6時 9時" );
schoolDate.setText(" 12時 15時 18時 21時 24時 3時 6時 9時 ");
}
else if(hh>12 && hh<=15){
homeDate.setText(" 15時 18時 21時 24時 3時 6時 9時 12時 ");
schoolDate.setText(" 15時 18時 21時 24時 3時 6時 9時 12時 ");
}
else if(hh>15 && hh<=18){
homeDate.setText(" 18時 21時 24時 3時 6時 9時 12時 15時 ");
schoolDate.setText(" 18時 21時 24時 3時 6時 9時 12時 15時 ");
}
else if(hh>18 && hh<=21){
homeDate.setText(" 21時 24時 3時 6時 9時 12時 15時 18時 ");
schoolDate.setText(" 21時 24時 3時 6時 9時 12時 15時 18時 ");
}
else if(hh>21){
homeDate.setText(" 24時 3時 6時 9時 12時 15時 18時 21時 ");
schoolDate.setText(" 24時 3時 6時 9時 12時 15時 18時 21時 ");
}
try{
Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addressList = gcd.getFromLocationName(home, 1);
Address address = addressList.get(0);
latitude = address.getLatitude();
longitude = address.getLongitude();
}catch(IOException e){
}
homelat = latitude;
homelng = longitude;
wt = new WeatherTask();
wt.setListener(homeCreateListener());
wt.onPostExecute("");
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
try{
Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addressList = gcd.getFromLocationName(school, 1);
Address address = addressList.get(0);
latitude = address.getLatitude();
longitude = address.getLongitude();
}catch(IOException e){
}
schoollat = latitude;
schoollng = longitude;
wt = new WeatherTask();
wt.setListener(schoolCreateListener());
wt.execute("");
//countDownLatch.countDown();
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
AppLaunchChecker.onActivityCreate(this);
}
private WeatherTask.Listener homeCreateListener() {
return new WeatherTask.Listener() {
@Override
public void onSuccess(String count) {
String[] res = count.split(",", 0);
latch.countDown();
String currentTemp = "";
String minTemp = "";
String maxTemp = "";
String iconId = "";
iconId += " ";
for (int i = 0; i < res.length/3; i++) {
currentTemp += " "+(res[i*3])+" ";
iconId += " "+(res[i*3+1])+" ";
if(res[i*3+1].equals("☔")){
rain+=1;
}
}
homeWeatherIcon.setText(iconId);
homeWeatherTemp.setText(currentTemp);
homeMarker = iconId;
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}
private WeatherTask.Listener schoolCreateListener() {
return new WeatherTask.Listener() {
@Override
public void onSuccess(String count) {
String[] res = count.split(",", 0);
latch.countDown();
String currentTemp="";
String minTemp="";
String maxTemp="";
String iconId="";
iconId += " ";
for(int i=0;i<res.length/3;i++){
currentTemp +=" "+(res[i*3])+" ";
iconId += " "+(res[i*3+1])+" ";
if(res[i*3+1].equals("☔")){
rain+=1;
}
}
schoolWeatherIcon.setText(iconId);
schoolWeatherTemp.setText(currentTemp);
schoolMarker = iconId;
if(rain==0){
kasaInfo.setText("今日はいい天気!傘はいらない!");
}
else if(rain<4){
kasaInfo.setText("折り畳み傘があると安心!");
}
else{
kasaInfo.setText("雨傘をもっていこう");
}
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}
private GetLatLng.Listener getlatlng() {
return new GetLatLng.Listener() {
@Override
public void onSuccess(String count) {
String[] res = count.split(",", 0);
latitude = Double.parseDouble(res[0]);
longitude = Double.parseDouble(res[1]);
}
};
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng home = new LatLng(homelat, homelng);
mMap.addMarker(new MarkerOptions().position(home).title(homeMarker).snippet(homeMarker).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
LatLng school = new LatLng(schoollat, schoollng);
mMap.addMarker(new MarkerOptions().position(school).title(schoolMarker).snippet(schoolMarker));
LatLng center = new LatLng(homelat+(homelat+schoollat)/2,homelng+(homelng+schoollng)/2);
mMap.moveCamera(CameraUpdateFactory.newLatLng(center));
zoomMap(homelat,homelng,schoollat,schoollng);
}
private void zoomMap(double homelat, double homelon, double schoollat, double schoollon){
double south,west,north,east;
if(homelat > schoollat){
south = homelat - 0.8;
north = schoollat + 0.8;
}else{
south = schoollat - 0.8;
north = homelat + 0.8;
}
if(homelon > schoollon){
west = schoollon - 0.8;
east = homelon +0.8;
}
else{
west = homelon - 0.8;
east = schoollon + 0.8;
}
LatLngBounds bounds = LatLngBounds.builder()
.include(new LatLng(south , west))
.include(new LatLng(north, east))
.build();
int width = getResources().getDisplayMetrics().widthPixels;
int height = getResources().getDisplayMetrics().heightPixels;
mMap.moveCamera(CameraUpdateFactory.
newLatLngBounds(bounds, width, height, 0));
}
}
activity_new.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f5f5f5">
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="476dp"
android:layout_height="293dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="293dp"
app:cameraZoom="8.0"
tools:content="@id/mapView"
tools:ignore="MissingConstraints"
tools:layout="@layout/activity_maps" />
<TextView
android:id="@+id/kasa_info"
android:layout_width="350dp"
android:layout_height="50dp"
android:background="#e6e6fa"
android:gravity="center"
android:layout_margin="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.54"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/mapView" />
<TextView
android:id="@+id/school_icon"
android:layout_width="77dp"
android:layout_height="84dp"
android:gravity="center"
android:layout_marginTop="8dp"
android:background="@drawable/icon_style"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/home_icon" />
<TextView
android:id="@+id/home_icon"
android:layout_width="77dp"
android:layout_height="88dp"
android:gravity="center"
android:layout_marginTop="8dp"
android:background="@drawable/icon_style"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/kasa_info" />
<TextView
android:id="@+id/school_weather_Temp"
android:layout_width="357dp"
android:layout_height="30dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:scrollbars="horizontal"
style="@style/list"
android:background="@drawable/frame_style"
android:gravity="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/school_icon"
app:layout_constraintTop_toBottomOf="@+id/school_weather_icon" />
<TextView
android:id="@+id/school_weather_icon"
android:layout_width="348dp"
android:layout_height="29dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:scrollbars="horizontal"
style="@style/list"
android:background="@drawable/frame_style"
android:gravity="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/school_icon"
app:layout_constraintTop_toBottomOf="@+id/school_date" />
<TextView
android:id="@+id/school_date"
android:layout_width="356dp"
android:layout_height="31dp"
android:scrollbars="horizontal"
style="@style/list"
android:gravity="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/school_icon"
app:layout_constraintTop_toBottomOf="@+id/home_weather_Temp" />
<TextView
android:id="@+id/home_weather_Temp"
android:layout_width="357dp"
android:layout_height="30dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:scrollbars="horizontal"
style="@style/list"
android:background="@drawable/frame_style"
android:gravity="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/home_icon"
app:layout_constraintTop_toBottomOf="@+id/home_weather_icon" />
<TextView
android:id="@+id/home_date"
android:layout_width="356dp"
android:layout_height="31dp"
android:scrollbars="horizontal"
style="@style/list"
android:gravity="center"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/home_icon"
app:layout_constraintTop_toBottomOf="@+id/kasa_info" />
<TextView
android:id="@+id/home_weather_icon"
android:layout_width="348dp"
android:layout_height="29dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:scrollbars="horizontal"
style="@style/list"
android:gravity="center"
android:background="@drawable/frame_style"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/home_icon"
app:layout_constraintTop_toBottomOf="@+id/home_date" />
</androidx.constraintlayout.widget.ConstraintLayout>