felix
felix

Reputation: 454

How can I share these variables between an Activity and a Class?

I have an app that uses a Service. For some reason though, the service does not recognize the variables "placeArrayList", "latitudeArrayList", "longitudeArrayList", and "destination".
What can I do here?

MainActivity.java

public class MainActivity extends AppCompatActivity {
PlacePicker.IntentBuilder builder;
int PLACE_PICKER_REQUEST;
ListView placeListView;
ArrayList<String> placeArrayList;
ArrayList<String> latitudeArrayList;
ArrayList<String> longitudeArrayList;
ArrayAdapter<String> arrayAdapter;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
ArrayList<Double> latitudeList;
Integer counter;
LocationManager locationManager;
String provider;
double lat;
double lng;
Place place;
static Set<String> set;
static Set<String> set1;
static Set<String> set2;
Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
placeListView = (ListView) findViewById(R.id.placeListView);
latitudeList = new ArrayList<>();
placeArrayList = new ArrayList<String>();
latitudeArrayList = new ArrayList<String>();
longitudeArrayList = new ArrayList<String>();



LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.getBestProvider(new Criteria(), false);


if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    // TODO: Consider calling
    //    ActivityCompat#requestPermissions
    // here to request the missing permissions, and then overriding
    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
    //                                          int[] grantResults)
    // to handle the case where the user grants the permission. See the documentation
    // for ActivityCompat#requestPermissions for more details.
    return;
}
final Location location = locationManager.getLastKnownLocation(provider);


final Location destination = new Location(provider);
destination.setLatitude(location.getLatitude());
destination.setLongitude(location.getLongitude());
Log.e("shit", String.valueOf(location.distanceTo(destination)));


ListView listView = (ListView) findViewById(R.id.placeListView);

SharedPreferences sharedPreferences = this.getSharedPreferences("bro", Context.MODE_PRIVATE);

set = sharedPreferences.getStringSet("names", null);

set1 = sharedPreferences.getStringSet("lats", null);
set2 = sharedPreferences.getStringSet("lngs", null);


placeArrayList.clear();
latitudeArrayList.clear();
longitudeArrayList.clear();

if (set != null) {

    placeArrayList.addAll(set);
    latitudeArrayList.addAll(set1);
    longitudeArrayList.addAll(set2);

} else {

    placeArrayList.add("Hold to delete");
    set = new HashSet<String>();
    set.addAll(placeArrayList);
    sharedPreferences.edit().putStringSet("names", set).apply();
    latitudeArrayList.add(String.valueOf("66.56083"));
    set1 = new HashSet<String>();
    set1.addAll(latitudeArrayList);
    sharedPreferences.edit().putStringSet("lats", set1).apply();
    longitudeArrayList.add(String.valueOf("39.3232"));
    set2 = new HashSet<String>();
    set2.addAll(longitudeArrayList);
    sharedPreferences.edit().putStringSet("lngs", set2).apply();

}



arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, placeArrayList);

listView.setAdapter(arrayAdapter);





placeListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {


        new AlertDialog.Builder(MainActivity.this)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle("Are you sure?")
                .setMessage("Do you want to delete this place?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {


                        placeArrayList.remove(position);

                        latitudeArrayList.remove(position);

                        SharedPreferences sharedPreferences = MainActivity.this.getSharedPreferences("bro", Context.MODE_PRIVATE);

                        if (set == null) {

                            set = new HashSet<String>();
                            set1 = new HashSet<String>();
                            set2 = new HashSet<String>();

                        } else {

                            set.clear();
                            set1.clear();
                            set2.clear();

                        }


                        set.addAll(placeArrayList);
                        set1.addAll(latitudeArrayList);
                        set2.addAll(longitudeArrayList);
                        sharedPreferences.edit().remove("names").apply();
                        sharedPreferences.edit().remove("lats").apply();
                        sharedPreferences.edit().remove("lngs").apply();
                        sharedPreferences.edit().putStringSet("names", set).apply();
                        sharedPreferences.edit().putStringSet("lats", set1).apply();
                        sharedPreferences.edit().putStringSet("lngs", set2).apply();
                        arrayAdapter.notifyDataSetChanged();


                    }
                })
                .setNegativeButton("No", null)
                .show();


        return false;
    }
});




final android.os.Handler handler = new android.os.Handler();
Runnable run = new Runnable() {
    @Override
    public void run() {

        if (location != null) {


            lat = location.getLatitude();

            lng = location.getLongitude();


            if (placeArrayList.size() != 0) {
                for (int i = 0; i < placeArrayList.size(); i++) {


                    Log.e("hello", String.valueOf(Float.parseFloat(latitudeArrayList.get(i))));

                    destination.setLatitude(Double.parseDouble(latitudeArrayList.get(i)));
                    destination.setLongitude(Double.parseDouble(longitudeArrayList.get(i)));
                    Log.e("distancemeters", String.valueOf(location.distanceTo(destination)));

                    if (location.distanceTo(destination)<100) {




                        AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

                        audioManager.setStreamVolume(AudioManager.STREAM_RING, AudioManager.RINGER_MODE_SILENT, AudioManager.FLAG_SHOW_UI);


                    } else {



                    }

                }


            }
        }


        handler.postDelayed(this, 10000);
    }
};
handler.post(run);










