Little Fluffy位置图书馆找不到位置

时间:2015-08-12 20:30:29

标签: java android service gps location

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

我关注了this service tutorialLFLL example。每次我运行它,到MyService.java中的Toast显示“No location found yet”。另外,我的理解是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);
}
}

1 个答案:

答案 0 :(得分:1)

我不知道你已经发现了这个问题。我觉得问题出在你的清单文件中。

<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;
}

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