1

ユーザーを追跡し、理論的には動物である別のユーザーを追跡するアプリを作成しています。私のアプリは次のようになります。ユーザー名を登録してパスすると、ユーザーは正しいユーザー名とパスワードを再入力してマップにログインできます。ここから問題が始まります。画面を作成すると、ユーザーの現在地がマップに読み込まれ、「動物」電話に SMS が自動送信されて GPS の詳細が要求されます。これにより、2 つの SMS メッセージが返されます。1 つは GPS 情報が含まれています。この情報を読み取って経度と緯度のデータを抽出し、それを double に変換してからマップ アクティビティに渡して、lnglat 変数に変換し、Google マップにマーカーで表示する SmsReceiver クラスがあります。今私が抱えている問題は、SMS が GPS 情報を返すのに数分かかることです。これが完了し、座標を地図ページに送信するために意図が使用されている場合、ボタンをクリックして経度と緯度は AnimalCoordinate に結合され、マーカーが表示されますが、時間のギャップがあるため、SMS が取得されると同時にボタンを押すことは不可能であり、データが smsreceiver クラスから何も送信されていないため、クラッシュが発生します。反対側で、onclick メソッドから意図を取り出すと、同じことが起こりますが、逆に、マップは意図を実行しますが、情報はまだ存在せず、クラッシュします。これは悪夢だったので、どんな助けでも大歓迎です。これが完了し、座標を地図ページに送信するためにインテントが使用される場合、経度と緯度が AnimalCoordinate に結合され、マーカーが表示されるようにボタンをクリックする必要があります。ボタンと同時にSMSが取得され、データがsmsreceiverクラスから反対側の何も送信されていないため、クラッシュが発生します.onclickメソッドからインテントを取り出すと、同じことが起こりますが、逆に、マップはインテントを実行しますが、情報はまだ存在せず、クラッシュします。これは悪夢だったので、どんな助けでも大歓迎です。これが完了し、座標を地図ページに送信するためにインテントが使用される場合、経度と緯度が AnimalCoordinate に結合され、マーカーが表示されるようにボタンをクリックする必要があります。ボタンと同時にSMSが取得され、データがsmsreceiverクラスから反対側の何も送信されていないため、クラッシュが発生します.onclickメソッドからインテントを取り出すと、同じことが起こりますが、逆に、マップはインテントを実行しますが、情報はまだ存在せず、クラッシュします。これは悪夢だったので、どんな助けでも大歓迎です。ただし、時間差があるため、SMS が取得されると同時にボタンを押すことは不可能であり、データが smsreceiver クラスから反対側の何も送信されていないため、クラッシュが発生します。 onclick メソッドでも同じことが起こりますが、逆に、マップはインテントを実行しますが、情報はまだ存在せず、クラッシュします。これは悪夢だったので、どんな助けでも大歓迎です。ただし、時間差があるため、SMS が取得されると同時にボタンを押すことは不可能であり、データが smsreceiver クラスから反対側の何も送信されていないため、クラッシュが発生します。 onclick メソッドでも同じことが起こりますが、逆に、マップはインテントを実行しますが、情報はまだ存在せず、クラッシュします。これは悪夢だったので、どんな助けでも大歓迎です。

また、説明が複雑すぎて申し訳ありません。できる限り説明されていることを確認したかったのです。2 つのクラスのコードは以下のとおりです。

Map class

public class MainScreen extends FragmentActivity implements LocationListener {
private GoogleMap map;
private LocationManager locationManager;
private String provider;
final Context context = this;



   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_screen);
    map = ((SupportMapFragment)getSupportFragmentManager().
    findFragmentById(R.id.map)).getMap();

    LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
    boolean enabledGPS = service
            .isProviderEnabled(LocationManager.GPS_PROVIDER);
    boolean enabledWiFi = service
            .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    // Check if enabled and if not send user to the GSP settings
    if (!enabledGPS) {
        Toast.makeText(this, "GPS signal not found", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
    }

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // Define the criteria how to select the locatioin provider -> use
    // default
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(provider);

    // Initialize the location fields
    if (location != null) {
        Toast.makeText(this, "Selected Provider " + provider,
                Toast.LENGTH_SHORT).show();
        onLocationChanged(location);
    } else {

        //do something
    }
    // Sets the map type to be "hybrid"
    map.setMapType(GoogleMap.MAP_TYPE_HYBRID);

    Bundle b = getIntent().getExtras();


    double lat =  location.getLatitude();
    double lng = location.getLongitude();
    Toast.makeText(this, "Location " + lat+","+lng,
            Toast.LENGTH_LONG).show();
    LatLng Usercoordinate = new LatLng(lat, lng);
    Marker User = map.addMarker(new MarkerOptions()
    .position(Usercoordinate)
    .title("You are here")
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
    //Move the camera instantly to user with a zoom of 15.
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(Usercoordinate, 15));

    // Zoom in, animating the camera.
    map.animateCamera(CameraUpdateFactory.zoomTo(18), 2000, null);

    //Sends sms to 'animal phone'
      String phoneNo = "***********";
      String sms = "GPSLocation";

      try {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNo, null, sms, null, null);
        Toast.makeText(getApplicationContext(), "SMS Sent!",
                    Toast.LENGTH_LONG).show();
      } catch (Exception e) {
        Toast.makeText(getApplicationContext(),
            "SMS faild, please try again later!",
            Toast.LENGTH_LONG).show();
        e.printStackTrace();
      }

    }

