退出谷歌地图意图在android中

Bha*_*ivi 5 android google-maps

我正在我的Android应用程序中实现自定义的转弯导航.为了实现这一点,我已经使用一个使用Intent.ACTION_VIEW作为动作的意图和"google.navigation:q"作为uri字符串从我的MainActivity启动了活动.谷歌地图导航页面已成功加载到我的应用程序中.

但是,我不知道如何优雅地退出此页面.如果我使用后退按钮,则需要4次后退按钮才能显示我的主活动屏幕.是否有可能在此页面中放置"退出"按钮.

我试过"onActivityForResult"和"onBackPressed"来破坏谷歌地图屏幕.这些都不起作用.请提供一些进一步的建议.

beg*_*ner 9

我知道我很晚才回答这个问题,但也许它可以帮助别人.

你不能在谷歌地图上回到你的活动/应用程序,因为你需要创建一个像ola/uber这样的浮动视图/小部件,它将在适当的实施后为你完成.这是我的实施.

首先,用户将从YourActivity转到应用程序地图.在此活动中,我们将在单击某个视图时询问SYSTEM_ALERT_WINDOW(DRAW OVER,用于SDK> MarshMallow)的权限.然后我们将启动谷歌地图以及我们创建的服务来创建浮动图标.

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);
    }
}
}
Run Code Online (Sandbox Code Playgroud)

服务类别: -

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);
}
}
Run Code Online (Sandbox Code Playgroud)

浮动图标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>
Run Code Online (Sandbox Code Playgroud)

清单文件: -

<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" />
Run Code Online (Sandbox Code Playgroud)

  • 天才.奇迹般有效.使用上面的代码,实现广播和接收,您可以终止导航意图并返回到您的活动. (2认同)