启动Google地图意向指示需要按3后退按钮才能返回我的应用

时间:2014-03-05 14:44:43

标签: android android-maps

我正在从我的应用中启动谷歌地图,如此

String link = "https://maps.google.com/maps?saddr="+sLat+","+sLong+"&daddr="+dLat+","+dLong+"&sensor=true";
Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(link));
myIntent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
main.startActivity(myIntent);

但是,有没有更好的方法启动到开始导航活动,以便用户在返回我的应用程序后点击后退按钮?

1 个答案:

答案 0 :(得分:0)

你有很多这个问题,但我会告诉你答案。您可以实现一个聊天头来创建一个可以返回上一个活动的图标。这是解决方案:

服务:

public class ChadHeadService extends Service {
private WindowManager mWindowManager;
private View mChatHeadView;

public ChadHeadService() {
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    //Inflate the chat head layout we created
    mChatHeadView = LayoutInflater.from(this).inflate(R.layout.layout_chat_head, null);

    //Add the view to the window.
    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

    //Specify the chat head position
    params.gravity = Gravity.TOP | Gravity.LEFT;        //Initially view will be added to top-left corner
    params.x = 0;
    params.y = 400;

    //Add the view to the window
    mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    mWindowManager.addView(mChatHeadView, params);

    //Set the close button.
    /*ImageView closeButton = (ImageView) mChatHeadView.findViewById(R.id.close_btn);
    closeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //close the service and remove the chat head from the window
            stopSelf();
        }
    });*/

    //Drag and move chat head using user's touch action.
    final ImageView chatHeadImage = (ImageView) mChatHeadView.findViewById(R.id.chat_head_profile_iv);
    chatHeadImage.setOnTouchListener(new View.OnTouchListener() {
        private int lastAction;
        private int initialX;
        private int initialY;
        private float initialTouchX;
        private float initialTouchY;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:

                    //remember the initial position.
                    initialX = params.x;
                    initialY = params.y;

                    //get the touch location
                    initialTouchX = event.getRawX();
                    initialTouchY = event.getRawY();

                    lastAction = event.getAction();
                    return true;
                case MotionEvent.ACTION_UP:
                    //As we implemented on touch listener with ACTION_MOVE,
                    //we have to check if the previous action was ACTION_DOWN
                    //to identify if the user clicked the view or not.
                    if (lastAction == MotionEvent.ACTION_DOWN) {
                        //Open the chat conversation click.
                        Intent intent = new Intent(ChadHeadService.this, "ACTIVYTY RETURN");
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);

                        //close the service and remove the chat heads
                        stopSelf();
                    }
                    lastAction = event.getAction();
                    return true;
                case MotionEvent.ACTION_MOVE:
                    //Calculate the X and Y coordinates of the view.
                    params.x = initialX + (int) (event.getRawX() - initialTouchX);
                    params.y = initialY + (int) (event.getRawY() - initialTouchY);

                    //Update the layout with new X & Y coordinate
                    mWindowManager.updateViewLayout(mChatHeadView, params);
                    lastAction = event.getAction();
                    return true;
            }
            return false;
        }
    });
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (mChatHeadView != null) mWindowManager.removeView(mChatHeadView);
}

}

启动应用

Uri gmmIntentUri = Uri.parse("google.navigation:q="+adrees+"&mode=d");
            Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
            mapIntent.setPackage("com.google.android.apps.maps");
            try {
                context.startActivity(mapIntent);
                context.startService(new Intent(context, ChadHeadService.class));
            }
            catch (ActivityNotFoundException ex){
                try {
                    context.startActivity(mapIntent);
                    context.startService(new Intent(context, ChadHeadService.class));
                }
                catch (ActivityNotFoundException e){
                    Toast.makeText(context,"Application not installed",Toast.LENGTH_LONG).show();
                }
            }