本帖最后由 love_xiaozhao 于 2011-06-23 08:30:45 编辑

解决方案 »

  1.   

      Android开发中Activity切换导致的onCreate重复执行的问题
    在我们用Android开发过程中,会碰到Activity在切换到后台或布局从横屏LANDSCAPE切换到PORTRAIT,会重新切换Activity会触发一次onCreate方法。在Android开发中这种情况视可以避免的,我们可以在androidmanifest.xml中的activit元素加入这个属性android:configChanges="orientation|keyboardHidden" 就能有效避免oncreat方法的重复加载, androidmanifest.xml内容如下:红色字体为添加部分<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.demo"
          android:versionCode="1"
          android:versionName="1.0">
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".DemoGPS"
                android:configChanges="orientation|keyboardHidden"
                      android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
      <uses-library android:name="com.google.android.maps" />    </application>
        <uses-sdk android:minSdkVersion="7" /> <uses-permission android:name="android.permission.INTERNET"></uses-permission>
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
     
    </manifest>   同时在Activity的Java文件中重载onConfigurationChanged(Configuration newConfig)这个方法,这样就不会在布局切换或窗口切换时重载等方法。代码如下: @Override 
        public void onConfigurationChanged(Configuration newConfig)
        { 
            super.onConfigurationChanged(newConfig); 
         if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
         {
    //land
         }
         else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
         {
    //port
         }
        }本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ocean20/archive/2010/10/31/5977615.aspx
      

  2.   

    public  int ping() {
    String system = (String) (System.getProperty("os.name")).toLowerCase();
    Log.d("eairflux", "system----------------" + system);
    String  pingCommand = "";
    if (system.indexOf("win") != -1) {
    pingCommand = "ping " + host + " -l " + leng + " -c "
    + count +  " -i " + interval_data + " -w "
    + wait;//" -t " + ping.getReExeTimes() + " -n "+ ping.getReExeTimes() +

    } else if (system.indexOf("linux") != -1) {
    pingCommand = "ping " + host + " -l " + leng + " -c "
    + count +  " -i " + interval_data + " -w "
    + wait;
    } else {
    pingCommand = "ping " + host + " -l " + leng + " -c "
    + count +  " -i " + interval_data + " -w "
    + wait;

    }
    Log.d("eairflux", "pingCommand--------------------" + pingCommand);
    int minTime = Integer.MAX_VALUE, curTime;
    try {
    Process process; process = Runtime.getRuntime().exec("ping " + host);
    Log.d("eairflux", "process--------------------" + process);
    BufferedReader in = new BufferedReader(new InputStreamReader(
    process.getInputStream()));
    String line="";
    int count = 10, index;
    // 最多只读10行
    Log.d("eairflux", "line--------------------" + line);
    if(in.readLine()==null||in.readLine().equals(""))
    {
    return 0;
    }
    while (!((line = in.readLine()).equals("")) && count-- != 0) {
    Log.d("eairflux", "in.readLine()--------------------" + in.readLine());
    line = line.toLowerCase();
    Log.d("eairflux", "line.toLowerCase()--------------------" + line.toLowerCase());
    if ((index = line.indexOf("time")) != -1) {
    Log.d("eairflux", "buf--------------------" + line.getBytes());

    byte[] buf = line.getBytes();
    int start = 0, end = buf.length, i, j;
    for (i = index + 4; i < buf.length; i++) {
    if (Character.isDigit((char) buf[i])) {
    start = i;
    Log.d("eairflux", "start------------" + start);
    break;
    }
    }
    if (i == buf.length){
    Log.d("eairflux", "buf.length------------" + buf.length);
    continue;
    }
    for (j = start; j < buf.length; j++) {
    if (Character.isLetter((char) buf[j])) {
    end = j;
    Log.d("eairflux", "end------------" + end);
    break;
    }
    }
    curTime = Integer.parseInt(new String(buf, start, end
    - start));
    if (curTime < minTime) {
    Log.d("eairflux", "minTime------------" + minTime);
    Log.d("eairflux", "curTime------------" + curTime);

    minTime = curTime;
    }
    }
    }
    } catch (NumberFormatException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return minTime;
    }