Hey guys. I'm making this android app called GPS Buddy. What I want it to do is to send my gps coordinates to a phone number via a text at a set interval. The problem that I am running into is fairly simple. I have all the barebones stuff done, all that I have left is implementing the the user adjustable variables.

Here is the problem:

I have a switch that will turn the background service on and off. If the switch is on, i have the boolean toggleBtn set to true, and I have it set to false if the button is off. That boolean is created is the MainActivity class, but I need to access it in my service class.

here is my code:

package com.bobrown101.gpsbuddy;

import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.telephony.SmsManager;

public class MainActivity extends Activity {

    public String Lat;
    public String Long;
    public boolean toggleBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         Button buttonStart = (Button)findViewById(R.id.button1);        
         buttonStart.setOnClickListener(startListener); // Register the onClick listener with the implementation above

         Calendar cal = Calendar.getInstance();

         Intent intent = new Intent(this, MyService.class);
         PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);

         AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
         // Start every 30 seconds
         alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 45*1000, pintent);  //7200 is 2 hours

         final Switch togglebutton = (Switch) findViewById(R.id.toggle_button);
         togglebutton.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
                 if (togglebutton.isChecked()) {
                     toggleBtn = true;
                     Toast.makeText(getBaseContext(), "Toggled switch to on!", Toast.LENGTH_SHORT).show();
                 } else {
                     toggleBtn = false;
                     Toast.makeText(getBaseContext(), "Toggled switch to off.", Toast.LENGTH_SHORT).show();

                 }
             }
         });


    }

    private OnClickListener startListener = new OnClickListener() {
        public void onClick(View v) {
            doEverything();
        }
    };

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


    public void sendSMS(String x) {
        String phoneNumber = "MYPHONENUMBER";
        String message = x;

        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNumber, null, message, null, null);
    }
    public void getLocation(){
        GPSTracker mGPS = new GPSTracker(this);

        if(mGPS.canGetLocation ){

        double mLat=mGPS.getLatitude();
        double mLong=mGPS.getLongitude();
        Lat = Double.toString(mLat);
        Long = Double.toString(mLong);      

        }else{
        // can't get the location
        }
    }
    public void doEverything(){
        getLocation();
        String text = Lat + "," + Long;
        sendSMS(text);
    }


}

here is the next class:

package com.bobrown101.gpsbuddy;

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Switch;
import android.widget.Toast;

public class MyService extends Service {
    public String Lat;
    public String Long;

    public void sendSMS(String x) {
        String phoneNumber = "5184950113";
        String message = x;

        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNumber, null, message, null, null);
    }
    public void getLocation(){
        GPSTracker mGPS = new GPSTracker(this);

        if(mGPS.canGetLocation ){

        double mLat=mGPS.getLatitude();
        double mLong=mGPS.getLongitude();
        Lat = Double.toString(mLat);
        Long = Double.toString(mLong);      

        }else{
        // can't get the location
        }
    }
    public void doEverything(){
        getLocation();
        String text = Lat + "," + Long;
        sendSMS(text);
    }


      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
        //TODO do something useful




          MainActivity main = new MainActivity();
          if(main.toggleBtn){
              doEverything();
          }else{
              Toast.makeText(getBaseContext(), "The toggle is off: text not sent", Toast.LENGTH_SHORT).show();
              Toast.makeText(getBaseContext(), String.valueOf(main.toggleBtn), Toast.LENGTH_SHORT).show();
          }
        return Service.START_STICKY; //changed from START_NOT_STICKY
      }

      @Override
      public IBinder onBind(Intent intent) {
      //TODO for communication return IBinder implementation
        return null;
      }
    } 

like I said the problem is the switch

everything else works and/or I have not finished it yet

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.