Track an user with location based App - android

I'm working on a Location based App. Here my requirement is I want to track a user location from my device like uber or Ly ft App.
Example: If a pizza guy(User B) wants to deliver pizza to me(User A) then he can share his user id, so using that id, I can enter in my device and see his exact precise location. But I want to track him until I required so how to achieve this in code. Help me with the architecture if you have come across such scenario.
I can also achieve the above scenario, but my doubt is how can I show in map without refreshing each time when the user moves say for example two minutes once I want to check the Latitude and Longitude and update it in my map for the same user

Create a Thread, that asks a Handler to get the position a few times. You need to use a Handler, because the getMyLocation method can only be called from GUI Thread:
private class MyLocationThread extends Thread{
#Override
public void run() {
int loops = 0;
// we give the location search a minute
while(loops < 60){
// we have to try it over a handler, because getMyLocation() has to be called from GUI Thread -_-
_getMyLocationHandler.sendEmptyMessage(0);
if(isInterrupted()){
return;
}
// take a short nap before next try
try {Thread.sleep(1000);} catch(Exception e){}
loops++;
}
}
}
Here's what the Handler does:
private Handler _getMyLocationHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
if(getMap().isMyLocationEnabled() && getMap().getMyLocation() != null){
_locationWatcher.interrupt();
drawCurrentImagePositions();
}
}
};

Related

Updating a chart from an ArrayList over time?

I have an ArrayList of values, and I would like to iterate through the ArrayList. For every new value, I would like to update the chart with that value, and then wait a set amount of time before doing the same thing to the next value.
At the moment, my log says that all of the values are being iterated over. However, on my testing device, the chart does not update until the very end; at that point, all of the values are loaded at once, so there is no desired "slideshow" effect.
When I want to start playing back the values in my ArrayList, this method is called:
public void playback(){
if(ret != null) {
for (int x = 0; x < ret.size(); x++) {
addEntry(ret.get(x));
try {
Thread.sleep(100);
} catch (Exception e){
//Do nothing
}
}
} else {
Log.d(LOG_TAG, "ret was null.");
}
}
What can I do so that the values are displayed on my chart, one after another, with a certain amount of time between each value?
Edit: Here was the solution I ended up implementing with help from Shadab Ansari:
public void playback(){
if(ret != null) {
addEntry(0);
} else {
Log.d(LOG_TAG, "ret was null.");
}
}
private void addEntry(int index) {
final int in = index;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
yVals1.get(0).setVal(ret.get(in).intValue());
RadarDataSet set1 = new RadarDataSet(yVals1, "Set 1");
// And other UI stuff
// Recursive call!
if(in < ret.size() - 1){
addEntry(in + 1);
}
}
}, 100);
}
In case it was not clear, ret was a global variable that contained the arrays that I was going to be inserting. yVals1 was an ArrayList of Entries to populate the radar chart.
The end result is that, in this example code, the chart is updated with the next value in my ArrayList every 100 milliseconds. During this time I can still zoom in/out of the chart and rotate it with no problems.
If your addEntry() performs a UI operation then let me explain your problem -
Explanation -
Android is an event based system. Something happens on the device (the screen is touched, a key is pressed, etc.) and Android raises an event. An App is notified of an event and when one occurs that it needs to respond to it does so, often running the code that you have written. Your App runs its code in a loop under the control of the Android Operating Systems (OS). This code loop is referred to as the App's thread of execution. There is only one thread and it is responsible for both running the App code and updating the display.
So the UI update does not happen immediately and your making the UI thread sleep for 100 ms every time the loop runs. And when Android tries to update the UI, you make the thread sleep which means during this time period UI thread will not do anything. And this happens till your loop finishes. After your loop ends, the final event gets executed and you will see your UI updated by the call of addEntry() with the latest value passed.
Solution -
You can use postDelayed()-
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//Perform your task and it will be executed after 100 ms
}
},100);

send SMS automatically if the state of a button does not change for a period

currently I'm developing an application which involve an action to perform send SMS automatically to a person if the state of a button does not change for a period. Simply speaking, if let say there a button called send(current state = off), and i left it for 15 minutes without changing the state, the application will send sms to a person for me automatically. Note that, although the application is close or user is in homescreen, the time for the state is still counting and still able to perform the action automatically. Please help me, any references is appreciate.
Take some global variables in the activity:
int time_in_minutes=15;
Thread th;
boolean ifPressed=false;
boolean loop=true;
When you start the Activity, start a background thread:
th=new Thread(new Runnable()
public void run()
{
while(loop)
{
for(int i=0;i<time_in_milliseconds;i++)
{
if(ifPressed)
break;
else
Thread.sleep(time_in_milliseconds);
}
if(i==time_in_milliseconds)
{
sendSMS();
loop=false;
}
else
{
ifPressed=false;
loop=true;
}
}
}
);
th.start();
Finally, make the ifPressed variable true when you press the send button.

Android : Make application wait till the current location is found

