2012-04-11 76 views
0

我需要幫助形成我的android應用程序。在這個應用程序中,現在,我從表單末端獲取一些數據,然後將其放在數據庫中。但我需要顯示,我需要將當前的GPS位置和當前時間放在數據庫。我已經嘗試使用教程,並在論壇上看到其他職位沒有結果。我需要幫助,因爲我不知道如何工作位置偵聽器,哪裏需要更改項目。 謝謝。 這是我的代碼獲取GPS位置(長,經緯度)和時間在這個Android應用程序

FORM.java

import android.app.Activity; 
import android.content.ComponentName; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AutoCompleteTextView; 

public class Form extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 

    public void onAddClick(View botton) { 
     AutoCompleteTextView spedID = (AutoCompleteTextView) findViewById(R.id.SpedID); 
     AutoCompleteTextView spedCliente = (AutoCompleteTextView) findViewById(R.id.SpedCliente); 
     AutoCompleteTextView spedCorriere = (AutoCompleteTextView) findViewById(R.id.SpedCorriere); 

     Intent intent = new Intent(); 
     intent.setClass(this, Result.class); 

     intent.putExtra("SpedID", spedID.getText().toString()); 
     intent.putExtra("SpedCliente", spedCliente.getText().toString()); 
     intent.putExtra("SpedCorriere", spedCorriere.getText().toString()); 
     startActivity(intent); 
    } 

    public void onCancelClick(View botton) { 
     Intent intent = new Intent(); 
     intent.setComponent(new ComponentName(this, Result.class)); 
     intent.putExtra("Cancel", "Cancel"); 
     startActivity(intent); 
    } 

} 

RESULT.java

import android.app.Activity; 
import android.content.ContentValues; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.widget.TextView; 

public class Result extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.result); 

     TextView resultText = (TextView) findViewById(R.id.resultText); 
     Bundle bundle = getIntent().getExtras(); 

     if (bundle.getString("Cancel") != null) { 
      resultText.setText(getString(R.string.cancelOp)); 
     } else { 
      String spedID = bundle.getString("SpedID"); 
      String spedCliente = bundle.getString("SpedCliente"); 
      String spedCorriere = bundle.getString("SpedCorriere"); 
      insertSpedizione(spedID, spedCliente, spedCorriere); // metodo per 
                    // inserire 
                    // dati in 
                    // db 
      resultText.setText(getString(R.string.resultOk) + "\n" + spedID 
        + "\n" + spedCliente + "\n" + spedCorriere); 
     } 
    } 

    private void insertSpedizione(String idsped, String cliente, 
      String idcorriere) { 
     // metodo insertspedizione riceve in ingresso i parametri sped,cliente 
     // etc etc 
     DatabaseHelper databaseHelper = new DatabaseHelper(this); 
     SQLiteDatabase db = databaseHelper.getWritableDatabase(); // creo un 
                    // database 
                    // Srivibile 

     ContentValues cv = new ContentValues(); // Hashmap 
     cv.put(DatabaseHelper.IDSPED, idsped); // nome colonna, valore 
     cv.put(DatabaseHelper.CLIENTE, cliente);// nome colonna, valore 
     cv.put(DatabaseHelper.IDCORRIERE, idcorriere);// nome colonna, valore 

     db.insert("spedizioni", DatabaseHelper.IDSPED, cv); // faccio un insert 
                  // nella TABLE 
                  // spedizioni con 
                  // parametri IDSPED 
                  // not null e il 
                  // ContentValues 
     db.close(); // quando abbiamo finito chiudiamo 
    } 

} 

DATABASEHELPER.java

import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class DatabaseHelper extends SQLiteOpenHelper { 
    public static final String DATABASE_NAME = "datasped.db"; // nome database 
                   // che viene 
                   // creato da 
                   // SQLiteOpenHelper 
    public static final String IDSPED = "idsped"; 
    public static final String CLIENTE = "cliente"; 
    public static final String IDCORRIERE = "idcorriere"; 

    public DatabaseHelper(Context context) { 
     super(context, DATABASE_NAME, null, 1); // 1 è la versione iniziale del 
               // database che viene aggiornata 
               // con onUpgrade 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { // onCreate crea la 
               // tabella(formattazione, 
               // indici, etc) con tutti i dati 
               // ce voglio nel .db 
     db.execSQL("CREATE TABLE spedizioni (_id INTEGER PRIMARY KEY AUTOINCREMENT,idsped TEXT, cliente TEXT,idcorriere TEXT);"); 
    }// in questo caso mi basta creare una Table semplice con id incrementale 
     // per ogni dato inserito 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // cambia 
                       // la 
                       // versione 
                       // del 
                       // database 
     android.util.Log.w("spedizioni", 
       "Ugrading database, which will destroy all old data"); 
     db.execSQL("DROP TABLE IF EXIST spedizioni"); 
     onCreate(db); // ricreo il Database 
    } 

} 

的Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="matteo.android.SpedyGo" android:versionCode="1" 
    android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="9" /> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".Form" android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <activity android:name=".Result" android:label="@string/result"> 
     </activity> 
    </application> 
</manifest> 

的strings.xml

