gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:05:40 26 4
gpt4 key购买 nike

我像这样从我的应用程序启动谷歌地图

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com