本文介绍了该应用程序不断自动重启本身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一切正常之前,我开始从doInBackground方法的意图(来自服务器的响应)传递给onLocationchanged方法在MainActivity内LocationListent。我对着APPP不断重新启动的问题,然后冻结和重新启动本身的设备。

MainAcitiviy:

 公共类MainActivity扩展ActionBarActivity {    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        LM的LocationManager =(的LocationManager)getSystemService(Context.LOCATION_SERVICE);
       //我说这个//此处。
        LocationListener的LL =新myLocationListener(本);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,0,LL);    }
      在MainActivity //内部类
    类myLocationListener实现LocationListener的{
        //我说这里的bContext和构造函数//
        最后上下文bContext;        公共myLocationListener(上下文的背景下){
            bContext =背景;
        }        @覆盖
        公共无效onLocationChanged(地点){                发件人的PostData =新的PostData();
               //我在这里补充的背景下parameter.//
                sender.post_data(jSONString,bContext);
                  //我在这里补充这部分接收来自onPostExecute的意图//
                捆绑额外= getIntent()getExtras()。
                如果(临时演员!= NULL){
                    ArrayList的<整数GT; C =演员
                            .getIntegerArrayList(stop_route);
                    对于(INT项目:C){
                        的System.out.println(这样做的目的不为空
                                +项目);
                    }
                }
保护无效onResume(){    super.onResume();
}@覆盖
保护无效的onPause(){
    super.onPause();
    }
}

一些OnLocationChanged输出的:

  05-17 21:24:04.969:我/的System.out(19497):这样做的目的不为空:7
05-17 21:24:04.969:我/的System.out(19497):这样做的目的不为空:31

类的PostData:

 公共类{的PostData
    串jSONString;    //上下文mContext;    公众的PostData(){
        超();    }    公共字符串getjSONString(){
        返回jSONString;    }    公共无效setjSONString(字符串jSONString){
        this.jSONString = jSONString;
    }    公共无效post_data(字符串jSONString,上下文的背景下){
        this.jSONString = jSONString;        新MyAsyncTask(上下文).execute(jSONString);    }    类MyAsyncTask扩展的AsyncTask<字符串,整数,太虚> {
        最后上下文mContext;
        ArrayList的<整数GT;路线=新的ArrayList<整数GT;();
        公共MyAsyncTask(上下文的背景下){
            mContext =背景;
        }
        @覆盖
        保护无效doInBackground(字符串... PARAMS){
            // TODO自动生成方法存根
            读者的BufferedReader = NULL;
            尝试{
                的System.out.println(的输出:doInBackground
                        +参数[0]);                网址myUrl =新的URL(
                        https://blabla.rhcloud.com/test);
                HttpURLConnection的康恩=(HttpURLConnection类)myUrl
                        .openConnection();
                conn.setRequestMethod(POST);
                conn.setDoOutput(真);
                conn.setConnectTimeout(10000);
                conn.setReadTimeout(10000);
                conn.setRequestProperty(内容类型,应用/ JSON);
                conn.connect();                //的System.out.println(GETRESPONSE code的输出:
                // + conn.getResponse code());
                //创建数据输出流
                DataOutputStream类WR =新的DataOutputStream类(
                        conn.getOutputStream());
                //写入字符串中的输出流
                wr.writeBytes(PARAMS [0]);                wr.close();                StringBuilder的SB =新的StringBuilder();
                读者=新的BufferedReader(新的InputStreamReader(
                        conn.getInputStream()));
                串线;                而((行= reader.readLine())!= NULL){
                    sb.append(行+\\ n);                }                GSON GSON =新GSON();
                StopsJSON数据= gson.fromJson(sb.toString(),StopsJSON.class);             路由= data.getRoutes();
                的System.out.println(下称StringBulder之前的输出
                        +路由);
                的System.out.println(下称StringBulder的输出:
                        + sb.toString());            }赶上(IOException异常五){                e.printStackTrace();
                返回null;
            } {最后
                如果(读者!= NULL){
                    尝试{
                        reader.close();
                        返回null;
                    }赶上(例外五){
                        // TODO自动生成catch块
                        e.printStackTrace();
                    }
                }
            }            返回null;        }        @覆盖
        保护无效onPostExecute(虚空结果){
            //意图与Asyntask类Conetxt和
            意向意图=新意图(mContext,MainActivity.class);
            intent.putExtra(stop_route路线);
            mContext.startActivity(意向);        }    }}

修改

  1月5日至18日:00:05.245:我/的System.out(4962):doInBackground {陆委会:在输出10:A5:D0:06:C6 :E9,纬度:53.8689588,东经:10.66550319,路线:0}
    一月5日至18日:00:05.255:我/的System.out(4962):(HTTPLOG)静电:isSBSettingEnabled假
    一月5日至18日:00:05.325:我/的System.out(4962):KnoxVpnUidStorageknoxVpnSupported API返回值是假的
    一月5日至18日:00:05.986:我/的System.out(4962):在StringBulder之前的输出[7,31]
    一月5日至18日:00:05.986:我/的System.out(4962):在StringBulder的输出:{身份:201,路线:[7,31]}
    一月5日至18日:00:05.996:I /时间轴(4962):时间线:Activity_launch_request ID:com.bustracker时间:14086740
    一月5日至18日:00:06.016:我/的System.out(4962):doInBackground {陆委会:在输出10:A5:D0:06:6:E9,纬度:53.8689588,东经:10.66550319,路线:4}
    一月5日至18日:00:06.016:我/的System.out(4962):(HTTPLOG)静电:isSBSettingEnabled假
    一月5日至18日:00:06.086:D /活动(4962):performCreate呼叫secproduct功能valuefalse
    一月5日至18日:00:06.086:D /活动(4962):performCreate调用调试弹性valuetrue
    一月5日至18日:00:06.256:I /时间轴(4962):时间线:Activity_idle ID:android.os.BinderProxy@c867c1时间:14087003
    一月5日至18日:00:06.346:我/的System.out(4962):在StringBulder之前的输出[7,31]
    一月5日至18日:00:06.346:我/的System.out(4962):在StringBulder的输出:{身份:201,路线:[7,31]}
    一月5日至18日:00:06.346:I /时间轴(4962):时间线:Activity_launch_request ID:com.bustracker时间:14087094
    一月5日至18日:00:06.346:我/的System.out(4962):doInBackground {陆委会:在输出10:A5:D0:06:6:E9,纬度:53.8689588,东经:10.66550319,路线:4}
    一月5日至18日:00:06.346:我/的System.out(4962):(HTTPLOG)静电:isSBSettingEnabled假
    一月5日至18日:00:06.407:D /活动(4962):performCreate呼叫secproduct功能valuefalse
    一月5日至18日:00:06.407:D /活动(4962):performCreate调用调试弹性valuetrue
    一月5日至18日:00:06.517:I /时间轴(4962):时间线:Activity_idle ID:android.os.BinderProxy@16efd0b5时间:14087265
    一月5日至18日:00:06.587:我/的System.out(4962):在StringBulder之前的输出[7,31]
    一月5日至18日:00:06.587:我/的System.out(4962):在StringBulder的输出:{身份:201,路线:[7,31]}
    一月5日至18日:00:06.587:I /时间轴(4962):时间线:Activity_launch_request ID:com.bustracker时间:14087338
    一月5日至18日:00:06.587:我/的System.out(4962):doInBackground {陆委会:在输出10:A5:D0:06:6:E9,纬度:53.8689588,东经:10.66550319,路线:0}
    一月5日至18日:00:06.587:我/的System.out(4962):(HTTPLOG)静电:isSBSettingEnabled假
    一月5日至18日:00:06.647:D /活动(4962):performCreate呼叫secproduct功能valuefalse
    一月5日至18日:00:06.647:D /活动(4962):performCreate调用调试弹性valuetrue
    一月5日至18日:00:06.757:我/的System.out(4962):在StringBulder之前的输出[7,31]
    一月5日至18日:00:06.757:我/的System.out(4962):在StringBulder的输出:{身份:201,路线:[7,31]}
    一月5日至18日:00:06.757:我/的System.out(4962):doInBackground {陆委会:在输出10:A5:D0:06:6:E9,纬度:53.8689588,东经:10.66550319,路线:0}
    一月5日至18日:00:06.757:我/的System.out(4962):(HTTPLOG)静电:isSBSettingEnabled假
    一月5日至18日:00:06.767:I /时间轴(4962):时间线:Activity_launch_request ID:com.bustracker时间:14087515
    一月5日至18日:00:06.887:D /活动(4962):performCreate呼叫secproduct功能valuefalse
    一月5日至18日:00:06.887:D /活动(4962):performCreate调用调试弹性valuetrue
    一月5日至18日:00:06.967:I /时间轴(4962):时间线:Activity_idle ID:android.os.BinderProxy@109f7fdd时间:14087719
    一月5日至18日:00:06.977:I /时间轴(4962):时间线:Activity_idle ID:android.os.BinderProxy@c4a769时间:14087720
    一月5日至18日:00:06.997:我/的System.out(4962):在StringBulder之前的输出[7,31]
    一月5日至18日:00:06.997:我/的System.out(4962):在StringBulder的输出:{身份:201,路线:[7,31]}
    一月5日至18日:00:06.997:我/的System.out(4962):doInBackground {陆委会:在输出10:A5:D0:06:6:E9,纬度:53.8689588,东经:10.66550319,路线:4}
    一月5日至18日:00:06.997:我/的System.out(4962):(HTTPLOG)静电:isSBSettingEnabled假
    一月5日至18日:00:07.017:I /时间轴(4962):时间线:Activity_launch_request ID:com.bustracker时间:14087762
    一月5日至18日:00:07.077:D /活动(4962):performCreate呼叫secproduct功能valuefalse
    一月5日至18日:00:07.077:D /活动(4962):performCreate调用调试弹性valuetrue
    一月5日至18日:00:07.147:我/的System.out(4962):在StringBulder之前的输出[7,31]
    一月5日至18日:00:07.147:我/的System.out(4962):在StringBulder的输出:{身份:201,路线:[7,31]}
    一月5日至18日:00:07.197:I /时间轴(4962):时间线:Activity_launch_request ID:com.bustracker时间:14087944
    一月5日至18日:00:07.297:D /活动(4962):performCreate呼叫secproduct功能valuefalse
    一月5日至18日:00:07.297:D /活动(4962):performCreate调用调试弹性valuetrue
    一月5日至18日:00:07.468:I /时间轴(4962):时间线:Activity_idle ID:android.os.BinderProxy@255edd7c时间:14088211
    一月5日至18日:00:07.468:I /时间轴(4962):时间线:Activity_idle ID:android.os.BinderProxy@2134ae11时间:14088211
    一月5日至18日:00:07.828:V / ActivityThread(4962):updateVisibility:ActivityRecord {1ac84605 token=android.os.BinderProxy@37a1708c {com.bustracker / com.bustracker.MainActivity}}显示:假的
    一月5日至18日:00:07.828:V / ActivityThread(4962):updateVisibility:ActivityRecord {1979865a token=android.os.BinderProxy@c867c1 {com.bustracker / com.bustracker.MainActivity}}显示:假的
    一月5日至18日:00:07.828:V / ActivityThread(4962):updateVisibility:ActivityRecord {2349d08b token=android.os.BinderProxy@16efd0b5 {com.bustracker / com.bustracker.MainActivity}}显示:假的
    一月5日至18日:00:07.828:V / ActivityThread(4962):updateVisibility:ActivityRecord {1a7a5568 token=android.os.BinderProxy@c4a769 {com.bustracker / com.bustracker.MainActivity}}显示:假的
    一月5日至18日:00:07.828:V / ActivityThread(4962):updateVisibility:ActivityRecord {28c3d81 token=android.os.BinderProxy@109f7fdd {com.bustracker / com.bustracker.MainActivity}}显示:假的
    一月5日至18日:00:07.828:V / ActivityThread(4962):updateVisibility:ActivityRecord {2339ae26 token=android.os.BinderProxy@2134ae11 {com.bustracker / com.bustracker.MainActivity}}显示:假的一月5日至18日:04:37.191:E / ActivityThread(4962):执行活动的停止未恢复:{com.bustracker / com.bustracker.MainActivity}
一月5日至18日:04:37.191:E / ActivityThread(4962):了java.lang.RuntimeException:表演活动,停止未恢复:{com.bustracker / com.bustracker.MainActivity}
一月5日至18日:04:37.191:E / ActivityThread(4962):在android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3822)
一月5日至18日:04:37.191:E / ActivityThread(4962):在android.app.ActivityThread.handleStopActivity(ActivityThread.java:3910)
一月5日至18日:04:37.191:E / ActivityThread(4962):在android.app.ActivityThread.access $ 1200(ActivityThread.java:177)
一月5日至18日:04:37.191:E / ActivityThread(4962):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477)
一月5日至18日:04:37.191:E / ActivityThread(4962):在android.os.Handler.dispatchMessage(Handler.java:102)
一月5日至18日:04:37.191:E / ActivityThread(4962):在android.os.Looper.loop(Looper.java:145)
一月5日至18日:04:37.191:E / ActivityThread(4962):在android.app.ActivityThread.main(ActivityThread.java:5944)
一月5日至18日:04:37.191:E / ActivityThread(4962):在java.lang.reflect.Method.invoke(本机方法)
一月5日至18日:04:37.191:E / ActivityThread(4962):在java.lang.reflect.Method.invoke(Method.java:372)
一月5日至18日:04:37.191:E / ActivityThread(4962):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1389)
一月5日至18日:04:37.191:E / ActivityThread(4962):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)


