gpt4 book ai didi

java - Little Fluffy 位置库找不到位置

转载 作者:行者123 更新时间:2023-12-01 11:14:42 26 4
gpt4 key购买 nike

我的目标是拥有一个在后台运行的服务,并以指定的时间间隔(例如每 10 分钟)将我的设备位置发送到远程服务器。我正在尝试使用 Little Fluffy Location Library 来优化电池生命周期,同时获取设备的位置。我还无法成功获取位置。我不确定我做错了什么。

我关注了this service tutorial还有这个LFLL example 。每次我运行此命令时,MyService.java 中的 Toast 都会显示“尚未找到位置”。此外,据我了解,LFLL 将循环并通过 BroadcastReceiver 获取新位置(我对此有错吗?)。但是,它永远不会再次尝试获取位置,也永远不会进入 MyBroadcastReceiver.java。

MainActivity.java

package com.testtracker;

import android.app.Activity;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity
{
Button startButton;
Button stopButton;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

startButton = (Button)findViewById(R.id.button2);
stopButton = (Button)findViewById(R.id.button);

startButton.setEnabled(true);
stopButton.setEnabled(false);

LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
boolean locationEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

if(!locationEnabled)
{
Intent intent = new Intent(Settings.ACTION_LOCALE_SETTINGS);
startActivity(intent);
}
}

public void startService(View view)
{
startButton.setEnabled(false);
stopButton.setEnabled(true);

Intent intent = new Intent(getBaseContext(), MyService.class);
startService(intent);

}

public void stopService(View view)
{
startButton.setEnabled(true);
stopButton.setEnabled(false);

Intent intent = new Intent(getBaseContext(), MyService.class);
stopService(intent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

MyService.java

package com.testtracker;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;
import android.widget.Toast;

import com.littlefluffytoys.littlefluffylocationlibrary.LocationInfo;
import com.littlefluffytoys.littlefluffylocationlibrary.LocationLibraryConstants;

public class MyService extends Service
{
protected PowerManager.WakeLock wakeLock;

@Override
public IBinder onBind(Intent intent)
{
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startID)
{
Log.d("LOGGER", "in start");

PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
wakeLock.acquire();

refreshDisplay();

return START_STICKY;
}

private void refreshDisplay()
{
Log.d("LOGGER", "MyService refreshDisplay()");
refreshDisplay(new LocationInfo(this));
}

private void refreshDisplay(final LocationInfo locationInfo)
{
if(locationInfo.anyLocationDataReceived())
{
Toast.makeText(this, "lat: " + Float.toString(locationInfo.lastLat) + " lng: " + Float.toString(locationInfo.lastLong) + " acc: " + Integer.toString(locationInfo.lastAccuracy) + " prov: " + locationInfo.lastProvider, Toast.LENGTH_SHORT).show();

if(locationInfo.hasLatestDataBeenBroadcast())
{
Toast.makeText(this, "Location has been broadcast", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "Locaiton broadcast pending", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(this, "No location found yet", Toast.LENGTH_SHORT).show();
}
}

private final BroadcastReceiver lftBroadcastReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
final LocationInfo locationInfo = (LocationInfo)intent.getSerializableExtra(LocationLibraryConstants.LOCATION_BROADCAST_EXTRA_LOCATIONINFO);
refreshDisplay(locationInfo);
}
};

@Override
public void onDestroy()
{
super.onDestroy();
wakeLock.release();
Log.d("LOGGER", "MyService onDestroy");
}
}

MyApplication.java

package com.testtracker;

import android.app.Application;
import android.util.Log;
import android.widget.Toast;

import com.littlefluffytoys.littlefluffylocationlibrary.LocationLibrary;

public class MyApplication extends Application
{
@Override
public void onCreate()
{
super.onCreate();

Log.d("LOGGER", "application oncreate");

Toast.makeText(this, "in application", Toast.LENGTH_SHORT).show();

LocationLibrary.showDebugOutput(true);

try
{
LocationLibrary.initialiseLibrary(getBaseContext(), 60 * 1000, 2 * 60 * 1000, "com.testtracker");
}
catch(UnsupportedOperationException e)
{
Log.d("LOGGER", "UnsupportedOperationException thrown - the device doesn't have any location providers");
}
}
}

MyBroadcastReceiver.java

package com.testtracker;

import com.littlefluffytoys.littlefluffylocationlibrary.LocationInfo;
import com.littlefluffytoys.littlefluffylocationlibrary.LocationLibraryConstants;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class MyBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "IN broadcaster!", Toast.LENGTH_SHORT).show();

Log.d("LOGGER", "BroadcastReceiver - onReceive: received location updated");

final LocationInfo locationInfo = (LocationInfo) intent.getSerializableExtra(LocationLibraryConstants.LOCATION_BROADCAST_EXTRA_LOCATIONINFO);

Intent contentIntent = new Intent(context, MainActivity.class);
PendingIntent contentPendingIntent = PendingIntent.getActivity(context, 0, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}
}

最佳答案

我不知道您是否已经发现了这个问题。我觉得问题出在你的 list 文件中。

<service android:name="com.littlefluffytoys.littlefluffylocationlibrary.LocationBroadcastService" />
<receiver android:name="com.littlefluffytoys.littlefluffylocationlibrary.PassiveLocationChangedReceiver" android:exported="true" />

那么你的接收器应该有这个,

<receiver android:name="com.testtracker.MyBroadcastReceiver">  
<intent-filter>
<action android:name="com.testtracker.littlefluffylocationlibrary.LOCATION_CHANGED" />
</intent-filter>
</receiver>

此外,在接收器中您将获取 LocationInfo 对象,我认为更好的方法是从接收器启动您的服务,并将 LocationInfo 作为附加内容传递给 Intent 。

Intent service = new Intent(context, MyService.class);
service.putExtra(LocationLibraryConstants.LOCATION_BROADCAST_EXTRA_LOCATIONINFO, locationInfo);

然后从 LocationInfo 中获取您的服务的纬度和经度。

final LocationInfo locationInfo = (LocationInfo) intent.getSerializableExtra(LocationLibraryConstants.LOCATION_BROADCAST_EXTRA_LOCATIONINFO);
if (locationInfo != null) {
float lat = locationInfo.lastLat;
float lng = locationInfo.lastLong;
}

希望使用这个库的人可以从中获得帮助。

关于java - Little Fluffy 位置库找不到位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31974800/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com