Passing data from the class to the MainActivity

MrPencil

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);
      }

  }
}
Farooq Arshed

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing value from MainActivity to custom listview adapter class

From Dev

passing a parameter from MainActivity.java into my SQLiteOpenHelper class

From Dev

Passing a variable from MainActivity to a Fragment

From Dev

Passing a List From the MainActivity to a Fragment

From Dev

Swift Passing data from appDelegate to another class

From Dev

Passing Data From A Class To WatchOS 2 (Connectivity)

From Dev

passing data from BroadcastReceiver to class that extends runnable

From Dev

Xamarin Android passing data from class to activity

From Dev

passing string data from Android MainActivity to PCL TabbedPage xamarin.form

From Dev

passing string data from Android MainActivity to PCL TabbedPage xamarin.form

From Dev

passing data from UITabBarView Class to sub class of UIViewController using swift

From Dev

Passing data from a class that 'extends activity' to a class that 'extends fragment'

From Dev

Sending data from Fragment to MainActivity

From Dev

Class Structure and Passing Data

From Dev

Struts 2 passing data from JSP to action class

From Dev

Passing data for list view from Asynctask to a custom list adapter class

From Dev

passing data from controller to model class mvc-5

From Dev

passing a variable from class to class

From Dev

Calling a method of the MainActivity from another class?

From Dev

Access to WebView from another function in MainActivity class

From Dev

execute asynctask from another class in mainactivity

From Dev

Calling a method of the MainActivity from another class?

From Dev

Run code from MainActivity in another Class

From Dev

calling method in mainactivity from another class in android

From Dev

Make UI changes in MainActivity from helper class

From Dev

Passing String Array from AsyncTask OnpostExecute to Mainactivity Not Working

From Dev

Passing data from IntentService

From Dev

Passing data from IntentService

From Dev

Passing data from column

Related Related

  1. 1

    Passing value from MainActivity to custom listview adapter class

  2. 2

    passing a parameter from MainActivity.java into my SQLiteOpenHelper class

  3. 3

    Passing a variable from MainActivity to a Fragment

  4. 4

    Passing a List From the MainActivity to a Fragment

  5. 5

    Swift Passing data from appDelegate to another class

  6. 6

    Passing Data From A Class To WatchOS 2 (Connectivity)

  7. 7

    passing data from BroadcastReceiver to class that extends runnable

  8. 8

    Xamarin Android passing data from class to activity

  9. 9

    passing string data from Android MainActivity to PCL TabbedPage xamarin.form

  10. 10

    passing string data from Android MainActivity to PCL TabbedPage xamarin.form

  11. 11

    passing data from UITabBarView Class to sub class of UIViewController using swift

  12. 12

    Passing data from a class that 'extends activity' to a class that 'extends fragment'

  13. 13

    Sending data from Fragment to MainActivity

  14. 14

    Class Structure and Passing Data

  15. 15

    Struts 2 passing data from JSP to action class

  16. 16

    Passing data for list view from Asynctask to a custom list adapter class

  17. 17

    passing data from controller to model class mvc-5

  18. 18

    passing a variable from class to class

  19. 19

    Calling a method of the MainActivity from another class?

  20. 20

    Access to WebView from another function in MainActivity class

  21. 21

    execute asynctask from another class in mainactivity

  22. 22

    Calling a method of the MainActivity from another class?

  23. 23

    Run code from MainActivity in another Class

  24. 24

    calling method in mainactivity from another class in android

  25. 25

    Make UI changes in MainActivity from helper class

  26. 26

    Passing String Array from AsyncTask OnpostExecute to Mainactivity Not Working

  27. 27

    Passing data from IntentService

  28. 28

    Passing data from IntentService

  29. 29

    Passing data from column

HotTag

Archive