<resources> 
    <string name="hello">Hello World, Form!</string> 
    <string name="app_name">SpedyGo</string> 
    <string name="InserisciSpedizione">Inserisci Spedizione</string> 
    <string name="textsped">Id Spedizione</string> 
    <string name="textcliente">Cliente</string> 
    <string name="textCorriere">Id Corriere</string> 
    <string name="cancel">Cancella</string> 
    <string name="confirm">Conferma</string> 

    <string name="result">Risultato</string> 
    <string name="resultOk">Operazione Avvenuta con Successo</string> 
    <string name="cancelOp">Operazione Cancellata</string> 
</resources> 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:text="@string/InserisciSpedizione" 
     android:textSize="30sp" android:textStyle="bold" android:gravity="center" /> 
    <LinearLayout android:layout_width="match_parent" 
     android:layout_height="wrap_content" android:id="@+id/linearLayout1"> 
     <TextView android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:text="@string/textsped" 
      android:textStyle="bold" android:padding="5sp" android:width="120sp" 
      android:textSize="16sp"></TextView> 
     <AutoCompleteTextView android:layout_weight="1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" android:width="500sp" android:id="@+id/SpedID"></AutoCompleteTextView> 
    </LinearLayout> 
    <LinearLayout android:layout_width="match_parent" 
     android:layout_height="wrap_content" android:id="@+id/linearLayout1"> 
     <TextView android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:textStyle="bold" 
      android:padding="5sp" android:text="@string/textcliente" 
      android:textSize="16sp" android:width="120sp"></TextView> 
     <AutoCompleteTextView android:layout_weight="1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" android:width="500sp" android:id="@+id/SpedCliente"></AutoCompleteTextView> 
    </LinearLayout> 
    <LinearLayout android:layout_width="match_parent" 
     android:layout_height="wrap_content" android:id="@+id/linearLayout1"> 
     <TextView android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:textStyle="bold" 
      android:padding="5sp" android:text="@string/textCorriere" 
      android:textSize="16sp" android:width="120sp"></TextView> 
     <AutoCompleteTextView android:layout_weight="1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" android:width="500sp" android:id="@+id/SpedCorriere"></AutoCompleteTextView> 
    </LinearLayout> 
    <LinearLayout android:layout_width="match_parent" 
     android:layout_height="wrap_content" android:id="@+id/linearLayout2"> 
     <Button android:layout_height="wrap_content" android:text="@string/cancel" 
      android:layout_width="130sp" android:onClick="onCancelClick"></Button> 
     <Button android:layout_height="wrap_content" android:text="@string/confirm" 
      android:layout_width="130sp" android:onClick="onAddClick"></Button> 
    </LinearLayout> 
</LinearLayout> 

爲result.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:textStyle="bold" 
     android:focusableInTouchMode="false" android:textSize="25sp" 
     android:padding="10sp" android:gravity="center" android:text="@string/result" /> 
    <TextView android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:textStyle="bold" 
     android:focusableInTouchMode="false" android:textSize="20sp" 
     android:padding="10sp" android:gravity="center" android:text="" 
     android:id="@+id/resultText" /> 

</LinearLayout> 
+0

您在任何課程中都沒有位置偵聽器。在這種情況下,你到目前爲止嘗試過什麼?我建議首先確定當前位置 - 即構建一個能夠在創建數據庫之前執行此操作的類。在這一點上保持簡單!我認爲你已經看過這個http://developer.android.com/guide/topics/location/obtaining-user-location.html? – Katana24 2012-04-11 22:39:15

回答

0

聲明private LocationManager locationManager;

然後

locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new GeoUpdateHandler()); 

下創建一個類GeoUpdateHandler它實現LocationListener的和所具有的功能

public void onLocationChanged(Location location) { 
      int lat = (int) (location.getLatitude() * 1E6); 
      int lon = (int) (location.getLongitude() * 1E6); 
      String tim = String.format("%d", location.getTime());} 
0

這裏是檢索用戶的位置,使用所述網絡提供商

// Acquire a reference to the system Location Manager 
     myLocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 

     // Define a listener that responds to location updates 
     locationListener = new LocationListener() 
     { 
        @Override 
        public void onLocationChanged(Location location) 
        { 
         // Return a string representation of the latitude & longitude. Pass it to the textview and update the text 
         String tempLat = ""+location.getLatitude(); 
         String tempLon = ""+location.getLongitude(); 

         tempLat = refineStrg(tempLat); 
         tempLon = refineStrg(tempLon); 

         // Store/set lat and long values 
         storeLat_storeLong(tempLat, tempLon); 
        } 

        @Override 
        public void onProviderDisabled(String arg0) 
        { 
         // TODO Auto-generated method stub 

        } 

        @Override 
        public void onProviderEnabled(String provider) 
        { 
         // TODO Auto-generated method stub 

        } 

        @Override 
        public void onStatusChanged(String provider, int status, Bundle extras) 
        { 
         // TODO Auto-generated method stub 
        } 
     }; 
     // The two lines below request location updates from the network location provider 
     myLocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,locationListener); 

記住的一個簡單的例子在清單文件中包含適當的權限以訪問用戶位置(請參閱我在我的評論中發佈的網站)

最後,檢索當前時間,你可以使用日曆或Date類,像這樣:

Calendar cal = Calendar.getInstance(); 
cal.getTime(); 

here關於如何在日曆使用Date類的信息。

相關問題