I am trying to build an Android Application that retrieves the users location for every 'x' seconds and then displays it in a toast.
There are three main java files that I am using in my code. They are: MainActivity.java which extends Activity and uses the alarm service provided by the AlarmReciever.java, AlarmReceiver.java extends the broadcast receiver and LocationModules.java, extends Service and implements location listener, which contain code to retrieve location.
The sequence of flow is as follows:
MainActivity.java ------> AlarmReceiver.java ------> LocationModules.java
LocationModules.java has a location manager that needs the application context which needs to be passed from MainActivity.java (I am not sure if I am right here).
Can anyone please help me with this. Thanks in advance.
myLocationManager=(LocationManager)mContext.getSystemService(LOCATION_SERVICE);
If your LocationModules extends Service, then you can just use getSystemService method (I mean, call it without using mContext).
Related
I wanted to make a network fragment on my Android app so I could upload and download information from my database server. Following the guide on Android networking on the Developer page and the corresponding example project on Github, I created a demo to test a network connection.
I copied the files DowloadCallback.java (contains the implemented network interface) and NetworkFragment.java (the network fragment thread) word for word from the example project and added the necessary permissions in AndroidManifest.xml.
When I tried to implement the fragment into my activity, I got errors in several rather odd and counterintuitive places:
Code
public class MainActivity extends FragmentActivity implements DownloadCallback {
...
#Override
public void updateFromDownload(String result) {
...
}
}
Errors
Class 'MainActivity' must either be declared in abstract or implement method 'updateFromDownload(T)' in 'DownloadCallback'
Method does not override method from its superclass
The public class says it needs a particular method for the class to implement DownloadCallback, but when I add such method it says that it does not exist in its superclass. How can these errors coexist? How can I fix this?
By the way, this is the exact same way the main activity class is defined in the sample project. Also I have posted this as an issue on Github but I am hoping to get a quicker response and attention here.
base on this You have to define <T> for DownloadCallback
In your case T is String
So change your code like below
public class MainActivity extends FragmentActivity implements DownloadCallback<String>
I created a simple little directory listing method in the default MainActivity.class
I was able to get it to function the way I wanted it to, however, when I moved the method to a different class and called it in MainActivity, I wound up getting a lot of Context Errors all over the place. After searching the web I am stumped and require assistance.
The code where it errors out is for the Context for FileArray:
(public class Utilities extends Activity)
arrayAdapter = new FileArrayAdapter(getApplicationContext(),R.layout.custom_explorer,dir);
Method Name:
public void listDirectories(ListView listView, File directory)
It errors out on the getApplicationContext, this method worked just fine in MainActivity.
I have not implemented Fragments to MainActivity yet, wanted to get the working functionality first then spread out for OOP; then call by Fragment.
Hope someone can help, any further info I am happy to share.
Thank you for the timely response: ρяσѕρєя K & Nilabja.
Ya I guess it just needed another person to suggest feeding Context as a param. I did that once but it did not work so I passed it off, attempted once again from Nilabja suggestion. Yip that was the solution.
utilities.ListDirectory(mainList, root, getApplicationContext());
public void listDIR(ListView listView, File directory, Context setContext)
I'm currently creating a game that uses multiple activites.
The different activities need to all be able to communicate with my main ConnectionManager class that sends stuff to my backend server.
The connection is currently only available in one of the activites like this:
connectionManager = new ConnectionManager( this );
connectionManager.start();
So what i want is, in the first activity the app should start the connectionManager and keep it running. Then when the user switches to another activity that activity needs to be able to send data to the connectionManager.
One possible way would be passing data when changing intents:
startActivity(new Intent(gameLobbyManager.this, LobbyManager.class).putExtra( key,value)));
But then i cannot pass the entire class to the new Intent which is needed in my case.
Appreciate any help!
I'm new to android and java.
I'm rearranging some of the classes in my app into separate class files. I had a onLocationListener class in my main activity class file. I moved the class to a separate java class file. Then, however the following code will not compile . . .
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),
"Gps Disabled",
Toast.LENGTH_SHORT ).show();
}
The getApplicationContext won't compile when this code is in a separate file. I tried this. and mainactivityname. but nothing seems to work. So I suppose this problem can be formed into a the following question:
How do you state the application context from code that exists in separate java class files outside the main activity file?
thanks, Gary
getApplicationContext() is a method of class Context, so you can only call it from a class or object that in some way extends Context. You factored your code out of Activity, which is such a class. Your solution, then, is for the Context class that contains your new class or object to pass its context in so that your new class can use it.
Your code inside your main Activity would look something like this:
MyOwnClass ownObject = new MyOwnClass();
// you have to implement setApplicationContext
ownObject.setApplicationContext( this.getApplicationContext() );
It's probably a good idea to get the application context right away, since it'll be stable for the lifetime of your app, unlike the Activity context which could go away on something as simple as an orientation change.
I'm working on a small project where I'm making a compass that is going to be used on top of a MapView. Everything works just fine when running the program (compass part) within its own activity (not on the MapView), but because the compass has to be integrated into another package containing the Activity that holds the MapView, I want my compass code to be accessed from that package.
The problem is that I don't know to initialize the sensors from the other package or if I have to do it from the Activity itself and then somehow pass the instance of the SensorManager to the compass package.
I hope that my question is specific enough, but if not, I'll gladly post some of my code :)
If you have a valid Context you can start the sensor manager from another package.
public class compass implements SensorEventListener {
SensorManager mSensorManager;
public compass(Context context) {
mSensorManager = (SensorManager)context.getSystemService(SENSOR_SERVICE);
// do stuff like register listener
}
}