- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 android 中使用 GPS 获取坐标。我已成功在前台获取位置,但每当我将应用程序置于后台时,GPS 标志就会消失,并且它会在后台停止定位点。我使用了不同版本的 android,一些定位位置点(低于 8.0),有些则没有(Pie)。
如何在 Pie 中继续在后台运行 GPS。我尝试了不同的 SO 答案,但没有得到解决方案。
我正在使用服务在后台运行应用程序并使用广播接收器获取坐标。但我在 Pie 版本中没有得到积分。
Here's my MainActivity.java
public class MainActivity extends AppCompatActivity {
private LocationManager locationManager;
private LocationListener locationListener;
private double latitude, longitude;
BroadcastReceiver broadcastReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
/*inflating layout*/
riderFirstName = findViewById(R.id.rider_fName);
riderLastName = findViewById(R.id.rider_lName);
riderNote = findViewById(R.id.note);
logout = findViewById(R.id.logout);
if (!runtime_permissions()) {
Intent i = new Intent(getApplicationContext(), GPS_Service.class);
startService(i);
}
}
@Override
protected void onResume() {
super.onResume();
if (broadcastReceiver==null){
broadcastReceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
double lat = (double) intent.getExtras().get("latitude");
double lng= (double) intent.getExtras().get("longitude");
Log.i("lat", String.valueOf(lat));
Log.i("lang", String.valueOf(lng));
Log.i("lang", String.valueOf(RiderID));
}
};
}
registerReceiver(broadcastReceiver, new IntentFilter("location_update"));
}
@Override
protected void onDestroy() {
super.onDestroy();
if (broadcastReceiver!=null)
unregisterReceiver(broadcastReceiver);
}
private boolean runtime_permissions() {
if (Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 100);
return true;
}
return false;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 100) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
Intent i = new Intent(getApplicationContext(), GPS_Service.class);
startService(i);
} else {
runtime_permissions();
}
}
}
}
Here's my Service class
public class GPS_Service extends Service {
PowerManager.WakeLock wakeLock;
private LocationListener listener;
private LocationManager locationManager;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@SuppressLint("InvalidWakeLockTag")
@Override
public void onCreate() {
listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Intent i = new Intent("location_update");
i.putExtra("latitude", location.getLatitude());
i.putExtra("longitude", location.getLongitude());
sendBroadcast(i);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
};
locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
//noinspection MissingPermission
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, CommonObjects.DELAY_LOCATION, 0, listener);
}
@Override
public void onDestroy() {
super.onDestroy();
if(locationManager != null){
//noinspection MissingPermission
locationManager.removeUpdates(listener);
}
}
}
Here's Manifest
<!-- my permissions -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
最佳答案
通过使用 startForegroundService()
而不是 startService()
我得到了解决我的问题(在后台获取 GPS 位置)的奥利奥 (8.0) 及更高版本。
为了实现,我们必须在 Manifest 中添加一个 Foreground 权限,并在 Service 类中添加一个 startForeground();
方法,如下所示:
这是我更新的 MianActivity.java 代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
if (!runtime_permissions()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
Intent serviceIntent = new Intent(context, YourService.class);
ContextCompat.startForegroundService(context, serviceIntent );
}
else
{
context.startService(new Intent(context, YourService.class));
}
}
}
更新了我的服务类
@SuppressLint("InvalidWakeLockTag")
@Override
public void onCreate() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
startMyOwnForeground();
else
startForeground(1, new Notification());
...
}
private void startMyOwnForeground(){
String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp";
String channelName = "My Background Service";
NotificationChannel chan = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
chan.setLightColor(Color.BLUE);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
}
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
manager.createNotificationChannel(chan);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(true)
.setContentTitle("App is running in background")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(2, notification);
}
更新 list 许可
...
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
关于android - 如何解决 GPS 在后台 android Pie 中自动关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56413488/
我正在使用 Xcode 4.5,部署目标为 iOS 5.1 当我编译我的应用程序时,我收到以下警告,这些方法与两个大小显着增加的特定方法有关。 ld: warning: PIE disabled. A
我想使用 lief 将可执行文件转换为共享库,我注意到它只支持 pie 可执行文件。所以我想知道是否有一种方法可以将 no-pie 可执行文件转换为 pie 可执行文件。 最佳答案 不,没有,除非在链
我的 Android 项目应该生成原生可执行文件作为构建的一部分,并支持 API 14 (Android 4.0) 的所有设备。 问题是 4.0 仅支持非 pie 可执行文件,而 Android >
我有一个全屏cordova应用程序,我曾经使用下面的css作为iPhone X的缺口, padding-top: 25px; padding-top: env(safe-area-inset-top)
我指的是这个https://ecomfe.github.io/echarts/doc/example/pie1.html#-en例子。我无法隐藏饼图中的相邻标签。我已经圈出了我希望隐藏在附加图像中的标
这是我的代码: function graphDataAllChart(graphData) { $(".dataContentAllPie").empty(); nv.addGrap
这有效: perl -pi -e 's/abc/cba/g' hellofile 但这不会: perl -pie 's/cba/abc/g' hellofile 换句话说 -pi -e 有效但 -pi
在 amcharts v4 中,PIE 切片的颜色在 10 种左右的颜色后重复。就像他们有一系列颜色,他们只使用这些颜色并在完成后重复。我希望所有切片都具有独特的颜色,而无需手动创建颜色。如果可能需要
我在我的应用程序中有一个 15 秒计时器的要求,在圆形进度条动画中将在秒数的中心包含标签 下面的代码给出了从 0.0 到 1.0f 的 labelprogress,但我想将它映射到 15 秒倒计时的平
我有一个在夜间运行后台服务的应用程序。它由 alarm clock app 触发运行. 该应用整夜将手机外部 SD 卡上的数据上传到 Dropbox。它可以在以前的 Android 版本上无缝运行,但
我正在尝试从这里自定义一个饼图: https://canvasjs.com/jquery-charts/pie-chart-index-data-label-inside/ 我使用的代码如下,经过我的
我正在尝试使用 CSS PIE 显示圆 Angular ,但它不起作用。请看my website for reference .您会看到在 IE7 和 IE8 中,顶部的小登录区域没有圆 Angula
我正致力于将我们公司的 Web 应用程序带入 21 世纪,并尝试使用一些新的 CSS3 功能,如 border-radius 和 box-shadow 以获得更现代的视觉效果。然而,我们 90% 以上
有一个简单的例子:http://fsou1.ru/files/verstka_examples/14/index2.html 我在 IE 中使用 PIE.js 附加边框半径,但是当我在悬停事件上添加“
我有一个网站 here ... 在 Chrome/FF 中查看它,您会看到带有圆 Angular 的标签。 IE8 仍然显示方 Angular 。 这是我调用 PIE 的 CSS... .class-
渲染边框的 RGBA 颜色时遇到问题。边框半径的 RGBA 颜色工作正常但不是边框颜色,它不显示任何边框颜色。 CSSPie 中是否有单独的“-pie-”标签用于在边框中使用 RGBA? 我的代码:
我用 gcc -fpie test.c 编译了一个简单的 hello world c 代码,现在使用 objdump 查看二进制文件: Disassembly of section __TEXT,__
我正在使用 CSS3 pie 并在 head 标签之前通过附加的 js 文件调用它。 出于某种原因,我的背景图片出现了。我已经尝试了标准添加 z-index 和位置相对修复,但它没有显示。任何帮助指导
这是我检测和显示性别和年龄的循环。我试图在 matplotlib 饼图上绘制它,但每次它运行循环时,它都会为每个预测打开一个不同的图表。如何实时更新同一个图表。 这里我更新完整的代码 使用 Pytho
我用的是ie8,其他版本不知道。我到处都在使用派,它通常工作正常。但是,我所有的表单输入元素都有框阴影和边框半径,并且没有边框(几乎所有样式)。在 FF/Safari/Chrome 中一切都很好,但在
我是一名优秀的程序员,十分优秀!