【问题标题】:Expo dev tool: Running Dev app in the backgroundExpo开发工具:在后台运行开发应用程序
【发布时间】:2019-06-05 22:33:37
【问题描述】:

这是我第一次在 Expo 中开发,我正在构建应用程序来跟踪位置并使用 node.js 每 5 秒将数据发送到服务器。我正在使用来自世博会的TaskManager API,我会跟踪所有内容,并且它可以正常工作,我正在获取数据。但是当我将我的应用程序放在后台时,它会停止console.log(data)

这是否应该在后台任务中运行(TaskManager),即使在使用 Expo Dev Tool 的开发环境中,还是需要在它工作之前进入生产模式?

当我像这样将应用程序切换到后台模式时,我的console.log 停止工作。

我的示例代码 App.js

const LOCATION_TRACKER = 'background-task-location';

export default class App extends Component {
     state = {
      mapRegion: null,
      hasLocationPermissions: false,
      locationResult: null,
      marker: {
        latitude: 0,
        longitude: 0
      },
      latitude: 0,
      longitude: 0,
      location: null,
      errorMessage: null
    }


   componentDidMount() {
    //this.watchCurLocation();
    this.onLoad();  

   }

   //init task manager
  onLoad = async() => {
    let isRegistered = await TaskManager.isTaskRegisteredAsync(LOCATION_TRACKER)
    if (!isRegistered) await Location.startLocationUpdatesAsync(LOCATION_TRACKER, {
      accuracy: Location.Accuracy.High,
      /* after edit */
      timeInterval: 2500,
      distanceInterval: 5,
    })
  }


onPress = async () => {
    console.log('waiting')
    await Location.startLocationUpdatesAsync(LOCATION_TRACKER, {
      accuracy: Location.Accuracy.High,
      timeInterval: 5000,
      distanceInterval: 5

    });

    console.log('waiting for get task option');
    //const data = await TaskManager.getTaskOptionsAsync(LOCATION_TRACKER)
    //console.log(data);

  };


watchCurLocation = () =>{
    this.onPress();
    setTimeout(() => {
      this.watchCurLocation();
    }, 5000);
  }


}


TaskManager.defineTask(LOCATION_TRACKER, ({ data, error }) => {
  if (error) {
    console.log(error)
    // Error occurred - check `error.message` for more details.
    return;
  }
  if (data) {
    const { locations } = data;
    console.log(data)
    // do something with the locations captured in the background
  }
});

【问题讨论】:

    标签: android react-native expo


    【解决方案1】:

    在 Android Oreo 及更高版本中,应用程序将不会在后台运行此类任务。 应有的限制:https://developer.android.com/about/versions/oreo/background#services

    您必须在系统托盘中显示一些通知才能让跟踪器在后台工作。

    a) 您可以尝试添加使用频道的通知,而无需弹出 expo

    https://docs.expo.io/versions/v32.0.0/guides/notification-channels

    或 b) 弹出 expo 并添加一个前台服务,该服务将在应用程序进入后台时启动

    与此类似,只是为了了解一下:

    public class ForegroundService extends Service {
    ///....
    
       public  void toForeground(){ 
            startForeground(NOTIFICATION_ID, getNotification()); 
        }
    
       public void toBackground(){ 
            stopForeground(true); 
       }   
        /**
         * Returns the {@link NotificationCompat} used as part of the foreground service.
         */
        private Notification getNotification() {
            Intent intent = new Intent(this, ForegroundService.class);  
    
            // The PendingIntent that leads to a call to onStartCommand() in this service.
            PendingIntent servicePendingIntent = PendingIntent.getService(this, 0, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
    
            // The PendingIntent to launch activity.
            PendingIntent activityPendingIntent = PendingIntent.getActivity(this, 0,
                    new Intent(this, MainActivity.class), 0);
    
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                    .addAction(R.drawable.shell_notification_icon, getString(R.string.launch_activity),
                            activityPendingIntent) 
                    .setContentText(text)
                    .setContentTitle(Utils.getLocationTitle(this))
                    .setOngoing(true)
                    .setPriority(Notification.PRIORITY_DEFAULT)
                    .setSmallIcon(R.drawable.shell_notification_icon)
                    .setTicker(text)
                    .setDefaults(Notification.DEFAULT_LIGHTS)
                    .setWhen(System.currentTimeMillis());
    
            // Set the Channel ID for Android O.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                builder.setChannelId(CHANNEL_ID); // Channel ID
            }
    
            return builder.build();
        }
    //...
    }
    

    【讨论】:

      猜你喜欢
      • 2020-11-16
      • 1970-01-01
      • 2017-11-17
      • 2018-11-18
      • 2012-06-17
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      • 1970-01-01
      相关资源
      最近更新 更多