arrayAdapter.notifyDataSetChanged();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        PLACE_PICKER_REQUEST = 1;
        builder = new PlacePicker.IntentBuilder();
        pickPlace();

    }
});
}


public void pickPlace() {

try {
    startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
    e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
    e.printStackTrace();
}


}




@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == PLACE_PICKER_REQUEST) {
    if (resultCode == RESULT_OK) {
        place = PlacePicker.getPlace(data, this);
        // String toastMsg = String.format("Place: %s", place.getName());
        //Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();



        placeArrayList.add(String.valueOf(place.getName()));
        latitudeArrayList.add(String.valueOf(place.getLatLng().latitude));
        longitudeArrayList.add(String.valueOf(place.getLatLng().longitude));
        arrayAdapter.notifyDataSetChanged();
        startService(new Intent(getBaseContext(), myService.class));

        SharedPreferences sharedPreferences = getSharedPreferences("bro", Context.MODE_PRIVATE);

        if (set == null) {

            set = new HashSet<String>();
            set1 = new HashSet<String>();
            set2 = new HashSet<String>();

        } else {

            set.clear();
            set1.clear();
            set2.clear();

        }


        arrayAdapter.notifyDataSetChanged();
        set.addAll(placeArrayList);
        set1.addAll(latitudeArrayList);
        set2.addAll(longitudeArrayList);
        sharedPreferences.edit().remove("names").apply();
        sharedPreferences.edit().remove("lats").apply();
        sharedPreferences.edit().remove("lngs").apply();
        sharedPreferences.edit().putStringSet("names", set).apply();
        sharedPreferences.edit().putStringSet("lats", set1).apply();
        sharedPreferences.edit().putStringSet("lngs", set2).apply();





    }

}
}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
    return true;
}

return super.onOptionsItemSelected(item);
}


}

myService.java

public class myService extends Service implements LocationListener {
Double lat;
Double lng;
@Override
public IBinder onBind(Intent arg0) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Let it continue running until it is stopped.
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return START_STICKY;


}

@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}

@Override
public void onLocationChanged(Location location) {

if (location != null) {


   lat = location.getLatitude();

    lng = location.getLongitude();


    if (placeArrayList.size() != 0) {
        for (int i = 0; i < placeArrayList.size(); i++) {


            Log.e("hello", String.valueOf(Float.parseFloat(latitudeArrayList.get(i))));

            destination.setLatitude(Double.parseDouble(latitudeArrayList.get(i)));
            destination.setLongitude(Double.parseDouble(longitudeArrayList.get(i)));
            Log.e("distancemeters", String.valueOf(location.distanceTo(destination)));

            if (location.distanceTo(destination)<100) {




                AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

                audioManager.setStreamVolume(AudioManager.STREAM_RING, AudioManager.RINGER_MODE_SILENT, AudioManager.FLAG_SHOW_UI);


            } else {



            }

        }


    }
}

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}
}

Upvotes: 0

Views: 58

Answers (3)

Phant&#244;maxx
Phant&#244;maxx

Reputation: 38098

You can make the variables static.
And then refer them by prefixing the Activity name to them.

Upvotes: 0

sanjay
sanjay

Reputation: 636

Don't make variables as static. Because it can cause memory leaks. And it is not at all a good idea. Better way is to write an Interface and implement it in MainActivity class. This interface will have methods for assigning new values to variables. And call the Interface methods from your service.

Upvotes: 1

camlunt
camlunt

Reputation: 48

Since the Service is a separate class it makes sense that it wouldn't be able to have access to the fields of MainAcitivity, since they are out of scope to "myService".

When you want to share information between a calling Activity and the Service being called, you'll be able to pass data in a key-value store through the intent.

You can put key-values directly into an Intent:

Intent intent = new Intent(getBaseContext(), myService.class);
itent.putExtra(key, value);

You can build a Bundle and add it to the intent also. Bundle's have a few more methods that make it easier to put rich data into them.

Bundle bundle = new Bundle();
bundle.extras.putString(key, value);
intent.putExtras(mBundle);

View more about Bundle's via the docs: http://developer.android.com/reference/android/os/Bundle.html

If you're interesting in passing a lot of data that cannot be easily represented in key value pairs then you may want to organize your data into your own Parceable class which can also be passed through an Intent. View more on making your own Parceable via the docs: http://developer.android.com/reference/android/os/Parcelable.html and via this answer How to send an object from one Android Activity to another using Intents?

Upvotes: 0

Related Questions