0

GPS 座標を Web サービスに定期的に送信するトランスミッター アプリを作成しています。これはコードです:

package online.veda.transafe_tx;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.location.Location;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.media.ToneGenerator;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;


public class trip extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, LocationListener {

    public String tripID;
    private GoogleApiClient mGoogleApiClient;

    int sentData;

    ArrayList<LocationData> locationUpdates;

    TextView sentDataTextView;
    TextView bufferedDataTextView;


    void PlayAlertIfNeeded()
    {
        if(locationUpdates.size()>=5)
        {
            ToneGenerator toneGen1 = new ToneGenerator(AudioManager.STREAM_ALARM, 100);
            toneGen1.startTone(ToneGenerator.TONE_CDMA_ABBR_ALERT,500);
            toneGen1.startTone(ToneGenerator.TONE_CDMA_ABBR_ALERT,1000);
        }

    }

    void CreateOKDialog( String message)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(trip.this);
        builder.setTitle(message);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                startActivity(new Intent(trip.this, login.class));
                dialog.dismiss();
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }

    void SetSentBufferedData()
    {
        runOnUiThread(new Runnable() {
            @Override
            public void run()
            {
                sentDataTextView.setText(Integer.toString(sentData));
                bufferedDataTextView.setText(Integer.toString(locationUpdates.size()));
                if(locationUpdates.size()>0) {
                    bufferedDataTextView.setTextColor(Color.parseColor("#ff0000"));
                }
                else
                    bufferedDataTextView.setTextColor(Color.parseColor("#ffa500"));
            }
        });
    }


    public void EndTrip(View v)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Confirm");
        builder.setMessage("Are you sure?");
        builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, trip.this);
                new EndTrip().execute();
                dialog.dismiss();
            }
        });

        builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }

    public void CancelTrip(View v)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Confirm");
        builder.setMessage("Are you sure?");
        builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, trip.this);
                new CancelTrip().execute();
                dialog.dismiss();
            }
        });

        builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trip);

        locationUpdates = new ArrayList<LocationData>();

        tripID = getIntent().getStringExtra("TripID");

        sentDataTextView = (TextView) findViewById(R.id.TextView_SentData);
        bufferedDataTextView = (TextView) findViewById(R.id.TextView_BufferedData);

        ((TextView) findViewById(R.id.TextView_TripID)).setText(getIntent().getStringExtra("TripID"));
        ((TextView) findViewById(R.id.TextView_BusNo)).setText(getIntent().getStringExtra("BusNo"));
        ((TextView) findViewById(R.id.TextView_Driver)).setText(getIntent().getStringExtra("Driver"));
        ((TextView) findViewById(R.id.TextView_TripStartTime)).setText(getIntent().getStringExtra("TripStartTime"));
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this).addApi(LocationServices.API)
                .build();
        mGoogleApiClient.connect();
    }


    @Override
    public void onConnected(Bundle connectionHint) {
        LocationRequest mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(10000);
        mLocationRequest.setFastestInterval(10000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

        try {
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        }
        catch(SecurityException ex)
        {
        }
    }

    @Override
    public void onConnectionSuspended(int cause) {
        Log.d("Akshay", "Connection to Google API suspended");
    }

    @Override
    public void onLocationChanged(Location location) {

        LocationData locationData = new LocationData();
        locationData.TripID = tripID;
        locationData.LocationTime=new SimpleDateFormat("yyyyMMdd-HHmmss").format(Calendar.getInstance().getTime()).toString();
        locationData.Latitude=location.getLatitude();
        locationData.Longitude=location.getLongitude();

        new InsertTripDetails(locationData).execute();
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    }





    private class InsertTripDetails extends AsyncTask<String, Void, Void>
    {
        LocationData LD;
        InsertTripDetails(LocationData ld)
        {
            LD=ld;
        }
        @Override
        protected Void doInBackground(String... params)
        {
            locationUpdates.add(LD);
                for (int i=locationUpdates.size()-1;i>=0;i--)
                {
                    try {
                        SoapObject request = new SoapObject("http://tempuri.org/", "InsertTripDetail");

                        request.addProperty(MyUtils.CreateProp("TripID", tripID, String.class));
                        request.addProperty(MyUtils.CreateProp("LocationTime", new SimpleDateFormat("yyyyMMdd-HHmmss").format(Calendar.getInstance().getTime()).toString(), String.class));
                        request.addProperty(MyUtils.CreateProp("Latitude", LD.Latitude, String.class));
                        request.addProperty(MyUtils.CreateProp("Longitude", LD.Longitude, String.class));

                        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11) {{
                            dotNet = true;
                        }};
                        MarshalDouble md = new MarshalDouble();
                        md.register(envelope);

                        envelope.setOutputSoapObject(request);
                        HttpTransportSE androidHttpTransport = new HttpTransportSE("http://veda.online/default.asmx");

                        androidHttpTransport.call("http://tempuri.org/InsertTripDetail", envelope);
                        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

                        locationUpdates.remove(i);
                        sentData++;
                    }
                    catch (Exception ex) {
                        System.out.print(ex.getMessage());
                    }
                    finally {

                        SetSentBufferedData();
                    }
                }
            PlayAlertIfNeeded();
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

        }
    }


    private class EndTrip extends AsyncTask<String, Void, Void>
    {
        boolean isError=false;
        @Override
        protected Void doInBackground(String... params) {

            try {
                SoapObject request = new SoapObject("http://tempuri.org/", "UpdateTrip");

                request.addProperty(MyUtils.CreateProp("TripID",tripID,String.class));
                request.addProperty(MyUtils.CreateProp("TripEndTime",new SimpleDateFormat("yyyyMMdd-HHmmss").format(Calendar.getInstance().getTime()).toString(),String.class));
                request.addProperty(MyUtils.CreateProp("TripStatus","0",String.class));

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11) {{dotNet=true;}};
                envelope.setOutputSoapObject(request);
                HttpTransportSE androidHttpTransport = new HttpTransportSE("http://veda.online/default.asmx");

                androidHttpTransport.call("http://tempuri.org/UpdateTrip", envelope);
                SoapObject response = (SoapObject) envelope.getResponse();
            }
            catch (Exception e)
            {
                isError=true;
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

            if(isError)
                CreateOKDialog("Error ending trip");
            else {
                CreateOKDialog("Trip ended");
            }
        }
    }


    private class CancelTrip extends AsyncTask<String, Void, Void>
    {
        boolean isError=false;
        @Override
        protected Void doInBackground(String... params) {

            try {
                SoapObject request = new SoapObject("http://tempuri.org/", "UpdateTrip");

                request.addProperty(MyUtils.CreateProp("TripID",tripID,String.class));
                request.addProperty(MyUtils.CreateProp("TripEndTime",new SimpleDateFormat("yyyyMMdd-HHmmss").format(Calendar.getInstance().getTime()).toString(),String.class));
                request.addProperty(MyUtils.CreateProp("TripStatus","-1",String.class));

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11) {{dotNet=true;}};
                envelope.setOutputSoapObject(request);
                HttpTransportSE androidHttpTransport = new HttpTransportSE("http://veda.online/default.asmx");

                androidHttpTransport.call("http://tempuri.org/UpdateTrip", envelope);
                SoapObject response = (SoapObject) envelope.getResponse();
            }
            catch (Exception e)
            {
                isError=true;
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

            if(isError)
                CreateOKDialog("Error cancelling trip");
            else {
                CreateOKDialog("Trip cancelled");
            }
        }
    }
}

当初は座標を送信するためだけに使用すると思っていましたが、それを改善して、旅が行われたときにパスも表示したいと考えています。誰でも私が正しい方向を指すのを手伝ってくれますか?

4

0 に答える 0