小蓬松位置库找不到位置 [英] Little Fluffy Location Library can't find location
问题描述
我的目标是有在后台运行,并在指定的时间间隔(如每隔10分钟)发送我的设备的位置到远程服务器的服务。我试图用小蓬松位置库,以优化电池寿命,同时获得该设备的位置。我一直没能顺利拿到的位置呢。我不知道我做错了。
我跟着这项服务教程这<一个href=\"https://$c$c.google.com/p/little-fluffy-location-library/downloads/detail?name=LittleFluffyTestClient_v15.zip&can=1&q=\"相对=nofollow> LFLL例如。每次我在MyService.java运行此,敬酒没有位置找到尚显示器。此外,这是我的理解是LFLL将循环,并通过广播接收器(我错了这个?)获得新的位置。然而,它永远不会尝试再次获得位置,永远不会出现进入MyBroadcastReceiver.java。
MainActivity.java
包com.testtracker;进口android.app.Activity;
进口android.content.Intent;
进口android.location.LocationManager;
进口android.os.Bundle;
进口android.provider.Settings;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.widget.Button;公共类MainActivity扩展活动
{
按钮startButton;
按钮STOPBUTTON;@覆盖
保护无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); startButton =(按钮)findViewById(R.id.button2);
STOPBUTTON =(按钮)findViewById(R.id.button); startButton.setEnabled(真);
stopButton.setEnabled(假); 的LocationManager的LocationManager =(的LocationManager)getSystemService(LOCATION_SERVICE);
布尔locationEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 如果(!locationEnabled)
{
意向意图=新意图(Settings.ACTION_LOCALE_SETTINGS);
startActivity(意向);
}
}公共无效startService(查看视图)
{
startButton.setEnabled(假);
stopButton.setEnabled(真); 意向意图=新意图(getBaseContext(),MyService.class);
startService(意向);}公共无效stopService(查看视图)
{
startButton.setEnabled(真);
stopButton.setEnabled(假); 意向意图=新意图(getBaseContext(),MyService.class);
stopService(意向);
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单)
{
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.menu_main,菜单);
返回true;
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
//处理动作栏项目点击这里。操作栏会
//自动处理上点击主页/向上按钮,只要
//你在AndroidManifest.xml中指定一个父活动。
INT ID = item.getItemId(); // noinspection SimplifiableIfStatement
如果(ID == R.id.action_settings){
返回true;
} 返回super.onOptionsItemSelected(项目);
}
}
MyService.java
包com.testtracker;进口android.app.Service;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.os.IBinder;
进口android.os.PowerManager;
进口android.util.Log;
进口android.widget.Toast;进口com.littlefluffytoys.littlefluffylocationlibrary.LocationInfo;
进口com.littlefluffytoys.littlefluffylocationlibrary.LocationLibraryConstants;公共类为MyService扩展服务
{
保护PowerManager.WakeLock激活锁定;@覆盖
公众的IBinder onBind(意向意图)
{
返回null;
}@覆盖
公众诠释onStartCommand(意向意图,诠释旗帜,INT startID)
{
Log.d(记录仪,在开始); 电源管理电源管理=(电源管理)getSystemService(Context.POWER_SERVICE);
激活锁定= powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCKMyWakeLock);
wakeLock.acquire(); refreshDisplay(); 返回START_STICKY;
}私人无效refreshDisplay()
{
Log.d(记录器,为MyService refreshDisplay());
refreshDisplay(新LocationInfo(本));
}私人无效refreshDisplay(最终LocationInfo locationInfo)
{
如果(locationInfo.anyLocationDataReceived())
{
Toast.makeText(这一点,纬度:+ Float.toString(locationInfo.lastLat)+LNG+ Float.toString(locationInfo.lastLong)+ACC:+ Integer.toString(locationInfo.lastAccuracy)+省:+ locationInfo.lastProvider,Toast.LENGTH_SHORT).show(); 如果(locationInfo.hasLatestDataBeenBroadcast())
{
Toast.makeText(这一点,位置已经播出的,Toast.LENGTH_SHORT).show();
}
其他
{
Toast.makeText(这一点,Locaiton播出待定,Toast.LENGTH_SHORT).show();
}
}
其他
{
Toast.makeText(这一点,没有位置找到没有?,Toast.LENGTH_SHORT).show();
}
}私人最终的BroadcastReceiver lftBroadcastReceiver =新的广播接收器()
{
@覆盖
公共无效的onReceive(上下文的背景下,意图意图)
{
最后LocationInfo locationInfo = (LocationInfo)intent.getSerializableExtra(LocationLibraryConstants.LOCATION_BROADCAST_EXTRA_LOCATIONINFO);
refreshDisplay(locationInfo);
}
};@覆盖
公共无效的onDestroy()
{
super.onDestroy();
wakeLock.release();
Log.d(记录仪,为MyService的onDestroy);
}
}
MyApplication.java
包com.testtracker;进口android.app.Application;
进口android.util.Log;
进口android.widget.Toast;进口com.littlefluffytoys.littlefluffylocationlibrary.LocationLibrary;公共类MyApplication的扩展应用
{
@覆盖
公共无效的onCreate()
{
super.onCreate(); Log.d(记录仪,应用程序在OnCreate); Toast.makeText(这一点,应用,Toast.LENGTH_SHORT).show(); LocationLibrary.showDebugOutput(真); 尝试
{
LocationLibrary.initialiseLibrary(getBaseContext(),60 * 1000,2 * 60 * 1000,com.testtracker);
}
赶上(UnsupportedOperationException异常E)
{
Log.d(记录仪,甩抛出:UnsupportedOperationException - 该设备没有任何位置提供);
}
}
}
MyBroadcastReceiver.java
包com.testtracker;进口com.littlefluffytoys.littlefluffylocationlibrary.LocationInfo;
进口com.littlefluffytoys.littlefluffylocationlibrary.LocationLibraryConstants;进口android.app.PendingIntent;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.util.Log;
进口android.widget.Toast;公共类MyBroadcastReceiver扩展广播接收器
{
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
Toast.makeText(!在广播背景下,Toast.LENGTH_SHORT).show(); Log.d(记录仪,广播接收器 - 的onReceive:接收到的位置更新); 最后LocationInfo locationInfo =(LocationInfo)intent.getSerializableExtra(LocationLibraryConstants.LOCATION_BROADCAST_EXTRA_LOCATIONINFO); 意图contentIntent =新意图(背景下,MainActivity.class);
的PendingIntent contentPendingIntent = PendingIntent.getActivity(上下文,0,contentIntent,PendingIntent.FLAG_UPDATE_CURRENT);
}
}
我不知道你已经找到了问题。我觉得这个问题是在你的清单文件。
&LT;服务机器人:名字=com.littlefluffytoys.littlefluffylocationlibrary.LocationBroadcastService/&GT;
&lt;接收机器人:名字=com.littlefluffytoys.littlefluffylocationlibrary.PassiveLocationChangedReceiver机器人:出口=真/&GT;
那么你的接收器应该有这个,
&lt;接收机器人:名字=com.testtracker.MyBroadcastReceiver&GT;
&所述;意图滤光器&gt;
&lt;作用机器人:名字=com.testtracker.littlefluffylocationlibrary.LOCATION_CHANGED/&GT;
&所述; /意图滤光器&gt;
&LT; /接收器&GT;
另外,你所得到的LocationInfo对象的接收器,我觉得更好的办法是从接收器启动服务,并通过为LocationInfo到额外的意图。
意向书服务=新意图(背景下,MyService.class);
service.putExtra(LocationLibraryConstants.LOCATION_BROADCAST_EXTRA_LOCATIONINFO,locationInfo);
再从LocationInfo,从你的服务经度和纬度。
最后LocationInfo locationInfo =(LocationInfo)intent.getSerializableExtra(LocationLibraryConstants.LOCATION_BROADCAST_EXTRA_LOCATIONINFO);
如果(locationInfo!= NULL){
浮纬度= locationInfo.lastLat;
浮动液化天然气= locationInfo.lastLong;
}
希望有人使用这个库可以从这个获得帮助。
My Goal is to have a service that runs in the background and sends my device's location to a remote server at a specified interval (eg. every 10 minutes). I am trying to use Little Fluffy Location Library to optimize battery life while getting the device's location. I haven't been able to successfully get the location yet. I am not sure what I am doing wrong.
I followed this service tutorial and this LFLL example. Each time I run this, to Toast in MyService.java "No location found yet" displays. Additionally, it is my understanding that LFLL will loop and get the new location via the BroadcastReceiver (am I wrong on this?). However, it never attempts to get the location again and never appears to enter 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);
}
}
I don't know you already found the issue. I feel the issue is in your Manifest file.
<service android:name="com.littlefluffytoys.littlefluffylocationlibrary.LocationBroadcastService" />
<receiver android:name="com.littlefluffytoys.littlefluffylocationlibrary.PassiveLocationChangedReceiver" android:exported="true" />
Then your receiver should have this,
<receiver android:name="com.testtracker.MyBroadcastReceiver">
<intent-filter>
<action android:name="com.testtracker.littlefluffylocationlibrary.LOCATION_CHANGED" />
</intent-filter>
</receiver>
Also, in the receiver you are getting the LocationInfo object, I feel the better approach is start your service from receiver, and pass the LocationInfo as extras to the intent.
Intent service = new Intent(context, MyService.class);
service.putExtra(LocationLibraryConstants.LOCATION_BROADCAST_EXTRA_LOCATIONINFO, locationInfo);
Then from the LocationInfo, get the latitude and longitude from your service.
final LocationInfo locationInfo = (LocationInfo) intent.getSerializableExtra(LocationLibraryConstants.LOCATION_BROADCAST_EXTRA_LOCATIONINFO);
if (locationInfo != null) {
float lat = locationInfo.lastLat;
float lng = locationInfo.lastLong;
}
Hope somebody uses this library can get help from this.
这篇关于小蓬松位置库找不到位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!