【问题标题】:Android Tracking App, posting httpAndroid 跟踪应用,发布 http
【发布时间】:2011-11-16 12:05:20
【问题描述】:

主要思想是在后台跟踪朋友,每3分钟更新一次他们的位置,并在http上发送位置; 当我在模拟器上运行它时,它会让我强制关闭。 我想让你记住,我是 Android 的初学者,我坚持这个一个月半,但我不知道如何将 Android 服务与httpPost 连接。

public class Tracker extends Service {

    private LocationManager locManager;
    private LocationListener locListener;
    private TelephonyManager mTelephonyMgr;


    // creating and starting the service
    public void onCreate() {
        super.onCreate();
        startService();
        Toast.makeText(getApplicationContext(), "Service created", Toast.LENGTH_LONG);
    }
    /*public void onStart(){
        super.onStart(intent, startId)
        startService();
    }*/

    // shutting down the service
    public void onDestroy() {
        super.onDestroy();
        shutDownService();
        Toast.makeText(getApplicationContext(), "Service is destroyed", Toast.LENGTH_LONG);

    }

    public void shutDownService() {
        locManager.removeUpdates(locListener);
    }

    private final IBinder mBinder = new LocalBinder();

    public IBinder onBind(Intent intent) {

        return mBinder;
    }

    public class LocalBinder extends Binder {
        Tracker.getService() {
            return Tracker.this;
        }
    }

    public void startService() {

        locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locListener = new MyLocationListener();
        locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,
                0, locListener);
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                locListener);
        postData(null, null);   //  return START_STICKY;
    }

    public void postData(String currLat, String currLon) {

        String Text2 = "String is" + "Latitude = " + currLat + "Longitude = "
                + currLon;
        String Text3 = "Phone number is: " + getMyDigitsPhoneNumber();
        // showing implicit intent 
        Toast.makeText(getApplicationContext(), Text2, Toast.LENGTH_LONG); 
        Toast.makeText(getApplicationContext(), Text3, Toast.LENGTH_LONG);

        HttpPost httppost = new HttpPost(
                "http://87.88.0.178/firm/logger.php");
        // Add the data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
        nameValuePairs.add(new BasicNameValuePair("PhoneNumber", getMyDigitsPhoneNumber()));
        nameValuePairs.add(new BasicNameValuePair("Latitude", currLat));
        nameValuePairs.add(new BasicNameValuePair("Longitude", currLon));
        HttpClient client = new DefaultHttpClient();
        try {
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = client.execute(httppost);
            HttpEntity entity = response.getEntity();
            String responseText = EntityUtils.toString(entity);
            responseText = responseText.trim();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getMyPhoneNumber() {

        mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        return mTelephonyMgr.getLine1Number();
    }

    public String getMyDigitsPhoneNumber() {
        String phoneNum = getMyPhoneNumber();
        return phoneNum.substring(2);
    }



    // retrieving location
    public class MyLocationListener implements LocationListener {
        @Override
        public void onLocationChanged(Location loc) {
            if (loc != null)
                loc.getLatitude();
            loc.getLongitude();

            double lat = loc.getLatitude()*1E6;
            double lon = loc.getLongitude()*1E6;

            String curr_lat = Double.toString(lat);
            String curr_lon = Double.toString(lon);

            postData(curr_lat, curr_lon);

            String Text = "My current location is: " + "Latitud = "
                    + loc.getLatitude()*1E6 +

                    "Longitud = " + loc.getLongitude()*1E6;

            Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT);
        }

                            //took from android website
        private static final int THREE_MINUTES = 1000 * 60 * 3; // three minutes

        protected boolean isBetterLocation(Location loc,
                Location currentBestLocation) {
            if (currentBestLocation == null) {
                // A new location is always better than no location
                return true;
            }

            // Check whether the new location fix is newer or older
            long timeDelta = loc.getTime() - currentBestLocation.getTime();
            boolean isSignificantlyNewer = timeDelta > THREE_MINUTES;
            boolean isSignificantlyOlder = timeDelta < -THREE_MINUTES;
            boolean isNewer = timeDelta > 0;

            // If it's been more than two minutes since the current location,
            // use the new location
            // because the user has likely moved
            if (isSignificantlyNewer) {
                return true;
                // If the new location is more than two minutes older, it must
                // be worse
            } else if (isSignificantlyOlder) {
                return false;
            }

            // Check whether the new location fix is more or less accurate
            int accuracyDelta = (int) (loc.getAccuracy() - currentBestLocation
                    .getAccuracy());
            boolean isLessAccurate = accuracyDelta > 0;
            boolean isMoreAccurate = accuracyDelta < 0;
            boolean isSignificantlyLessAccurate = accuracyDelta > 200;

            // Check if the old and new location are from the same provider
            boolean isFromSameProvider = isSameProvider(loc.getProvider(),
                    currentBestLocation.getProvider());

            // Determine location quality using a combination of timeliness and
            // accuracy
            if (isMoreAccurate) {
                return true;
            } else if (isNewer && !isLessAccurate) {
                return true;
            } else if (isNewer && !isSignificantlyLessAccurate
                    && isFromSameProvider) {
                return true;
            }
            return false;
        }

        /** Checks whether two providers are the same */
        private boolean isSameProvider(String provider1, String provider2) {
            if (provider1 == null) {
                return provider2 == null;
            }
            return provider1.equals(provider2);
        }

        @Override
        public void onProviderDisabled(String provider) {
            // required for interface, not used
        }

        @Override
        public void onProviderEnabled(String provider) {
            // required for interface, not used
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // required for interface, not used
        }
    }
}

【问题讨论】:

  • 如果你有一个强制关闭对话框,你也有一个堆栈跟踪。请张贴。
  • 哪行代码导致强制关闭?
  • link => 这是我在android中上传的logcat链接

标签: android service location http-post


【解决方案1】:

上面写着ClassNotFoundExceptionFriendTracker。您发布的代码不包含类FriendTracker,但它确实有类Tracker。尝试在您的代码中搜索单词FriendTracker,并将所有这些替换为Tracker。 (也显示)您是否将 FriendTracker 重命名为 Tracker

编辑
(用上述答案修复的原始异常)

新的异常堆栈跟踪显示ClassCastExceptionSecurityException。服务必须在 AndroidManifest.xml 中声明,如:

<service android:enabled="true" android:name="com.yourcompany.FriendTracker" />

您的包裹似乎是com.android.tracker
我建议将其更改为 com.yourcompany.tracker

【讨论】:

  • 我已将所有内容更改为friendtracker,但仍有致命例外:main
  • 这是一个不同的例外吗? (显示堆栈跟踪,以便我们检查它)
  • 您是否在清单中的 activity 标记中声明了 FriendTracker?
  • 是的,我已经写了所有的权限,也声明了FriendTracker。 link
  • 您能否调试您的应用程序以查看执行是否进入onCreate()onDestroy()?您还可以发布启动服务的代码吗?
猜你喜欢
  • 2013-04-20
  • 1970-01-01
  • 1970-01-01
  • 2011-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-24
  • 2017-07-12
相关资源
最近更新 更多