解决方案

有在code了一些问题,但我看到的主要问题是,我们在调用 getExtras( )直接调用后 sender.post_data(),这将启动AsyncTask的。请记住,通过定义一个AsyncTask的是异步的,所以你要等到它的使用从它的结果任何数据之前完成。

您也有变失配的位置:

 捆绑额外= getIntent()getExtras();
         如果(临时演员!= NULL){

此外,它看起来就像你没有覆盖一些必要将覆盖LocationListener的。

尝试使用 onNewIntent(),以捕获后进来的新意图的AsyncTask 已完成,看看是否能工程。见这里文档。

此外,添加的android:launchMode =singleTop到AndroidManifest.xml为MainActivity:

 <活动
    机器人:名字=。MainActivity
    机器人:launchMode =singleTop

编辑:从日志,它看起来像你的后台线程都可以从该的AsyncTask 被称为太频繁备份

尝试给它多一点的时间来执行。
有一点需要注意的是,每次你叫 startActivity() onPostExecute(),您MainActivity将被暂停,再次恢复。
因此,它看起来像你只是在做这种方式过于频繁(根据记录每秒多次)。

尝试用稍大的间隔注册您的位置更新,并给它一个小的最小距离为好,看看能否解决:

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

全部code:

 公共类MainActivity扩展ActionBarActivity {    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        LM的LocationManager =(的LocationManager)getSystemService(Context.LOCATION_SERVICE);
        //我说这个//此处。
        LocationListener的LL =新myLocationListener(本);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,10000,0.1F,LL);    }    @覆盖
    保护无效onResume(){
        super.onResume();
    }    @覆盖
    保护无效的onPause(){
        super.onPause();
    }    @覆盖
    保护无效onNewIntent(意向意图){
        super.onNewIntent(意向);
        // getIntent()应该总是返回最新
        setIntent(意向);        //我在这里补充这部分接收来自onPostExecute的意图//
        捆绑额外= getIntent()getExtras()。
        如果(临时演员!= NULL){
            ArrayList的<整数GT; C =演员
                    .getIntegerArrayList(stop_route);
            对于(INT项目:C){
                的System.out.println(这样做的目的不为空
                        +项目);
            }
        }
    }    在MainActivity //内部类
    类myLocationListener实现LocationListener的{
        //我说这里的bContext和构造函数//
        最后上下文bContext;        公共myLocationListener(上下文的背景下){
            bContext =背景;
        }        @覆盖
        公共无效onLocationChanged(地点){            发件人的PostData =新的PostData();
            //我在这里补充的背景下parameter.//
            sender.post_data(jSONString,bContext);
        }        @覆盖
        公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){        }        @覆盖
        公共无效onProviderEnabled(字符串提供商){        }        @覆盖
        公共无效onProviderDisabled(字符串提供商){        }    }
}

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)
解决方案

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

        }

    }
}

这篇关于该应用程序不断自动重启本身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 14:44
查看更多