Android把百度定位放到service里面

//百度定位的类继承service

public abstract class Location extends Service  implements BDLocationListener{

    private LocationClient locationClient = null;
    private static final int UPDATE_TIME = 5000;
    
   public void localtionGeographyRegion(Context context){
        locationClient = new LocationClient(context);
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 是否打开GPS
        option.setCoorType("bd09ll"); // 设置返回值的坐标类型。
        option.setPriority(LocationClientOption.NetWorkFirst); // 设置定位优先级
        option.setProdName("LocationDemo"); // 设置产品线名称。强烈建议您使用自定义的产品线名称,方便我们以后为您提供更高效准确的定位服务。
        option.setScanSpan(UPDATE_TIME); // 设置定时定位的时间间隔。单位毫秒
        locationClient.setLocOption(option);
        locationClient.registerLocationListener(this);
        locationClient.start();
        if (locationClient != null && locationClient.isStarted()) {
            locationClient.requestLocation();
        }
   }
   
   public void distory() {
        if (locationClient != null && locationClient.isStarted()) {
            locationClient.stop();
            locationClient = null;
        }
   }
   
    @Override
    public void onReceivePoi(BDLocation location) {
        
    }

}



//自己写的service继承百度的定位Location
public class MyService extends Location{
    public final static int SEND_FAIL=1;
    public Messenger cMessenger;//客服端的信使
//    public int count=0;

    @Override
    public IBinder onBind(Intent intent) {
        return myMessenger.getBinder();
    }
    
    Handler handler=new Handler(){
        public void handleMessage(android.os.Message msg) {
            cMessenger=msg.replyTo;
            startLocation(getApplicationContext());
            Toast.makeText(getApplicationContext(), "服务端接受到了东西", 0).show();
            
            //也可以给客服端发送消息
//           cMessenger=msg.replyTo;
//           Message message=Message.obtain(null,SEND_FAIL);
//           message.replyTo = myMessenger;
//           try {
//               cMessenger.send(message);
//           }catch (RemoteException e) {
//                e.printStackTrace();
//           }
        }


    };
    private Messenger myMessenger=new Messenger(handler);
    
    private void startLocation(Context context) {
        int flag=3;
        if(flag==3){
            
        }else{
            
        }
      this.localtionGeographyRegion(context);    
    }
    
    
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.distory();
    }
    
    @Override
    public void onReceiveLocation(BDLocation location) {
        if (location == null) {
            return;
        }
        if(location.getLocType()==161){//如果定位成功
            double longitude=location.getLongitude();
            double latitude=location.getLatitude();
            Toast.makeText(getApplicationContext(), "当前经度:"+longitude+"当前纬度:"+latitude, 0).show();
//            count++;
//            if(count==5){
            //给客服端发送消息
//                Message message=Message.obtain(null,SEND_FAIL);
//                try {
//                    cMessenger.send(message);
//                } catch (RemoteException e) {
//                    e.printStackTrace();
//                }
//            }
        }        
    }


//写了一个service管理类,继承Activity

public class ServiceManage extends Activity{
    private Messenger sMessenger;//服务端信使
    private Messenger myMessenger;//自己信使对象
    
    public Context context;
    
   public void bindService(){
       Intent intent=new Intent(context, MyService.class);
       boolean flag=context.bindService(intent, conn, Context.BIND_AUTO_CREATE);
       if(!flag){
            Toast.makeText(context,"绑定失败", Toast.LENGTH_LONG).show();
       }
   }
   
   public void unBindSrvice(){
       unbindService(conn);
   }
   
    private ServiceConnection conn=new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            sMessenger=null;
            myMessenger=null;;
        }
        
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            sMessenger=new Messenger(service);//进行连接的时候会返回一个服务端信使对象回来
            myMessenger=new Messenger(handler);//初始化自己的信使对象
            
            Message message=Message.obtain(null,null);
            message.replyTo = myMessenger;
            try {
                sMessenger.send(message);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    };
    
    private Handler handler=new Handler(){
        //接收服务端返回过来的消息
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            case MyService.SEND_FAIL:
                Toast.makeText(context, "连接成功",0).show();
                break;
            }
        };
    };

}


//继承ServiceManage类 这样方便调用

public class MainActivity extends ServiceManage {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.context=this;
        this.bindService();
    }
    
    public void unBind(View v){
        this.unBindSrvice();
    }
    
    public void bind(View v){
        this.bindService();
    }
}


//布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    
        <Button
        android:onClick="unBind"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="取消绑定"
        />
        
                <Button
        android:onClick="bind"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开启绑定"
        />
    

</LinearLayout>



//百度的一些权限

        <!-- 权限 -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
    <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
    
        <!-- 使用百度定位需要追加的一些权限 -->
    <uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <uses-permission android:name="android.permission.READ_LOGS"/>


                <!--    如果百度定位会报错,加上这个服务 -->
        <service   
            android:name="com.baidu.location.f"
            android:enabled="true"  
            android:process=":remote"   
            android:permission="android.permission.BAIDU_LOCATION_SERVICE">  
            <intent-filter>  
                <action android:name="com.baidu.location.service_v2.6"></action>  
            </intent-filter>  
        </service> 


我用的百度定位版本

jar包:locSDK_2.6.jar

so文件:liblocSDK_2.4.so





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值