美文网首页
Android7.0以上系统定位信息在锁频黑屏下获取及上传

Android7.0以上系统定位信息在锁频黑屏下获取及上传

作者: 宝马奔驰_xyz | 来源:发表于2018-09-26 15:32 被阅读131次

    需求:项目需要实时采集用户的经纬度信息并上传后台

    实现及测试结果: 集成了高德地图获取经纬度,在7.0 8.0系统及锁屏,黑屏时部分手机无法获取定位信息,

    解决方案如下:

    1. 注册锁屏广播

    /*****

        * 注册锁频广播

        */

        public void initLockScreenReceiver() {

            //注册锁屏广播,主要是解决高德在锁屏黑屏情况下定位不更新的问题

            IntentFilter intentfilter = new IntentFilter();

            intentfilter.addAction(Intent.ACTION_SCREEN_ON);

            intentfilter.addAction(Intent.ACTION_SCREEN_OFF);

            intentfilter.addAction(Intent.ACTION_USER_PRESENT);

            mreceiver = new Mreceiver();

            registerReceiver(mreceiver, intentfilter);

        }

    2. 开启定时唤醒功能

    IntentFilterintentFile =new IntentFilter();

    intentFile.addAction("repeating");

    locationReceiver =new LocationReceiver();

    registerReceiver(locationReceiver, intentFile);

    //写一个定时的Pendingintent

    Intentintent =new Intent();

    intent.setAction("repeating");

    alarmPi = PendingIntent.getBroadcast(this, 0, intent, 0);

    alarm = (AlarmManager) getSystemService(ALARM_SERVICE);

    3. Android8.0系统上开启后台定位功能

    @SuppressLint("NewApi")

    private NotificationbuildNotification() {

    Notification.Builderbuilder =null;

            Notificationnotification =null;

            if (android.os.Build.VERSION.SDK_INT >=26) {

    //Android O上对Notification进行了修改,如果设置的targetSDKVersion>=26建议使用此种方式创建通知栏

                if (null ==notificationManager) {

    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

                }

    StringchannelId = getPackageName();

                if (!isCreateChannel) {

    NotificationChannelnotificationChannel =new NotificationChannel(channelId,

                            NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);

                    notificationChannel.enableLights(true);//是否在桌面icon右上角展示小圆点

                    notificationChannel.setLightColor(Color.BLUE); //小圆点颜色

                    notificationChannel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知

                    notificationManager.createNotificationChannel(notificationChannel);

                    isCreateChannel =true;

                }

    builder =new Notification.Builder(getApplicationContext(), channelId);

            }else {

    builder =new Notification.Builder(getApplicationContext());

            }

    builder.setSmallIcon(R.drawable.ic_launcher)

    .setContentTitle(Utils.getAppName(this))

    .setContentText("正在后台运行")

    .setWhen(System.currentTimeMillis());

            if (android.os.Build.VERSION.SDK_INT >=16) {

    notification =builder.build();

            }else {

    return builder.getNotification();

            }

    //        //启动定位上传

    //        if(null != locationClient){

    //            startLocation();

    //        }

    //initReceiver();

            wake();

            return notification;

        }

    4. 开启设备锁不休眠并在AndroidManifest.xml种申请权限

        PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);

            wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "mywakrlock");

            wl.setReferenceCounted(false);

    权限: <uses-permission android:name="android.permission.WAKE_LOCK"/>

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

    5. 由于部分厂商做了低功耗处理,需要用户进入手机设置页面->手动打开锁屏清理应用,找到对应的app设置为不清理即可

    相关文章

      网友评论

          本文标题:Android7.0以上系统定位信息在锁频黑屏下获取及上传

          本文链接:https://www.haomeiwen.com/subject/npofpxtx.html