从Android地图意图退出谷歌地图 [英] Exit from google maps intent in android

查看:75
本文介绍了从Android地图意图退出谷歌地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的android应用程序中实现了自定义的turn-by-turn导航。为了达到这个目的,我使用Intent.ACTION_VIEW作为动作并使用google.navigation:q作为uri字符串的意图,从我的MainActivity开始了该活动。谷歌地图导航页面已成功加载到我的应用程序中。



但是,我不知道如何从这个页面正常退出。如果我使用后退按钮,则需要4次后退按钮才能显示我的主要活动屏幕。是否有可能在本页面放置退出按钮。



我已经尝试过onActivityForResult和onBackPressed来销毁google maps屏幕。这些都不起作用。请提供进一步的建议。

解决方案

我知道我很晚回答这个问题,但也许可以帮助某人。 / p>

您无法从Google地图返回到您的活动/应用程序,因此您需要创建浮动视图/小部件(例如ola / uber)你经过适当的实施。这是我的实现。



首先,用户将从YourActivity映射应用程序。在这个活动中,我们将点击某个视图,询问SYSTEM_ALERT_WINDOW(Draw OVER,对于SDK> MarshMallow)的权限。然后,我们将启动Google地图以及由我们创建的服务来创建浮动图标。

  class YourActivity extends AppCompatActivity {

private GetFloatingIconClick mGetServiceClick;
public static boolean isFloatingIconServiceAlive = false;

onCreate(){
mGetServiceClick = new GetFloatingIconClick();

somebtn.onclick(){
askDrawOverPermission();


$ b private class GetFloatingIconClick extends BroadcastReceiver {
$ b $ @Override
public void onReceive(Context context,Intent intent){
Intent selfIntent = new Intent(YourActivity.this,YourActivity.class);
selfIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(selfIntent);



private void askDrawOverPermission(){
if(Build.VERSION.SDK_INT< Build.VERSION_CODES.M){
//如果操作系统是棉花糖,然后创建浮动图标,不需要许可
createFloatingBackButton();
} else {
if(!Settings.canDrawOverlays(this)){
//在设置中请求DRAW_OVER权限
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse(package:+ getApplicationContext()。getPackageName()));
startActivityForResult(intent,REQ_CODE_DRAW_OVER);
} else {
createFloatingBackButton();




//开始在地图上创建浮动图标的服务
private void createFloatingBackButton(){
Intent iconServiceIntent =新意图(YourActivity.this,FloatingOverMapIconService.class);
iconServiceIntent.putExtra(RIDE_ID,str_rideId);

Intent navigation = new Intent(Intent.ACTION_VIEW,Uri
.parse(google.navigation:q =+ lat_DEST +,+ lng_DEST +& mode = d ));
navigation.setPackage(com.google.android.apps.maps);
startActivityForResult(navigation,1234);

startService(iconServiceIntent);
}

@TargetApi(Build.VERSION_CODES.M)
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
if(requestCode == REQ_CODE_DRAW_OVER){
//作为来自Settings的权限不提供任何回调,因此再次检查权限
//以便我们可以绘制浮动而不要求用户点击先前点击的视图
//再次
if(Settings.canDrawOverlays(this)){
createFloatingBackButton();
} else {
//权限不是由用户提供的,执行您的任务
//GlobalVariables.alert(mContext,此权限对于此应用程序的功能是必需的);
}
} else if(requestCode == 1234){
//没有结果由google map返回,因为google不提供任何apis或文档
// for它。
} else {
super.onActivityResult(requestCode,resultCode,data);





服务类别: - p>

  public class FloatingOverMapIconService extends Service {
private WindowManager windowManager;
private FrameLayout frameLayout;
私人字符串str_ride_id;
public static final String BROADCAST_ACTION =com.yourpackage.YourActivity;

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

@Override
public void onCreate(){
super.onCreate();
createFloatingBackButton();

$ b @Override
public int onStartCommand(Intent intent,int flags,int startId){
//接收来自活动的任何数据
str_ride_id = intent.getStringExtra(RIDE_ID);
返回START_STICKY;
}

@Override
public void onDestroy(){
super.onDestroy();
windowManager.removeView(frameLayout);
}

private void createFloatingBackButton(){

CurrentJobDetail.isFloatingIconServiceAlive = true;

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);
params.gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;

windowManager =(WindowManager)getSystemService(WINDOW_SERVICE);
frameLayout = new FrameLayout(this);

LayoutInflater layoutInflater =(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);

//这里是您可以在框架布局中注入任何布局的位置
layoutInflater.inflate(R.layout.custom_start_ride_back_button_over_map,frameLayout);

ImageView backOnMap =(ImageView)frameLayout.findViewById(R.id.custom_drawover_back_button);
backOnMap.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent intent = new Intent(BROADCAST_ACTION);
intent.putExtra(RIDE_ID,str_ride_id);
sendBroadcast(intent);

//停止服务
FloatingOverMapIconService.this.stopSelf();
CurrentJobDetail .isFloatingIconServiceAlive = false;
}
});

windowManager.addView(frameLayout,params);


$ / code $ / pre
$ b $浮动图标Xml:$ / b

 < LinearLayout 
android:layout_width =wrap_content
android:layout_height =wrap_content
android:orientation = 垂直 >

< ImageView
android:id =@ + id / custom_drawover_back_button
android:layout_width =70dp
android:layout_height =100dp
android:src =@ drawable / common_full_open_on_phone
android:scaleType =center
android:background =@ color / colorAccent/>
< / LinearLayout>

清单文件: -

 < uses-permission android:name =android.permission.SYSTEM_ALERT_WINDOW/> 

< activity
android:name =。Activities.YourActivity
android:launchMode =singleTop/>

< service
android:name =。Utils.FloatingOverMapIconService
android:exported =false/>


I am implementing custom turn-by-turn navigation in my android application. To achieve this, I have started the activity from my MainActivity using an intent which uses Intent.ACTION_VIEW as action and "google.navigation:q" as uri string.The google maps navigation page is successfully loaded in my app.

But, I don't know how to gracefully exit from this page. If I use back button press, it takes 4 back button clicks to display my main activity screen. Is there any possibility to place "exit" button in this page.

I have tried "onActivityForResult" and "onBackPressed" for destroying the google maps screens. None of this works. Please provide some suggestions to go further.

解决方案

I know I am pretty late to answer this but maybe it can help someone.

You cannot come back from google map to your activity/app on single back press for this you need to create a floating view/widget like ola/uber which will do this for you after proper implementation. Here is my implementation.

First the user will go to map app from YourActivity. In this activity we will ask the permission for SYSTEM_ALERT_WINDOW (DRAW OVER, for SDK > MarshMallow) on click of some view. Then we will launch google map as well as a Service created by us to create a floating icon.

class YourActivity extends AppCompatActivity{

private GetFloatingIconClick mGetServiceClick;
public static boolean isFloatingIconServiceAlive = false;

onCreate(){
    mGetServiceClick = new GetFloatingIconClick();

    somebtn.onclick(){
        askDrawOverPermission();
    }
}

private class GetFloatingIconClick extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent selfIntent = new Intent(YourActivity.this, YourActivity.class);
        selfIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP
                | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(selfIntent);
    }
}

private void askDrawOverPermission() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        // if OS is pre-marshmallow then create the floating icon, no permission is needed
        createFloatingBackButton();
    } else {
        if (!Settings.canDrawOverlays(this)) {
            // asking for DRAW_OVER permission in settings
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + getApplicationContext().getPackageName()));
            startActivityForResult(intent, REQ_CODE_DRAW_OVER);
        } else {
            createFloatingBackButton();
        }
    }
}

// starting service for creating a floating icon over map
private void createFloatingBackButton() {
    Intent iconServiceIntent = new Intent(YourActivity.this, FloatingOverMapIconService.class);
    iconServiceIntent.putExtra("RIDE_ID", str_rideId);

    Intent navigation = new Intent(Intent.ACTION_VIEW, Uri
            .parse("google.navigation:q=" + lat_DEST + "," + lng_DEST + "&mode=d"));
    navigation.setPackage("com.google.android.apps.maps");
    startActivityForResult(navigation, 1234);

    startService(iconServiceIntent);
}

@TargetApi(Build.VERSION_CODES.M)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQ_CODE_DRAW_OVER) {
        // as permissions from Settings don't provide any callbacks, hence checking again for the permission
        // so that we can draw our floating without asking user to click on the previously clicked view
        // again
        if (Settings.canDrawOverlays(this)) {
            createFloatingBackButton();
        } else {
            //permission is not provided by user, do your task
            //GlobalVariables.alert(mContext, "This permission is necessary for this application's functioning");
        }
    } else if (requestCode == 1234) {
        // no result is returned by google map, as google don't provide any apis or documentation
        // for it.
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
}

Service Class:-

public class FloatingOverMapIconService extends Service {
private WindowManager windowManager;
private FrameLayout frameLayout;
private String str_ride_id;
public static final String BROADCAST_ACTION = "com.yourpackage.YourActivity";

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

@Override
public void onCreate() {
    super.onCreate();
    createFloatingBackButton();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // to receive any data from activity
    str_ride_id = intent.getStringExtra("RIDE_ID");
    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
    windowManager.removeView(frameLayout);
}

private void createFloatingBackButton() {

    CurrentJobDetail.isFloatingIconServiceAlive = true;

    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);
    params.gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    frameLayout = new FrameLayout(this);

    LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

    // Here is the place where you can inject whatever layout you want in the frame layout
    layoutInflater.inflate(R.layout.custom_start_ride_back_button_over_map, frameLayout);

    ImageView backOnMap = (ImageView) frameLayout.findViewById(R.id.custom_drawover_back_button);
    backOnMap.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(BROADCAST_ACTION);
            intent.putExtra("RIDE_ID", str_ride_id);
            sendBroadcast(intent);

            //stopping the service
            FloatingOverMapIconService.this.stopSelf();
            CurrentJobDetail.isFloatingIconServiceAlive = false;
        }
    });

    windowManager.addView(frameLayout, params);
}
}

Floating Icon Xml:-

<LinearLayout 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<ImageView
    android:id="@+id/custom_drawover_back_button"
    android:layout_width="70dp"
    android:layout_height="100dp"
    android:src="@drawable/common_full_open_on_phone"
    android:scaleType="center"
    android:background="@color/colorAccent"/>
</LinearLayout>

Manifest file :-

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

<activity
        android:name=".Activities.YourActivity"
        android:launchMode="singleTop" />

<service
        android:name=".Utils.FloatingOverMapIconService"
        android:exported="false" />

这篇关于从Android地图意图退出谷歌地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