android-工厂设计模式

1.根据广播获取经纬度 进行高德地图导航

2.定义接口  实现定位的url 和经纬度

public interface INavigationInterface {

public boolean NavigationTo(String url);

public String packageUrl(double lat,double lon,int type,String poi);

}

3.根据广播获取经纬度 工厂分配给工人生产:

Navigation mNavi ;

@Override

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

mNavi=new Navigation(context);

if (action.equals("com.glsx.bootup.send.autonavi")){

Log.i(TAG, "action" + intent.getAction());

// 根据该目的地信息、经纬度,打开后视镜上的地图应用,直接进入导航功能界面,发起导航。

if (intent.getStringExtra("DestPoint") != "") {

String DestPoint = intent.getStringExtra("DestPoint");

String[] str = null;

str = convertStrArray(DestPoint);

latitude = str[1];// 经度值

longitude = str[0];// 纬度值

double lat =  Double.parseDouble(latitude);

double lon = Double.parseDouble(longitude);

Log.i(TAG,"lat"+lat);

Log.i(TAG,"lon"+lon);

Log.i(TAG,"nihafo");

mNavi.NavigationTo(mNavi.packageUri(lat,lon, 2, ""));

}

}

4.相关的工厂代码:

public class Navigation {

/** 速度�?��*/

public final int NAVI_TYPE_FASTER = 0;

/** 费用�?��*/

public final int NAVI_TYPE_LOWCOAST = 0;

/** 不走高�?*/

public final int NAVI_TYPE_NOT_HIGHTROAD = 0;

private static INavigationInterface navi = null;

public static INavigationInterface getNavi(){

return navi;

}

public Navigation(Context context){

navi = new AMapAutoNavi(context);

}

public boolean NavigationTo(String url){

if(navi == null){

return false;

}

return navi.NavigationTo(url);

}

public String packageUri(double lat, double lon, int type, String poi){

if(navi == null){

return null;

}

return navi.packageUrl(lat, lon, type, poi);

}

}

5.各工人生成产品代码:

public class AMapAutoNavi implements  INavigationInterface{

Context mContext = null;

AMapAutoNavi(Context context){

mContext = context;

}

@Override

public boolean NavigationTo(String url) {

// TODO Auto-generated method stub

try{

Intent i = new Intent(Intent.ACTION_VIEW, android.net.Uri.parse(url));

i.setPackage("com.autonavi.amapauto");

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

mContext.startActivity(i);

}catch(Exception e){

e.printStackTrace();

return false;

}

return true;

}

/**

* 车机版高德地图导航http://lbs.amap.com/api/amapauto/#id008

* cat=android.intent.category.DEFAULT

dat=androidauto://navi?sourceApplication=appname&poiname=fangheng&lat=36.547901&lon=104.258354&dev=1&style=2

pkg=com.autonavi.amapauto

*/

@Override

public String packageUrl(double lat, double lon, int type, String poi) {

// TODO Auto-generated method stub

String uri = "";

if(poi == null||poi == ""||"".equals(poi)){

uri = "androidauto://navi?sourceApplication=Vehicle&poiname=&lat=" + lat + "&lon="

+ lon + "&dev=1&style=2";

}else{

uri = "androidauto://navi?sourceApplication=Vehicle&poiname="+poi+"&lat=" + lat + "&lon="

+ lon + "&dev=1&style=2";

}

return uri;

}

你可能感兴趣的:(android-工厂设计模式)