Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7578087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:20:53+00:00 2026-05-30T17:20:53+00:00

I am somewhat new to Java and Android and I’m having trouble with classes

  • 0

I am somewhat new to Java and Android and I’m having trouble with classes and activities. I was cleaning up my code and moved a lot of it from my MainActivity to different classes, but I was only able to get the app to work by creating new activities instead of classes.

  • I need to stay in the main view and just use the class’s methods
  • From the main activity a button, counts down and then calls the LocationActivity.
  • LocationActivity finds the GPS coordinates and then sends them to SendActivity.

This is the only way I could get it to work because I just needed to start the locationListener, so I just started it in the onCreate section.

MainActivity.java

    public class MainActivity extends Activity {

    Button mCloseButton;
    Button mOpenButton;
    MultiDirectionSlidingDrawer mDrawer;

    private Button send_button;
    Button sendButton;
    EditText msgTextField;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature( Window.FEATURE_NO_TITLE );
        setContentView(R.layout.main);

        send_button = (Button)findViewById(R.id.button2); 

        mDrawer.open();

        mCloseButton.setOnClickListener( new OnClickListener() {
            public void onClick( View v )
            {
                mDrawer.animateClose();
            }
        });

        mOpenButton.setOnClickListener( new OnClickListener() {

            public void onClick( View v )
            {
                if( !mDrawer.isOpened() )
                    mDrawer.animateOpen();
            }
        });

        final SharedPreferences shared = getSharedPreferences("PEOPLE_PREFERENCES", MODE_PRIVATE);
        final String first = shared.getString("FIRSTNAME", "");
        final String last = shared.getString("LASTNAME", "0");


       ///////Profile Button//////////////// 
        Button profile = (Button) findViewById(R.id.button1);
        profile.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, PreferencesActivity.class));
            }
        });
        ///////////////////////////////////

       //////Generate ID//////////////////
        if (usr_id == null) {

            char[] chars = "abcdefghijklmnopqrstuvwxyzABSDEFGHIJKLMNOPQRSTUVWXYZ1234567890".toCharArray();
            Random r = new Random(System.currentTimeMillis());
            char[] id = new char[8];
            for (int i = 0;  i < 8;  i++) {
                id[i] = chars[r.nextInt(chars.length)];
            }
            usr_id = new String(id);
            Editor editor = shared.edit();
            editor.putString("USR_ID", usr_id);
            editor.commit();
        }
        //////////////////////////////////                 

        ////////Send Alert////////////////
        ///////Begin Timer///////////////
        send_button.setOnClickListener(new OnClickListener() {

            private boolean running = false;
            private CountDownTimer timer;
            public void onClick(View v) {
              if(!running)
              {
                running = true;
                timer = new CountDownTimer(4000, 1000) {

                    @Override
                    public void onFinish() {
                        send_button.setText("SENT");  
                        startActivity(new Intent(MainActivity.this, LocationActivity.class));
                        SendUserActivity.sendId(usr_id1, first, last);                                          
                    }

                    @Override
                    public void onTick(long sec) {
                        send_button.setText("CANCEL (" + sec / 1000 + ")");

                    }
                }.start();
              }
              else
              {
                 timer.cancel();
                 send_button.setText("Send!");
                 running = false;
              }
            }
        });
    }  
    ///////////////////////////////////


    @Override
   public void onContentChanged()
   {
    super.onContentChanged();
    mCloseButton = (Button) findViewById( R.id.button_open );
    mOpenButton = (Button) findViewById( R.id.button_open );
    mDrawer = (MultiDirectionSlidingDrawer) findViewById( R.id.drawer );
   }
}

LocationActivity.java

    package com.alex.www;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

public class LocationActivity extends Activity {


    private LocationManager locManager;
    private LocationListener locListener;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startLocation();
    }

    void startLocation()
    {   

        SharedPreferences shared = getSharedPreferences("PEOPLE_PREFERENCES", MODE_PRIVATE);
    final String usr_id2 = shared.getString("USR_ID", "none");

    //get a reference to the LocationManager
    locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);


    //checked to receive updates from the position
    locListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            SendActivity.send(location, usr_id2);
        }
        public void onProviderDisabled(String provider){
            //labelState.setText("Provider OFF");
        }
        public void onProviderEnabled(String provider){
            //labelState.setText("Provider ON ");
        }
        public void onStatusChanged(String provider, int status, Bundle extras){
            Log.i("", "Provider Status: " + status);
            }
        };

        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
    }
}

SendActivity.java

    package com.alex.www;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.SharedPreferences;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SendActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public static void send(Location loc, String usr_id2)
    {

         Log.i("", String.valueOf(loc.getLatitude() + " - " + String.valueOf(loc.getLongitude())));

         String lat = String.valueOf(loc.getLatitude()); 
         String lon = String.valueOf(loc.getLongitude());

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://example.com/test/example.php");

         try {
           List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
           nameValuePairs.add(new BasicNameValuePair("lat", lat)); 
           nameValuePairs.add(new BasicNameValuePair("lon", lon));
           nameValuePairs.add(new BasicNameValuePair("id", usr_id2));
           httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
           httpclient.execute(httppost);
         } 
         catch (ClientProtocolException e) {
             // TODO Auto-generated catch block
         } 
         catch (IOException e) {
             // TODO Auto-generated catch block
         }
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T17:20:54+00:00Added an answer on May 30, 2026 at 5:20 pm

    I edited your post for more clarity, since the question was interesting. This is a dilemma that many new developers have when they start with android. And trust me I have seen really bad code. And getting the right advice at the right time is important. So here goes.

    You are on the right track. Every view must have its own activity. Dont get out of that habit. If you go the java way, then you are tempted to not use activity’s, but that will make you a very bad android developer.

    Yes, you do need to read a bit more on how to write a android app.

    If you use 1 view 1 activity principle, android OS will support you a lot. For example, back button traversing back in sequence will be supported.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm somewhat new to Java and the Android framework, however what I'm doing I
I am new to Android, but i know somewhat in Java, and Java EE
I am somewhat new to R, and i have this piece of code which
I am somewhat new to JAVA. I've been working with it in college, but
I'm still somewhat new to Java and trying to insert data into a database.
I am somewhat new to the Java Generics feature. Can you please help me
The following simple code in Java behaves somewhat in a strange way that I
Im somewhat new in java, been programming for about a year now and im
I am somewhat new to Java so perhaps I misunderstand the use cases for
I am somewhat new to android development, this is my first time trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.