获取/发送短信时如何获取 GPS 坐标? [复制]

Posted

技术标签:

【中文标题】获取/发送短信时如何获取 GPS 坐标? [复制]【英文标题】:How to get GPS coordinates when get/send SMS? [duplicate] 【发布时间】:2016-01-19 05:38:25 【问题描述】:

我正在制作短信记录器。我已经获得了数据、短信正文等信息。但我还想添加从我的设备发送短信的 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();
    

谢谢

【问题讨论】:

这篇文章够吗? ***.com/questions/1513485/… @AlbertoInet 谢谢。但主要问题是如何统一获取坐标和短信 不客气。您能否发布一些您的 SMS 记录器类的片段以尝试在其上实现 GPS? @AlbertoInet 添加一些代码 完成@IevgenMaikovets! 【参考方案1】:

我现在在这台 PC 上没有 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 权限

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

【讨论】:

以上是关于获取/发送短信时如何获取 GPS 坐标? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

获取 GPS 坐标并保存为文本文件

Iphone,如何在后台保存坐标gps? [复制]

Android:获取 GPS 坐标并将其发送到服务器的服务

如何在按钮单击时获取 GPS 坐标? (最简单的)

登录网站时如何从黑莓获取 GPS 坐标

如何通过 SMS 检索 GPS 位置