android - 显示服务和特定视图
问题描述
我只需要显示一个浮动图标以及已启动的地图活动,并在地图活动消失时停止它。(当用户从地图导航回我的应用程序时)地图上的浮动服务
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");
}
});
解决方案
推荐阅读
- if-statement - 在 def 中编写分段函数
- c# - 如何使用触控作为 InkCanvas 的输入?
- javascript - 我想检查我的 id 或 class 是否存在于表中 | JavaScript
- c++ - IO 服务重启后,Boost 截止时间计时器持续触发
- php - @IsGranted 对主题属性的注释
- javascript - 重定向预填充链接
- odoo - 如何保护 Odoo 默认管理员帐户?
- twitter-bootstrap - 在 Bootstrap 4 中重新定位导航栏的元素
- mysql - 更新具有随机唯一数字的 SQL 表
- rust - 如何在结构中的数组块上实现迭代器?