2011-12-08 6 views
1

안녕하세요, 다음 코드에 대한 설명이 필요합니다. 여기 때때로 자바 스크립트 기능이 작동하고 언젠가는 그렇지 않습니다. 결과적으로 나는 어디에 문제가 있는지 정말로 알지 못한다. 여기에서 내 index.html 파일은지도 위치를 자동으로로드합니다. 그런 다음 인수로 현재 lat, lng javascript 함수를 호출합니다. 그래서 처음에는 by default map을로드합니다. 그런 다음 javascript 함수로지도를 재정의해야합니다. 문제는 때로는 발생하지만 가끔은 발생하지 않습니다. 그래서 나는 이것에 대한 해답을 원합니다.Android Webview 불규칙한로드 문제

public class MapOptionsDemoModified extends Activity{ 
    //Geocoder geocoder; 
    WebView mWebView; 
    LocationManager mlocManager=null; 
    LocationListener mlocListener; 
    private MyLocationOverlay myLocationOverlay; 
    protected MapView map; 
    private RadioButton mapButton; 
    private RadioButton satelliteButton; 
    private ToggleButton trafficToggle; 
    private ToggleButton labelsToggle; 
    private Configuration config; 
    EditText LOC; 
    Button routesbtn,settingsbtn; 
    public String location="State Street" 
    double lati,longi; 
    GeoPoint currentLocation; 
    double curlat,curlong; 
    InputMethodManager imm; 
    //String adrs; 
    double latitude=40.07546; 
    double longitude=-76.329999; 


    String adrs=""; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.map_options_modified); 
     lati=34.1161; 
     longi=-118.149399; 
     adrs=""; 
     routesbtn=(Button)findViewById(R.id.mom_bt2); 

     mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
     mlocListener = new MyLocationListener(); 
     mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener); 
     mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 


     mWebView = (WebView) findViewById(R.id.webview); 
     WebSettings webSettings = mWebView.getSettings(); 
     webSettings.setJavaScriptEnabled(true); 
     mWebView.loadUrl("file:///android_asset/index.html"); 
     //mWebView.loadUrl("http://www.google.com"); 
     webSettings.setBuiltInZoomControls(true); 
     webSettings.setSupportZoom(true); 
     imm=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 




     /* taking data from caller activity page */ 
     // ###################Receiving data starts 
     Bundle extras = getIntent().getExtras(); 
     if (extras == null) { 
     return; 
     } 
     lati=extras.getDouble("latitude"); 
     longi = extras.getDouble("longitude"); 
     adrs=extras.getString("adrs"); 

     // ##### Receiving data ends 


     AlertDialog.Builder pdb=new AlertDialog.Builder(MapOptionsDemoModified.this);   
     pdb.setTitle("GPS"); 
     pdb.setMessage(adrs+" "+Double.toString(lati)+" "+Double.toString(longi)); 
     pdb.setPositiveButton("Ok", null); 
     pdb.show(); 


     mWebView.loadUrl("javascript:getCurrentLocation("+lati+","+longi+")"); 


     routesbtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 


       if(mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ 
        //while(j<cnt) 
        //{ 

         mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener); 
         if(MyLocationListener.latitude>0) 
         { 
          latitude=MyLocationListener.latitude; 
          longitude=MyLocationListener.longitude; 
          //flag=1; 

          Geocoder gcd = new Geocoder(MapOptionsDemoModified.this, Locale.getDefault()); 
          List<Address> addresses; 
          try { 
           addresses = gcd.getFromLocation(latitude,longitude, 5); 
           if (addresses.size() > 0) 
           { 
            adrs=addresses.get(0).getAddressLine(0)+" "+addresses.get(0).getAddressLine(1)+" "+addresses.get(0).getAddressLine(2); 
           } 
          } catch (IOException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 

          //break; 

          finish(); 
          Intent i = new Intent(MapOptionsDemoModified.this,RoutesPageOne.class); 
          i.putExtra("adrs", adrs); 
          i.putExtra("latitude", latitude); 
          i.putExtra("longitude", longitude); 
          startActivity(i); 




         } 
         else 
         { 

          AlertDialog.Builder pdb=new AlertDialog.Builder(MapOptionsDemoModified.this);   
          pdb.setTitle("GPS"); 
          pdb.setMessage("GPS activation in progress"); 
          pdb.setPositiveButton("Ok", null); 
          pdb.show(); 

         } 

        //} 

       } 
       else { 

        AlertDialog.Builder pdb=new AlertDialog.Builder(MapOptionsDemoModified.this);   
        pdb.setTitle("GPS"); 
        pdb.setMessage("GPS is not turned on..., please start gps"); 
        pdb.setPositiveButton("Ok", null); 
        pdb.show(); 
       } 

      } 
     }); 




     LOC=(EditText)findViewById(R.id.mom_editText1);  
     LOC.setOnKeyListener(new OnKeyListener() { 

      @Override 
      public boolean onKey(View v, int keyCode, KeyEvent event) { 
       // TODO Auto-generated method stub 
       location = LOC.getText().toString(); 

       if (keyCode == KeyEvent.KEYCODE_ENTER && location.length() > 0) { 

        LOC.setText(""); 

        imm.hideSoftInputFromWindow(LOC.getWindowToken(), 0); 

        mWebView.loadUrl("javascript:getLocation('" + location + "')"); 


        /*AlertDialog.Builder edb=new AlertDialog.Builder(MapOptionsDemoModified.this);   
        edb.setTitle("gps"); 
        edb.setMessage(location+" lat= "+lati+" long="+longi); 
        edb.setPositiveButton("Ok", null); 
        edb.show();*/ 



        //searchBarcode(barcode); 
        return true; 
       } 

       return false; 
      } 

      }); 



     //imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0); 
     getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
     mWebView.setWebViewClient(new HelloWebViewClient()); 






    private class HelloWebViewClient extends WebViewClient { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      view.loadUrl(url); 
      return true; 
     } 
    } 

    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) { 
      mWebView.goBack(); 
      return true; 
     } 
     return super.onKeyDown(keyCode, event); 
    } 


} 

답변

0

문제가 발생했습니다. 때로는 첫 번째 URL에로드하는 데 더 많은 시간이 걸렸습니다. 결과적으로 두 번째 URL이 호출되지 않았습니다. 그래서 나는 loadUrl 호출 사이에 SystemClock.sleep(1000)을 포함 시켰고 매력처럼 작동했습니다.