2013-01-22 31 views
-1

我需要指導一個班級在我的位置改變後運行。任何人都可以幫助我。我認爲這個問題是因爲這個代碼:當位置改變時調用一個班級

Intent i = new Intent(getApplicationContext(), CreateNewProduct.class); 
startActivity(i); 

這是整個代碼:

(這是我把一些情況時,我的位置變化,也這是我必須調用部分下面的下一個類。)

class myLocationlistener implements LocationListener { 
     @Override 
     public void onLocationChanged(Location location) { 
      if (location != null) { 
       double pLong = location.getLongitude(); 
       double pLat = location.getLatitude(); 
       textLat.setText(Double.toString(pLat)); 
       textLong.setText(Double.toString(pLong)); 

       Intent i = new Intent(getApplicationContext(), CreateNewProduct.class); 
       startActivity(i); 
      } 
     } 
     @Override 
     public void onProviderDisabled(String provider) { 
     } 
     @Override 
     public void onProviderEnabled(String provider) { 
     } 
     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
     } 
    } 

(類被稱爲)

class CreateNewProduct extends AsyncTask<String, String, String> { 
     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(Sample.this); 
      pDialog.setMessage("Sending Location"); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 
     /** 
     * Creating product 
     * */ 
     protected String doInBackground(String... args) { 
      String latitude = textLong.getText().toString(); 
      String longitude = textLat.getText().toString(); 
      // Building Parameters 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("latitude", latitude)); 
      params.add(new BasicNameValuePair("longitude", longitude)); 
      // getting JSON Object 
      // Note that create product url accepts POST method 
      JSONObject json = jsonParser.makeHttpRequest(url_create_product, 
        "POST", params); 
      // check log cat fro response 
      Log.d("Create Response", json.toString()); 
      // check for success tag 
      try { 
       int success = json.getInt(TAG_SUCCESS); 
       if (success == 1) { 
        Toast.makeText(getBaseContext(), "Success", 
          Toast.LENGTH_SHORT).show(); 
       } else { 
        // failed to create product 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 
     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String file_url) { 
      // dismiss the dialog once done 
      pDialog.dismiss(); 
     } 
    } 
+2

startActivity,正如它的名字傾向於表明,啓動一個** **活動,不是的AsyncTask。 – njzk2

+0

那麼我應該怎樣用asynctask打電話給我的班級呢?謝謝 –

+0

我爲你寫的回答... – TN888

回答

2

你應該ê xecute aynctask作爲

new CreateNewProduct().execute(); 

因爲startActivity()只是如名字所暗示的

+0

謝謝Shreya S,我的問題解決了。 :) –

2

類CreateNewProduct不活動開始的活性。您無法開始使用Intent和startActivity()方法。最好的解決方案是創建這個類的對象並從這個類中調用方法。

代碼示例:

@Override 

     public void onLocationChanged(Location location) { 

      if (location != null) { 

       double pLong = location.getLongitude(); 

       double pLat = location.getLatitude(); 

       textLat.setText(Double.toString(pLat)); 

       textLong.setText(Double.toString(pLong)); 

       CreateNewProduct p = new CreateNewProduct(); 
       p.execute(); 

      } 
     } 

我希望我幫助。如果你有更多的問題,請問!

0

與調用類不同,您可以使用LocationManager類爲所需位置添加接近度。

你可以使用addProximityAlert()方法和PendingIntent來實現它。

將在PendingIntent中的位置更改上執行的代碼。

Android Proximity Alert

相關問題