为Android手机设置唯一ID

时间:2012-02-07 13:03:14

标签: android mysql gps uuid

我正在尝试为手机设置唯一ID,但每次用户启动按钮时,它都会生成一个新ID并将其发送到数据库。该应用程序将跟踪用户的位置,并且我想设置一次ID,以便每次启动跟踪时,用户将始终具有相同的ID。现在,程序将发送ID,每次发送时都会保持不变。然而,当用户返回主屏幕并返回到屏幕以启动跟踪时,会出现问题。以下是事件的示例:

  1. 用户启动跟踪
  2. 用户返回主屏幕(原始ID丢失)
  3. 用户返回屏幕,按钮启动跟踪
  4. 用户再次启动跟踪(这会生成新ID)
  5. 我希望保留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
             }
    
            }
    
        }
    

3 个答案:

答案 0 :(得分:0)

仅生成一次ID并将其保存在某处(文件,数据库......)。

  1. 检查是否存在已保存的ID。
  2. 如果是,请阅读。转到第5步。
  3. 生成ID。
  4. 保存ID。
  5. 发送ID。

答案 1 :(得分:0)

它的一种非常不同的方法可能是你不喜欢它,因为每个android设备都有一个唯一的id只使用那个id,你可以从telephonymanager对象获取这个id并调用getid方法。当你收到该id时,在sharedpreferences中保存该id并在需要时使用它

答案 2 :(得分:0)

如前一张海报所述,您可以访问例如TelephonyManager并获取唯一的设备标识符。但请务必注意,此信息的存在因设备而异 - 前面提到的TelephonyManager.getID()可能会返回null。

如果您的设备具有电话功能 - 例如某些平板电脑或目前没有SIM卡的手机 - 那么它可能会有一个IMSI和IMEI,您可以将其用作唯一ID的基础。最佳做法是不要直接使用它,因为它是个人信息 - 可能使用哈希或校验和。

上述优势,而不是纯粹的专有计算,是ID唯一性的高概率 - 因为已知源(例如IMEI)是唯一的 - 并且它可以在运行中重复计算而不是必须存储在任何地方。