Mr Asker
Mr Asker

Reputation: 2380

The app keeps restarting itself automatically

Everything works fine before I started to pass the Intent (response from the server) from the doInBackground method to the onLocationchanged method in the inner LocationListent of the MainActivity. I am facing the problem the appp keeps restarting, then freezing and the device restart itself.

MainAcitiviy:

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
       //I added "this" here//.
        LocationListener ll = new myLocationListener(this);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, ll);

    }
      //Inner class in MainActivity
    class myLocationListener implements LocationListener {
        // I added here the bContext and the constructor//
        final Context bContext;

        public myLocationListener(Context context){
            bContext = context;
        }

        @Override
        public void onLocationChanged(Location location) {

                PostData sender = new PostData();
               // I added here the context  parameter.//
                sender.post_data(jSONString, bContext);
                  //I added here this part to receive the intent from onPostExecute //
                Bundle extra = getIntent().getExtras();
                if (extras != null) {
                    ArrayList<Integer> c = extras
                            .getIntegerArrayList("stop_route");
                    for (int item : c) {
                        System.out.println("The Intent is not empty: "
                                + item);
                    }
                }  
protected void onResume() {

    super.onResume();
}

@Override
protected void onPause() {
    super.onPause();
    }             
}

Some of the output in OnLocationChanged:

05-17 21:24:04.969: I/System.out(19497): The Intent is not empty: 7
05-17 21:24:04.969: I/System.out(19497): The Intent is not empty: 31

PostData class:

public class PostData {
    String jSONString;

    // Context mContext;

    public PostData() {
        super();

    }

    public String getjSONString() {
        return jSONString;

    }

    public void setjSONString(String jSONString) {
        this.jSONString = jSONString;
    }

    public void post_data(String jSONString, Context context) {
        this.jSONString = jSONString;

        new MyAsyncTask(context).execute(jSONString);

    }

    class MyAsyncTask extends AsyncTask<String, Integer, Void> {
        final Context mContext;
        ArrayList<Integer> routes = new ArrayList<Integer>();


        public MyAsyncTask(Context context) {
            mContext = context;
        }


        @Override
        protected Void doInBackground(String... params) {
            // TODO Auto-generated method stub
            BufferedReader reader = null;


            try {
                System.out.println("The output of : doInBackground "
                        + params[0]);

                URL myUrl = new URL(
                        "https://blabla.rhcloud.com/test");
                HttpURLConnection conn = (HttpURLConnection) myUrl
                        .openConnection();
                conn.setRequestMethod("POST");
                conn.setDoOutput(true);
                conn.setConnectTimeout(10000);
                conn.setReadTimeout(10000);
                conn.setRequestProperty("Content-Type", "application/json");
                conn.connect();

                // System.out.println("The output of getResponsecode: "
                // + conn.getResponseCode());
                // create data output stream
                DataOutputStream wr = new DataOutputStream(
                        conn.getOutputStream());
                // write to the output stream from the string
                wr.writeBytes(params[0]);

                wr.close();

                StringBuilder sb = new StringBuilder();
                reader = new BufferedReader(new InputStreamReader(
                        conn.getInputStream()));
                String line;

                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");

                }

                Gson gson = new Gson();
                StopsJSON data = gson.fromJson(sb.toString(), StopsJSON.class);

             routes = data.getRoutes();


                System.out.println("The output of the StringBulder before "
                        + routes);
                System.out.println("The output of the StringBulder: "
                        + sb.toString());

            } catch (IOException e) {

                e.printStackTrace();
                return null;
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                        return null;
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

            return null;

        }

        @Override
        protected void onPostExecute(Void result) {
            // Intent with Conetxt of the Asyntask class and
            Intent intent = new Intent(mContext, MainActivity.class);
            intent.putExtra("stop_route", routes);
            mContext.startActivity(intent);

        }

    }

}

Edit

    05-18 01:00:05.245: I/System.out(4962): The output of : doInBackground {"mac":"10:A5:D0:06:C6:E9","latitude":53.8689588,"longitude":10.66550319,"route":0}
    05-18 01:00:05.255: I/System.out(4962): (HTTPLog)-Static: isSBSettingEnabled false
    05-18 01:00:05.325: I/System.out(4962): KnoxVpnUidStorageknoxVpnSupported API value returned is false
    05-18 01:00:05.986: I/System.out(4962): The output of the StringBulder before [7, 31]
    05-18 01:00:05.986: I/System.out(4962): The output of the StringBulder: {"status":201,"routes":[7,31]}
    05-18 01:00:05.996: I/Timeline(4962): Timeline: Activity_launch_request id:com.bustracker time:14086740
    05-18 01:00:06.016: I/System.out(4962): The output of : doInBackground {"mac":"10:A5:D0:06:C6:E9","latitude":53.8689588,"longitude":10.66550319,"route":4}
    05-18 01:00:06.016: I/System.out(4962): (HTTPLog)-Static: isSBSettingEnabled false
    05-18 01:00:06.086: D/Activity(4962): performCreate Call secproduct feature valuefalse
    05-18 01:00:06.086: D/Activity(4962): performCreate Call debug elastic valuetrue
    05-18 01:00:06.256: I/Timeline(4962): Timeline: Activity_idle id: android.os.BinderProxy@c867c1 time:14087003
    05-18 01:00:06.346: I/System.out(4962): The output of the StringBulder before [7, 31]
    05-18 01:00:06.346: I/System.out(4962): The output of the StringBulder: {"status":201,"routes":[7,31]}
    05-18 01:00:06.346: I/Timeline(4962): Timeline: Activity_launch_request id:com.bustracker time:14087094
    05-18 01:00:06.346: I/System.out(4962): The output of : doInBackground {"mac":"10:A5:D0:06:C6:E9","latitude":53.8689588,"longitude":10.66550319,"route":4}
    05-18 01:00:06.346: I/System.out(4962): (HTTPLog)-Static: isSBSettingEnabled false
    05-18 01:00:06.407: D/Activity(4962): performCreate Call secproduct feature valuefalse
    05-18 01:00:06.407: D/Activity(4962): performCreate Call debug elastic valuetrue
    05-18 01:00:06.517: I/Timeline(4962): Timeline: Activity_idle id: android.os.BinderProxy@16efd0b5 time:14087265
    05-18 01:00:06.587: I/System.out(4962): The output of the StringBulder before [7, 31]
    05-18 01:00:06.587: I/System.out(4962): The output of the StringBulder: {"status":201,"routes":[7,31]}
    05-18 01:00:06.587: I/Timeline(4962): Timeline: Activity_launch_request id:com.bustracker time:14087338
    05-18 01:00:06.587: I/System.out(4962): The output of : doInBackground {"mac":"10:A5:D0:06:C6:E9","latitude":53.8689588,"longitude":10.66550319,"route":0}
    05-18 01:00:06.587: I/System.out(4962): (HTTPLog)-Static: isSBSettingEnabled false
    05-18 01:00:06.647: D/Activity(4962): performCreate Call secproduct feature valuefalse
    05-18 01:00:06.647: D/Activity(4962): performCreate Call debug elastic valuetrue
    05-18 01:00:06.757: I/System.out(4962): The output of the StringBulder before [7, 31]
    05-18 01:00:06.757: I/System.out(4962): The output of the StringBulder: {"status":201,"routes":[7,31]}
    05-18 01:00:06.757: I/System.out(4962): The output of : doInBackground {"mac":"10:A5:D0:06:C6:E9","latitude":53.8689588,"longitude":10.66550319,"route":0}
    05-18 01:00:06.757: I/System.out(4962): (HTTPLog)-Static: isSBSettingEnabled false
    05-18 01:00:06.767: I/Timeline(4962): Timeline: Activity_launch_request id:com.bustracker time:14087515
    05-18 01:00:06.887: D/Activity(4962): performCreate Call secproduct feature valuefalse
    05-18 01:00:06.887: D/Activity(4962): performCreate Call debug elastic valuetrue
    05-18 01:00:06.967: I/Timeline(4962): Timeline: Activity_idle id: android.os.BinderProxy@109f7fdd time:14087719
    05-18 01:00:06.977: I/Timeline(4962): Timeline: Activity_idle id: android.os.BinderProxy@c4a769 time:14087720
    05-18 01:00:06.997: I/System.out(4962): The output of the StringBulder before [7, 31]
    05-18 01:00:06.997: I/System.out(4962): The output of the StringBulder: {"status":201,"routes":[7,31]}
    05-18 01:00:06.997: I/System.out(4962): The output of : doInBackground {"mac":"10:A5:D0:06:C6:E9","latitude":53.8689588,"longitude":10.66550319,"route":4}
    05-18 01:00:06.997: I/System.out(4962): (HTTPLog)-Static: isSBSettingEnabled false
    05-18 01:00:07.017: I/Timeline(4962): Timeline: Activity_launch_request id:com.bustracker time:14087762
    05-18 01:00:07.077: D/Activity(4962): performCreate Call secproduct feature valuefalse
    05-18 01:00:07.077: D/Activity(4962): performCreate Call debug elastic valuetrue
    05-18 01:00:07.147: I/System.out(4962): The output of the StringBulder before [7, 31]
    05-18 01:00:07.147: I/System.out(4962): The output of the StringBulder: {"status":201,"routes":[7,31]}
    05-18 01:00:07.197: I/Timeline(4962): Timeline: Activity_launch_request id:com.bustracker time:14087944
    05-18 01:00:07.297: D/Activity(4962): performCreate Call secproduct feature valuefalse
    05-18 01:00:07.297: D/Activity(4962): performCreate Call debug elastic valuetrue
    05-18 01:00:07.468: I/Timeline(4962): Timeline: Activity_idle id: android.os.BinderProxy@255edd7c time:14088211
    05-18 01:00:07.468: I/Timeline(4962): Timeline: Activity_idle id: android.os.BinderProxy@2134ae11 time:14088211
    05-18 01:00:07.828: V/ActivityThread(4962): updateVisibility : ActivityRecord{1ac84605 token=android.os.BinderProxy@37a1708c {com.bustracker/com.bustracker.MainActivity}} show : false
    05-18 01:00:07.828: V/ActivityThread(4962): updateVisibility : ActivityRecord{1979865a token=android.os.BinderProxy@c867c1 {com.bustracker/com.bustracker.MainActivity}} show : false
    05-18 01:00:07.828: V/ActivityThread(4962): updateVisibility : ActivityRecord{2349d08b token=android.os.BinderProxy@16efd0b5 {com.bustracker/com.bustracker.MainActivity}} show : false
    05-18 01:00:07.828: V/ActivityThread(4962): updateVisibility : ActivityRecord{1a7a5568 token=android.os.BinderProxy@c4a769 {com.bustracker/com.bustracker.MainActivity}} show : false
    05-18 01:00:07.828: V/ActivityThread(4962): updateVisibility : ActivityRecord{28c3d81 token=android.os.BinderProxy@109f7fdd {com.bustracker/com.bustracker.MainActivity}} show : false
    05-18 01:00:07.828: V/ActivityThread(4962): updateVisibility : ActivityRecord{2339ae26 token=android.os.BinderProxy@2134ae11 {com.bustracker/com.bustracker.MainActivity}} show : false

05-18 01:04:37.191: E/ActivityThread(4962): Performing stop of activity that is not resumed: {com.bustracker/com.bustracker.MainActivity}
05-18 01:04:37.191: E/ActivityThread(4962): java.lang.RuntimeException: Performing stop of activity that is not resumed: {com.bustracker/com.bustracker.MainActivity}
05-18 01:04:37.191: E/ActivityThread(4962):     at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3822)
05-18 01:04:37.191: E/ActivityThread(4962):     at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3910)
05-18 01:04:37.191: E/ActivityThread(4962):     at android.app.ActivityThread.access$1200(ActivityThread.java:177)
05-18 01:04:37.191: E/ActivityThread(4962):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
05-18 01:04:37.191: E/ActivityThread(4962):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-18 01:04:37.191: E/ActivityThread(4962):     at android.os.Looper.loop(Looper.java:145)
05-18 01:04:37.191: E/ActivityThread(4962):     at android.app.ActivityThread.main(ActivityThread.java:5944)
05-18 01:04:37.191: E/ActivityThread(4962):     at java.lang.reflect.Method.invoke(Native Method)
05-18 01:04:37.191: E/ActivityThread(4962):     at java.lang.reflect.Method.invoke(Method.java:372)
05-18 01:04:37.191: E/ActivityThread(4962):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
05-18 01:04:37.191: E/ActivityThread(4962):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)