public void map_help(View view) {
    //method for the help button

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            context);

        // set title
        alertDialogBuilder.setTitle("Help");

        // set dialog message
        alertDialogBuilder
     .setMessage("Click the 'Pet' button to display the pets location." +
                    "This can take a few minutes to retrieve.")
            .setCancelable(false)
    .setPositiveButton("ok",new   DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog,int id)        {
                    // if this button is clicked, close
                    // current activity
                    dialog.cancel();
                }
              });
            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();
                                };

public void Find_Pet(View view)
{

      //String phoneNo = "07516909014";
     // String sms = "GPSLocation";

     // try {
    //  SmsManager smsManager = SmsManager.getDefault();
        //smsManager.sendTextMessage(phoneNo, null, sms, null, null);
        //Toast.makeText(getApplicationContext(), "SMS Sent!",
            //      Toast.LENGTH_LONG).show();
//    } catch (Exception e) {
    //  Toast.makeText(getApplicationContext(),
        //  "SMS faild, please try again later!",
            //Toast.LENGTH_LONG).show();
    //  e.printStackTrace();
      //}
}

public void Show_Pet(View view)
{
    //gets coordinates from SmsReceiver
    Bundle b = getIntent().getExtras();
    double AnimalLat = b.getDouble("key");

    Bundle d = getIntent().getExtras();
    double AnimalLon = d.getDouble("key1");

    LatLng Animalcoordinate = new LatLng(AnimalLat, AnimalLon);
    //adds pets marker on map
    Marker Animal = map.addMarker(new MarkerOptions()
    .position(Animalcoordinate)
    .title("Your pet is here")
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
}


/* Request updates at startup */
@Override
protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(provider, 400, 1, this);
}

/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
}

@Override
public void onLocationChanged(Location location) {


}


@Override
public void onProviderDisabled(String provider) {
    Toast.makeText(this, "Enabled new provider " + provider,
            Toast.LENGTH_SHORT).show();

}


@Override
public void onProviderEnabled(String provider) {
    Toast.makeText(this, "Disabled provider " + provider,
            Toast.LENGTH_SHORT).show();

}


@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

}

SmsReceiver クラス

public class SmsReceiver extends BroadcastReceiver
{
String lat = null;
String lon = null;
String message = null;
final SmsReceiver context = this;

@Override
public void onReceive(Context context, Intent intent) 
{
    //---get the SMS message passed in---
    Bundle bundle = intent.getExtras();        
    SmsMessage[] msgs = null;
    String str = "";            
    if (bundle != null)
    {
        //---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];            
        for (int i=0; i<msgs.length; i++){
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
            str += msgs[i].getMessageBody().toString();

        }}

            message = str.toString();
            String[] test = message.split("");
            char[] test2 = test[1].toCharArray(); 
            //if the first character of the sms is C then read gps information
            if (test2[0] == 'C' || test2[0] =='c')
            {
            lat = message.substring(45, 56);
            lon = message.substring(67, 78);

            double AnimalLat=Double.parseDouble(lat);
            double AnimalLon=Double.parseDouble(lon);

            //Pass coordinates to MainScreen
            Intent a = new Intent(getApplicationContext(), MainScreen.class);
            Bundle b = new Bundle();
            b.putDouble("key", AnimalLat);
            a.putExtras(b);
            startActivity(a);

            Intent c = new Intent(getApplicationContext(), MainScreen.class);
            Bundle d = new Bundle();
            d.putDouble("key1", AnimalLon);
            c.putExtras(d);
            startActivity(c);

            }else {
            }       

   }

private void startActivity(Intent a) {
    // TODO Auto-generated method stub

}

private Context getApplicationContext() {
    // TODO Auto-generated method stub
    return null;
}}

コードのレイアウトについてもお詫び申し上げます。このサイトにコードを貼り付けたのはこれが初めてです。再度、感謝します。

4

1 に答える 1