2012-02-07 9 views
0

私は電話の一意のIDを設定しようとしているが、各時間は、ユーザーがボタンを開始し、それが新しいIDを生成し、データベースに送信します。アプリはユーザーの場所を追跡し、IDを1回設定してトラッキングが開始されるたびに常にユーザーが同じ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

+0

私が知る限り、エミュレータを再コンパイルしないでください。しかし、エミュレータ上で実行している場合を検出し、上記の方法が一般的に価値があると思った場合は、動作が異なります。あなたの場合は、それは価値があるよりも多くのトラブルかもしれないように聞こえる。 –

0

IDを一度だけ生成し、どこかに保存します(ファイル、DB ...)。保存されたIDが存在する場合

  1. チェック。
  2. もしそうなら、それを読んでください。 5.
  3. IDの生成に進みます。
  4. IDを保存します。
  5. IDを送信します。
0

すべてのアンドロイドデバイスは、そのIDを使用するだけで、あなたはtelephonymanagerオブジェクトと呼び出しgetidメソッドからこのIDを取得することができます非常に異なるアプローチは、それを好きではないかもしれません。 UはidがsharedpreferencesにそのIDを保存し、

関連する問題