Upvotes: 2

Views: 2729

Answers (1)

Daniel Nugent
Daniel Nugent

Reputation: 43322

There are a few problems in the code, but the main issue that I see is that you're calling getExtras() directly after calling sender.post_data(), which starts the AsyncTask. Remember, an AsyncTask by definition is asynchronous, so you have to wait until it's done before using any data from it's results.

You also had variable miss-match here:

Bundle extra = getIntent().getExtras();
         if (extras != null) {

Also, it looks like you hadn't overridden some necessary overrides for the LocationListener.

Try using onNewIntent(), to capture the new Intent that comes in after the AsyncTask is complete, and see if that works. See documentation here.

Also, add android:launchMode="singleTop" to your AndroidManifest.xml for MainActivity:

<activity
    android:name=".MainActivity"
    android:launchMode="singleTop"

Edit: From the logs, it looks like your background threads are getting backed up from the AsyncTask being called too frequently.

Try giving it a little bit more time to execute. One thing to note is that every time you call startActivity() from onPostExecute(), your MainActivity will be paused and resumed again. So, it looks like you're just doing this way too often (multiple times per second according to the logs).

Try registering your Location Updates with a slightly larger interval, and give it a small minimum distance as well, and see if that fixes it:

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0.1F, ll);

Full code:

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        //I added "this" here//.
        LocationListener ll = new myLocationListener(this);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0.1F, ll);

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        // getIntent() should always return the most recent
        setIntent(intent);

        //I added here this part to receive the intent from onPostExecute //
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            ArrayList<Integer> c = extras
                    .getIntegerArrayList("stop_route");
            for (int item : c) {
                System.out.println("The Intent is not empty: "
                        + item);
            }
        }
    }

    //Inner class in MainActivity
    class myLocationListener implements LocationListener {
        // I added here the bContext and the constructor//
        final Context bContext;

        public myLocationListener(Context context) {
            bContext = context;
        }

        @Override
        public void onLocationChanged(Location location) {

            PostData sender = new PostData();
            // I added here the context  parameter.//
            sender.post_data(jSONString, bContext);
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }

    }
}

Upvotes: 1

Related Questions