android - 启动谷歌地图 Intent 获取方向需要按 3 次后退按钮才能返回到我的应用程序

标签 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);

但是,是否有更好的方式启动开始导航 Activity,以便在用户返回到我的应用后点击后退按钮?

最佳答案

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

服务:

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();
                }
            }

关于android - 启动谷歌地图 Intent 获取方向需要按 3 次后退按钮才能返回到我的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22200982/

相关文章:

Android Google map v2 删除自定义图 block 上的标签

Android:Google Map v2 key :不适用于不同的系统

android - fragment 中的数据处理

android - 清除android应用程序用户数据

android - 广播BroadcastRecord超时

java - Android:如何在显示软键盘的同时显示 Activity 的上下文菜单

Android - 使用 DOM 解析器的 KML 解析器

android - 旋转的 OpenStreetMap View - 如何在 Android 中旋转后沿手指移动方向滑动 map ?

java - 检查 fragment 是否存在并重用它