2012-02-07 122 views
0

我試圖爲手機設置唯一的ID,但每次用戶啓動按鈕時,它都會生成一個新ID並將其發送到數據庫。該應用程序將跟蹤用戶的位置,我想設置一次ID,以便每次啓動跟蹤時,用戶將始終具有相同的ID。目前,該程序將發送該ID,並且每次發送時都保持不變。然而,這個問題發生在用戶返回主屏幕並返回到屏幕以啓動跟蹤。以下是相關的例子:設置Android手機的唯一ID

  1. 用戶發起跟蹤
  2. 用戶返回到主屏幕(原始ID丟失)
  3. 用戶將返回按鈕,屏幕啓動跟蹤
  4. 用戶開始再次跟蹤(這會生成一個新的ID)

我想保留ID而不是每次用戶啓動跟蹤時生成一個新ID。有關如何做到這一點的任何想法?以下是一些可能有所幫助的代碼。

public class SendAlert extends Activity { 


    private Button help_button; 
    private TextView latitude; 
    private TextView longitude; 
    private TextView id; 
    Button sendButton; 
    EditText msgTextField; 

    String uuid = UUID.randomUUID().toString(); 

    private LocationManager locManager; 
    private LocationListener locListener; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.sendalert); 

     SharedPreferences shared = getSharedPreferences("PEOPLE_PREFERENCES", MODE_PRIVATE); 
     final String first = shared.getString("FIRSTNAME", ""); 
     final String last = shared.getString("LASTNAME", ""); 
     final long phone = shared.getLong("PHONE", 0); 
     final String city = shared.getString("CITY", ""); 
     final int zip = shared.getInt("ZIP", 0); 

     help_button = (Button)findViewById(R.id.button1); 
     latitude = (TextView)findViewById(R.id.label_latitude); 
     longitude = (TextView)findViewById(R.id.label_longitude); 
     id = (TextView)findViewById(R.id.label_id); 

     help_button.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startLocation(); 
       //id.setText(String.valueOf(uuid)); 

       sendId(first, last, phone, city, zip); 
      } 
     }); 
    } 

    private void startLocation() 
    { 

     //We get a reference to the LocationManager 
     locManager = 
      (LocationManager)getSystemService(Context.LOCATION_SERVICE); 

     //We get the last known position 
     Location loc = 
      locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

     //We show the last known position 
     showPosition(loc); 

     //We checked to receive updates from the position 
     locListener = new LocationListener() { 
      public void onLocationChanged(Location location) { 
       showPosition(location); 
      } 
      public void onProviderDisabled(String provider){ 
       //labelState.setText("Provider OFF"); 
      } 
      public void onProviderEnabled(String provider){ 
       //labelState.setText("Provider ON "); 
      } 
      public void onStatusChanged(String provider, int status, Bundle extras){ 
       //Log.i("", "Provider Status: " + status); 
       //labelState.setText("Provider Status: " + status); 
      } 
     }; 

     locManager.requestLocationUpdates(
       LocationManager.GPS_PROVIDER, 0, 0, locListener); 
    } 

    private void showPosition(Location loc) { 
     if(loc != null) 
     { 
      latitude.setText(String.valueOf(loc.getLatitude())); 
      longitude.setText(String.valueOf(loc.getLongitude())); 
      id.setText(String.valueOf(uuid)); 
      Log.i("", String.valueOf(loc.getLatitude() + " - " + String.valueOf(loc.getLongitude()))); 


      send(latitude); 


     } 
     else 
     { 
      latitude.setText("Latitude: No Data"); 
      longitude.setText("Longitude: No Data"); 
     } 
    } 

    private void send(View v) 
    { 
     // get the message from the message text box 
     //String msg = latitude.getText().toString() + "," + longitude.getText().toString(); s 
     String lat = latitude.getText().toString(); 
     String lon = longitude.getText().toString(); 

     // make sure the fields are not empty 
     //if (lat.length()>0) 
     if (lat != "0" && lon != "0") 
     { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://www.example.com/receive.php"); 
     try { 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); //changed to 4 
      nameValuePairs.add(new BasicNameValuePair("lat", lat)); //changed "message" to "lat" changed "msg" to "lat" 
      nameValuePairs.add(new BasicNameValuePair("lon", lon)); //added this line 
      nameValuePairs.add(new BasicNameValuePair("id", uuid)); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      httpclient.execute(httppost); 
      //msgTextField.setText(""); // clear text box 
     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
     } 

     } 
     else 
     { 
      // display message if text fields are empty 
      Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show(); 
     } 

    } 

    private void sendId(String first, String last, long phone, String city, int zip) 
    { 
     // get the message from the message text box 
     String user_id = id.getText().toString(); 

     // make sure the fields are not empty 
     //if (lat.length()>0) 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://www.example.com/receive_user.php"); 
     try { 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); //changed to 4 
      nameValuePairs.add(new BasicNameValuePair("id", user_id)); 
      nameValuePairs.add(new BasicNameValuePair("firstname", first)); 
      nameValuePairs.add(new BasicNameValuePair("lastname", last)); 
      nameValuePairs.add(new BasicNameValuePair("phone", Long.toString(phone))); 
      nameValuePairs.add(new BasicNameValuePair("city", city)); 
      nameValuePairs.add(new BasicNameValuePair("zip", Integer.toString(zip))); 

      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      httpclient.execute(httppost); 
      //msgTextField.setText(""); // clear text box 
     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
     } 

     } 

    } 

回答

0

正如前面的海報指出的那樣, TelephonyManager並獲取唯一的設備標識符。然而,重要的是要注意,這些信息的存在因設備而異 - 上述的TelephonyManager.getID()可能會返回null。

如果您的設備具備電話功能 - 例如某些目前沒有SIM卡的平板電腦或電話不是 - 那麼它很可能具有可用作唯一ID基礎的IMSI和IMEI。最佳做法應該是不要直接使用它,因爲它是個人信息 - 可能使用散列或校驗和。

上面的優點,而不是純粹的專有計算,是ID的唯一性的高几率 - 因爲源(例如IMEI)已知是唯一的 - 並且它可以重複計算飛行而不必存儲在任何地方。

+0

那麼我使用模擬器,所以它會返回0000 ...並將很難測試(我必須一次測試多個用戶)。有關如何設置ID並防止其被更改的任何建議? – mkyong 2012-02-07 13:38:40

+0

據我所知,無需重新編譯仿真器。但是,如果您認爲上述方法通常很有價值,您可以檢測到您在仿真器上運行並且行爲不同的情況。在你的情況下,它聽起來像它可能比它的價值更麻煩。 – 2012-02-07 13:55:07

0

只生成一次ID並將其保存在某處(文件,DB ...)。

  1. 檢查保存的ID是否存在。
  2. 如果有,請閱讀。轉到步驟5.
  3. 生成ID。
  4. 保存身份證。
  5. 發送ID。
0

它的一個非常不同的方法可能是你不會喜歡它,因爲每個Android設備都有一個唯一的id,只需使用該id,就可以從telephonymanager對象獲得此id並調用getid方法。當你收到該id時,將該id保存在sharedpreferences中並在需要時使用它