该应用程序会自动重启

时间:2015-05-17 22:03:02

标签: android

在我开始将意图(来自服务器的响应)从doInBackground方法传递到MainActivity的内部LocationListent中的onLocationchanged方法之前,一切正常。我正面临appp不断重启的问题,然后冻结,设备重启。

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

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类:

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

        }

    }

}

修改

    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)

1 个答案:

答案 0 :(得分:1)

代码中存在一些问题,但我看到的主要问题是您在调用getExtras()后直接调用sender.post_data(),这会启动AsyncTask。请记住,根据定义,AsyncTask是异步的,因此您必须等到它完成后再使用其结果中的任何数据。

你在这里也有变量错过匹配:

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

此外,您似乎没有覆盖LocationListener的一些必要覆盖。

尝试使用onNewIntent()来捕获AsyncTask完成后进入的新Intent,看看是否有效。请参阅documentation here

另外,将android:launchMode="singleTop"添加到AndroidManifest.xml中以获取MainActivity:

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

编辑:从日志中看,您的后台线程似乎从过于频繁调用的AsyncTask进行备份。

尝试给它多一点时间来执行。 需要注意的一点是,每次从startActivity()拨打onPostExecute()时,您的MainActivity都会暂停并重新恢复。 所以,看起来你只是经常这样做(根据日志每秒多次)。

尝试以稍大的间隔注册您的位置更新,并给它一个小的最小距离,看看是否修复了它:

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

完整代码:

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) {

        }

    }
}