首页 > 解决方案 > 显示服务和特定视图

问题描述

我只需要显示一个浮动图标以及已启动的地图活动,并在地图活动消失时停止它。(当用户从​​地图导航回我的应用程序时)地图上的浮动服务

 mIntent = new Intent(MainActivity.this, FloatingIconService.class);
public void startTrip(String s , String d)
{
    Uri gmIntentUri = Uri.parse("https://www.google.co.in/maps/dir/" + s + "/" + d);
    Intent mapIntent = new Intent(Intent.ACTION_VIEW,gmIntentUri);
    mapIntent.setPackage("com.google.android.apps.maps");
    mapIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivityForResult(mapIntent,4000);
    //view floating notes icon
    startService(mIntent);
}

onPause 是保持服务与地图一起运行所必需的,并且 onActivityResult 不符合我的要求

@Override
protected void onPause() {
    super.onPause();
    // To prevent starting the service if the required permission is NOT granted.
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || Settings.canDrawOverlays(this)) {
        startService(new Intent(MainActivity.this, FloatingIconService.class).putExtra("activity_background", true));
        finish();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == DRAW_OVER_OTHER_APP_PERMISSION) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(this)) {
                Toast.makeText(this, "Premission not granted", Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    } else if (requestCode==4000 && (resultCode == Activity.RESULT_CANCELED || resultCode == Activity.RESULT_OK)){
        stopService(mIntent);
    }else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}


fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startTrip("USA", "Alexandria");
            }
        });

标签: androidservice

解决方案


推荐阅读