首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将android webview保持在后台(使用位置)

如何将android webview保持在后台(使用位置)
EN

Stack Overflow用户
提问于 2021-09-05 02:00:03
回答 1查看 223关注 0票数 2

我有一个在Android Studio的webview里面的PWA,它采取用户的位置,只要用户有互联网,gps和应用程序在前台。

我的问题是当他们最小化或关闭应用程序时。我需要继续收集用户的位置。

有没有办法让我的网页视图允许我的PWA在后台或应用程序关闭时继续收集位置?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-16 20:41:10

您应该使用带有部分唤醒锁定的前台服务,以防止电话休眠。下面是一个实现Wakelock的前台服务类的例子:

代码语言:javascript
运行
复制
public class ICService extends Service {
    
    private static final int ID = 1;                        // The id of the notification
    
    private NotificationCompat.Builder builder;
    private NotificationManager mNotificationManager;
    private PowerManager.WakeLock wakeLock;                 // PARTIAL_WAKELOCK
    
    /**
     * Returns the instance of the service
     */
    public class LocalBinder extends Binder {
        public ICService getServiceInstance(){
            return ICService.this;
        }
    }
    private final IBinder mBinder = new LocalBinder();      // IBinder
    
    @Override
    public void onCreate() {
        super.onCreate();
        // PARTIAL_WAKELOCK
        PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"INSERT_YOUR_APP_NAME:wakelock");
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        startForeground(ID, getNotification());
        return START_NOT_STICKY;
    }
    
    @SuppressLint("WakelockTimeout")
    @Override
    public IBinder onBind(Intent intent) {
        if (wakeLock != null && !wakeLock.isHeld()) {
            wakeLock.acquire();
        }
        return mBinder;
    }
    
    @Override
    public void onDestroy() {
        // PARTIAL_WAKELOCK
        if (wakeLock != null && wakeLock.isHeld()) {
            wakeLock.release();
        }
        super.onDestroy();
    }

    private Notification getNotification() {
        final String CHANNEL_ID = "YOUR_SERVICE_CHANNEL";

        builder = new NotificationCompat.Builder(this, CHANNEL_ID);
        //builder.setSmallIcon(R.drawable.ic_notification_24dp)
        builder.setSmallIcon(R.mipmap.YOUR_RESOURCE_ICON)
            .setColor(getResources().getColor(R.color.colorPrimaryLight))
            .setContentTitle(getString(R.string.app_name))
            .setShowWhen(false)
            .setPriority(NotificationCompat.PRIORITY_LOW)
            .setCategory(NotificationCompat.CATEGORY_SERVICE)
            .setOngoing(true)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setContentText(composeContentText());

        final Intent startIntent = new Intent(getApplicationContext(), ICActivity.class);
        startIntent.setAction(Intent.ACTION_MAIN);
        startIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        startIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 1, startIntent, 0);
        builder.setContentIntent(contentIntent);
        return builder.build();
    }
}

显然,你仍然需要为你的应用程序禁用电池优化。您可以浏览此服务here的实际工作示例。

别忘了在你的AndroidManifest.xml中声明你的服务类型为"location“,以便在后台运行时,在短暂的信号丢失后,该服务也能接收到GPS更新:

代码语言:javascript
运行
复制
<!-- Recommended for Android 9 (API level 28) and lower. -->
<!-- Required for Android 10 (API level 29) and higher. -->
<service
    android:name="MyGPSService"
    android:foregroundServiceType="location" ... >
    <!-- Any inner elements would go here. -->
</service>

需要注意的是,一旦在前台访问GPS,就不需要请求android.permission.ACCESS_BACKGROUND_LOCATION许可来继续在后台接收位置:android.permission.ACCESS_FINE_LOCATION足以满足您的使用。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69060102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档