|  | 
 
| 最近在编写一个基于Android 2.1 的手机应用程序,其中的一个功能是利用Google 的地图API接口实现足迹追踪,整个程序设计大概分为三个部分,UI设计、GoogleMapAPI接口调用以及后台Service所做的数据的采集和传输以及和服务器的通讯。Android的UI设计和JAVA、MFC、C#.NET有些不同,毕竟是手持设备,硬件资源的限制要求它用尽量轻便的代码框架去完成功能,Android的用户界面是用XML来进行布局和管理的,支持直接拖拽,但是效果并不是很好。它的主活动界面是在main.xml中编写的,在这里可以定义一些按钮啊、文本框什么的。GoogleMapAPI的用法网上有很多教程,首先要去申请一个KEY,然后才能去调用API得到想要的数据,我们这里要获得GPS的实时数据,所以要用到 LocationListeninger 和 LocationManager 两个类,绘制地图可以调用Mapview类,里面有很多操作地图的方法,类似放大缩小、拖拽描点等都可以调用函数直接实现。
 主要来说一下后台服务的编写吧,在Android中Service是用来进行后台数据处理的东西,类似于Linux下的后台进程,就是当前活动创建的Service在这个活动窗口退出后Service还是继续运行的,我们要对用户的行动进行跟踪,就要长时间采集GPS发送过来的地理位置信息,这样的东西写成一个Service再合适不过了。自己编写的Service要继承系统的Service类,然后Override其中的方法。
 要开启这个Service可以用复制代码package server.track;
import java.util.Calendar;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class Track extends Service {
        private static final String TAG = "Track";
        private LocationManager lm;
        private LocationListener locationListener;
        final SDRW filerecord = new SDRW();
        private String record ;
        private String length;
        private int headposition;
        @Override
        public IBinder onBind(Intent arg0) {
//                Log.d(TAG, "onBind.");
                return null;
        }
        
        public void onStart(Intent intent, int startId) {  
                Log.d(TAG, "onStart.");
                Toast.makeText(getApplicationContext(), "启动服务",Toast.LENGTH_SHORT).show();
        super.onStart(intent, startId);
//        startDb();
//                Bundle extras = intent.getExtras();
//                if (extras != null) {
//                        track_id = extras.getInt(LocateDbAdapter.TRACKID);
//                }
//                Log.d(TAG, "track_id =" + track_id);
                // ---use the LocationManager class to obtain GPS locations---
                Calendar calendar = Calendar.getInstance();
                headposition = 5;
                record = "head:                                                                                 \r\n";
                filerecord.write(record);
                record = "user:"+"Yastand\r\n"+"date:"+calendar.get(Calendar.YEAR) + "-" +calendar.get(Calendar.MONTH) + "-" +  calendar.get(Calendar.DAY_OF_MONTH) + " "
                + calendar.get(Calendar.HOUR_OF_DAY) + ":"
                + calendar.get(Calendar.MINUTE) + ":"  + calendar.get(Calendar.SECOND)+"\r\n";
                filerecord.write(record);
                length = String.valueOf(record.length())+" ";
                filerecord.writehead(length,headposition);
                headposition += length.length();
                lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                locationListener = new MyLocationListener();
                lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListener);
    }
        
        
    public void onDestroy() {
            Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
            super.onDestroy();
            lm.removeUpdates(locationListener);
//        stopService(new Intent("Context.LOCATION_SERVICE"));
            stopSelf();
    }
    protected class MyLocationListener implements LocationListener {
                @Override
                public void onLocationChanged(Location loc) {
                        Log.d(TAG, "MyLocationListener::onLocationChanged..");
/*                        if (loc != null) {
                                // //////////
                                if(mlcDbHelper == null){
                                        mlcDbHelper.open();
                                }
                                mlcDbHelper.createLocate(track_id,  loc.getLongitude(),loc.getLatitude(), loc.getAltitude());
                        }
                        */
                        record = "GPS:"+" lon:"+String.valueOf(loc.getLongitude())+" lat:"+String.valueOf(loc.getLatitude())+" alt:"+String.valueOf(loc.getAltitude())+"\r\n";
                        if (loc != null)
                        {
                        //        filepoint.write(edit1.getText().toString()); 
                                filerecord.write(record);
                        }
                }
                @Override
                public void onProviderDisabled(String provider) {
                        Toast.makeText(
                                        getBaseContext(),
                                        "ProviderDisabled.",
                                        Toast.LENGTH_SHORT).show();                }
                @Override
                public void onProviderEnabled(String provider) {
                        Toast.makeText(
                                        getBaseContext(),
                                        "ProviderEnabled,provider:"+provider,
                                        Toast.LENGTH_SHORT).show();                }
                @Override
                public void onStatusChanged(String provider, int status, Bundle extras) {
                        // TODO Auto-generated method stub
                }
        }
        
}
执行这句话后,Service类首先去找Create函数,再运行Onstart函数,一个Service就成功的运行了,但是退出当前程序这个操作对这个后台运行的Service并没有什么影响,那么怎样结束这个Service呢?可以这样复制代码Intent i = new Intent("server.track.START_TRACK_SERVICE");
                startService(i);
执行这句话,系统就会调用Service实例的onDestroy()函数,到这里本以为万事大吉了,结果发现调用stopService之后GPS发送回来的数据还是源源不断的写入到文件中,原来在onDestroy()函数中必须显示的把Service实例中调用的线程都结束之后才能停止,在我们的Service中调用的LocationLinstener是这问题的关键,必须在onDestroy()中结束它运行的线程复制代码stopService(new Intent("server.track.START_TRACK_SERVICE"));
ok了,调用这个函数之后后台运行的Service就自己终止了。复制代码 public void onDestroy() {
            Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
            super.onDestroy();
            lm.removeUpdates(locationListener);
//        stopService(new Intent("Context.LOCATION_SERVICE"));
            stopSelf();
    }
 | 
 |