首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android跟踪应用,发布http

Android跟踪应用,发布http
EN

Stack Overflow用户
提问于 2011-11-16 20:05:20
回答 1查看 615关注 0票数 1

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

代码语言:javascript
复制
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
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2011-11-16 21:39:48

上面写着FriendTracker类的ClassNotFoundException。您发布的代码不包含类FriendTracker,但它确实包含类Tracker。尝试在您的代码中搜索word FriendTracker,并将其全部替换为Tracker。(也是清单)您是否将FriendTracker重命名为Tracker

编辑

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

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

代码语言:javascript
复制
<service android:enabled="true" android:name="com.yourcompany.FriendTracker" />

您的包似乎是com.android.tracker

我建议将它改为类似于com.yourcompany.tracker的代码

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8151377

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档