I'm trying to make my app wait till the current location is found. I've tried few different ways using Threads and all have failed really. I was using wait() and notify() but application just hung and never found the current location.
I amen't using google map api as it is not part of the application. Does anyone have any ideas how to do this or examples.
EDIT : The Thread I was using did not start till the user pressed a button then within onLocationChanged other data is processed e.g. adding the new location to ArrayList, Calculate the distance between the current and last Location as well as the Time taken to get to the new location
You could try starting an AsyncTask in onCreateto get the location. Your default onCreate layout could be a "loading" page, then when your AsyncTask completes successfully with the location it draws your "real" UI.
So if I understand what you want to do correctly, then I would avoid making another thread in onClick(). Instead, onClick() should just request a location, display a progress dialog, and return. Since the work you want to do happens after you receive the new location, I would start an AsyncTask there. Then you finally remove the dialog box (removing it returns control to the user) when the AsyncTask finishes.
Code usually helps, so, I would put this in onCreate() or wherever:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.refresh();
}
});
And put this in your LocationListener:
public void refresh() {
myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
myDialog = new ProgressDialog(myContext);
myDialog.setIndeterminate(true);
myDialog.show();
}
#Override
public void onLocationChanged(Location location) {
// now do work with your location,
// which your probably want to do in a different thread
new MyAsyncTask().execute(new Location[] { location });
}
And then you need an AsyncTask, which may look like this:
class MyAsyncTask extends AsyncTask<Location, Void, Void> {
#Override
protected Void doInBackground(Location... location) {
// start doing your distance/directions/etc work here
return null;
}
#Override
protected void onPostExecute(Void v) {
// this gets called automatically when you're done,
// so release the dialog box
myDialog.dismiss();
myDialog = null;
}
}

How to do an update loop?

Im doing a little app, its a memory game, you choose one card, it turns up, you choose the second card, it turns up, if they are the same they are out of the game, if they dont match, they are turned down again.
I have
public class PlayActivity extends Activity implements OnClickListener.
The flip events are trigged by click handlers, declared at public void onCreate(Bundle savedInstanceState) they work fine.
When the first card is selected, it calls my method Action, this sets the image from default (the card back) to the 'real' image (the card front). Fine so far.
My problem is the second card: when its selected, it calls method Action, where it should set the front image (lets call it 'middle action'), then a litle pause (a while loop doing nothing until x milliseconds), and then it checks what to do (if they match or not) and turn them down or take the out of the game. You can see where is the problem: the screen only displays the result after x milliseconds (the 'middle action' is not being draw).
Since I have done some little games with XNA, I know the loop Update-Draw, so I know here im updating the same thing twice so always the last one is drawn. But here, the only updating I can have is when click events are trigged, I need a periodic, constant update.
Help?
You can probably use a TimerTask in order to handle that. You can implement it like the following.
This probably isn't the most robust way to do it, but it is an idea. If I figure out a better way to do it in a short time I'll edit my post. :)
Also I would like to add that if you want to make a game that uses an update / draw loop you may need to use a SurfaceView to draw your game. Look at the example here http://developer.android.com/resources/samples/JetBoy/index.html
public class TestGameActivity extends Activity {
/* UIHandler prevents exceptions from
performing UI logic on a non-UI thread */
private static final int MESSAGE_HIDE_CARD = 0;
private class UIHandler extends Handler {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_HIDE_CARD:
ImageView cardView = (ImageView) msg.obj;
cardView.setImageResource(R.drawable.faceDownCard);
break;
}
}
}
private UIHandler handler = new UIHandler();
// Handle my click. V is the card view
public void onClick(View v) {
final int viewID = v.getId();
// Create a hide task
TimerTask hideTask = new TimerTask() {
#Override
public void run() {
// Construct a message so you won't get an exception
Message msg = new Message();
msg.what = MESSAGE_HIDE_CARD;
msg.obj = findViewById(viewID);
handler.sendMessage(msg);
}
};
// Schedule the task for 2 seconds
new Timer().schedule(hideTask, 2000);
}
}

How to replace POI images in the ARView

I'm running an Wikitude application which shows the point if Interest
(POIs). When the application starts, I click a button to launch ARView
(AUgmented Reality) and there I could see the POI images superimposed
on the Live Camera images.
Now I want to change those images at frequent intervals.
I'm using :
// Need handler for callbacks to the UI thread
final Handler mHandler = new Handler();
// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
public void run() {
updateResultsInUi();
}
};
protected void startLongRunningOperation() {
// Fire off a thread to do some work
Thread t = new Thread() {
public void run() {
Pi.computePi(800).toString(); //Do something for a while
mHandler.post(mUpdateResults); //then update image
}
};
t.start();
}
But nothing is working. I'm sure I'm doing some mistake...
Thanking you all in advance.
When you say you are running a "Wikitude application", do you mean you are building an app using their publicly available on-device Android API (http://www.wikitude.org/developers)? If so, then dynamically changing the POI marker images is not supported. The AR view is an activity within the Wikitude app itself, launched via your intent. You have no further POI control (apart from callback intents) after the camera view is launched.
We can add our customized icons for a Point of Interest. Have you gone through the sample code provided by Wikitude?. There you can find a method startARViewWithIcons(). Just go through it once, Let me know if there is anything else.... Thanks & Regards, Raghavendra K

Categories

Resources