获取/发送短信时如何获取GPS坐标?

时间:2015-10-20 13:57:58

标签: java android logging gps sms

我制作短信记录器。我已经获得了数据,短信等信息。但我还想添加从我的设备发送短信的GPS坐标,以及收到的短信。

我如何获得短信息:

public List<String> getInboundSMSCaptured() {
    EnterpriseDeviceManager edm = (EnterpriseDeviceManager) getSystemService(EnterpriseDeviceManager.ENTERPRISE_POLICY_SERVICE);
    DeviceInventory deviceInventoryPolicy = edm.getDeviceInventory();

    List<String> list = new ArrayList<String>();
    List<String> outlist = new ArrayList<String>();

    try {
        deviceInventoryPolicy.enableSMSCapture(true);
        // The device has likely logged some SMS messages at some point
        // after
        // enabling the policy.
        list = deviceInventoryPolicy.getInboundSMSCaptured();

        String separator = ";";

        TelephonyManager telemamanger = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        // String getSimSerialNumber = telemamanger.getSimSerialNumber();  //serial
        // String imei = telemamanger.getDeviceId(); // imei
        String OperatorName = telemamanger.getSimOperatorName(); // operator

        for (String log : list) {
            String character = "$";
            String newlog = log + character;
            newlog = newlog.replace("\n", "").replace("\r", "");
            outlist.add(newData(getPieceOfStr("TimeStamp:", " - ", log))
                    + separator + "In" + separator
                    + getPieceOfStr("From:", " - ", log) + separator
                    + OperatorName + separator + "\ufeff"
                    + getPieceOfStr("Body:", LastElement(newlog), newlog)
                    + separator);
        }

    } catch (SecurityException e) {
        Log.w(TAG, "SecurityException: " + e);
    }
    return outlist;
}

然后我在设备上创建CSV文件:

public void writeAllSMSs(List<String> InSMSs, List<String> OutSMSs) {

    List<String> AllSMSs_list = new ArrayList<String>();

    InSMSs = getInboundSMSCaptured();
    OutSMSs = getOutboundSMSCaptured();

    TelephonyManager telemamanger = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    String imei = telemamanger.getDeviceId();

    // separator for excel
    String separator = ";";

    AllSMSs_list = InSMSs;

    AllSMSs_list.addAll(OutSMSs);

    // sort from old to new calls
    Collections.sort(AllSMSs_list);

    // add info on top of file
    AllSMSs_list.add(0, "Time" + separator + "Status" + separator
            + "SMS Number" + separator + "Operator" + separator + "Body"
            + separator + "Latitude" + separator + "Longitude");

    try {
        FileOutputStream fileout = openFileOutput("SMS's.csv",
                MODE_MULTI_PROCESS);
        OutputStreamWriter outputWriter = new OutputStreamWriter(fileout);

        for (int i = 0; i < AllSMSs_list.size(); i++) {
            outputWriter.write(AllSMSs_list.get(i) + "\n");
        }

        Collections.reverse(AllSMSs_list);

        outputWriter.write("\n\nFile created: " + currentData());

        outputWriter.flush();
        outputWriter.close();

        // display file saved message
        Toast.makeText(getBaseContext(), "File saved successfully!",
                Toast.LENGTH_SHORT).show();

        copyFile("/data/data/com.example.samsungmdm/files/SMS's.csv",
        // for android /0/ for knox /100/
                "/storage/emulated/0/KNOX_Logs/"
                // "/storage/emulated/100/KNOX_Logs/"
                        + imei + " SMS's.csv");

    } catch (Exception e) {
        e.printStackTrace();
    }
}

由于

1 个答案:

答案 0 :(得分:2)

我现在没有Android Studio,所以对于任何语法错误都很抱歉。在您创建的课程中,您必须添加

public classs yourclass {
    public void writeAllSMSs(List<String> InSMSs, List<String> OutSMSs) {

        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener locationListener = new GPS();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

        ...
        //Now you can use 
        GPS gps = new GPS();
        //And obtain
        gps.getCurrentLatitude();
        gps.getCurrentLongitude();
        //Where you need put coordinates
    }
}

并创建一个新类,例如GPS,它实现了LocationListener

private class GPS implements LocationListener {
    private static location = new LatLng();

    public double getCurrentLatitude(){
        return location.latitude;
    }

    public double getCurrentLongitude(){
        return location.longitude;
    }

    @Override
    public void onLocationChanged(Location _location) {
        location.latitude = _location.getLatitude();
        location.longitude = _location.getLongitude();       
    }

    @Override
    public void onProviderDisabled(String provider) {}

    @Override
    public void onProviderEnabled(String provider) {}

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}
}

不要忘记Android Manifest PERMISSION

< uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />