I am starting a Service TrackingService
from MainActivity in the onCreate()
. From the TrackingService
I am passing JSONString
to the PostData
class. Then I am getting a Response from the server with the direction, route
. I want to pass these values to the MainActivity but I am not getting them in the MainActivity. direction and route have values and onPostExecute()
is being invoked and this condition if (route != 0) {}
in the method is being entered but the ´onNewIntent()´ method in the MainActivity is not being invoked. How can I fix it to get the direction and route in the MainActivity
from the PostData
class?
MainActivity class:
public class MainActivity extends ActionBarActivity implements
AsyncTaskCallback {
private static Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = new Intent(this, TrackingService.class);
startService(i);
}
public static Context getAppContext() {
return context;
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Bundle extras = getIntent().getExtras();
if (extras != null && extras.containsKey("dirtRout")) {
String directRoute = extras.getString("dirtRout");
String[] split = directRoute.split(",");
String direc = split[0];
String route1 = split[1];
boolean test = true;
if (test) {
test = false;
System.out.println("test f MainActivity: " + direc + ": "
+ route1);
tvD = (TextView) findViewById(R.id.textDirect);
tvD.setText(direc);
tvR = (TextView) findViewById(R.id.textRoute);
tvR.setText(route1);
}
}
}
}
Trackingservice class:
public class TrackingService extends Service implements AsyncTaskCallback,
LocationListener {
...
@Override
public void onLocationChanged(Location location) {
PostData sender = new PostData(TrackingService.this);
sender.post_data(jSONString, this);
}
}
PostData class:
public class PostData {
String jSONString;
private AsyncTaskCallback callback;
public PostData(AsyncTaskCallback callback) {
this.callback = callback;
}
class MyAsyncTask extends AsyncTask<String, Integer, ArrayList<Integer>> {
int route;
String direction;
.....
@Override
protected ArrayList<Integer> doInBackground(String... params) {
......
Gson gson = new Gson();
JSONStore data = gson.fromJson(sb.toString(), JSONStore.class);
route = data.getRoute();
direction = data.getDirection();
}
protected void onPostExecute(ArrayList<Integer> result) {
if (route != 0) {
String directRoute = direction + "," +Integer.toString(route);
Intent intent = new Intent(MainActivity.getAppContext(), MainActivity.class);
intent.putExtra("dirtRout", directRoute);
}
}
}
You are not starting your activity after creating the intent. Get the current activity from the app and call startactivity method.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments