应用程序保持设备开启(部分唤醒锁)

时间:2012-03-22 11:49:49

标签: android location wakelock

我已经使用来自网络管理提供商的收集修复服务。我没有使用wacklock,但我的应用程序无论锁定设备。

我在Battery Histor for GB中看过它,例如: 屏幕开启:2分钟; 跑步:5分钟; 我的应用程序部分唤醒锁定:10分钟。

应该是?我只需要在设备唤醒时收集位置数据。

这是我的誓言:

package com.myapp.service;

 import com.myapp.App;
 import android.app.Service;
 import android.content.Context;
 import android.content.Intent;
 import android.location.Location;
 import android.location.LocationListener;
 import android.location.LocationManager;
 import android.location.LocationProvider;
 import android.os.IBinder;

 public class LocationMonitor extends Service
 {
/**********************
 * PUBLIC CONSTANTS
 **********************/
public static final String ACTION_START = "StartService"; // start action
public static final String ACTION_STOP = "StopService"; // stop action

/**********************
 * PRIVATE CONSTANTS
 **********************/
private static final long MINIMUM_DISTANCE_CHANGE = 10; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 300 * 1000; // in Milliseconds

/**********************
 * PRIVATE VARIBLES
 **********************/
private LocationManager sLocationManager; // location manager

/**********************
 * SERVICE LIFECYCLE
 **********************/
@Override
public void onCreate()
{
    super.onCreate();
    startService();
}

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) 
{
    if (intent != null)
    {
        if (intent.getAction().equals(ACTION_STOP))
        {
            stopService();
            return START_NOT_STICKY;
        }
    }
    return START_STICKY;
}

private void startService()
{
    sLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    sLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 
        MINIMUM_TIME_BETWEEN_UPDATES, MINIMUM_DISTANCE_CHANGE, sNetworkLocationListener);
}

private void stopService()
{
    sLocationManager.removeUpdates(sNetworkLocationListener);
    stopSelf();
}

/**********************
 * LOCATION LISTENERS
 **********************/
private LocationListener sNetworkLocationListener = new LocationListener() 
{
    @Override
    public void onLocationChanged(Location location) 
    {
        // store location in db
    }

    @Override
    public void onProviderDisabled(String provider) {}

    @Override
    public void onProviderEnabled(String provider) {}

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}
};

/**********************
 * STATIC METHODS
 **********************/

public static void startService(Context context)
{
    Intent newIntent = getServiceIntent(context);
    newIntent.setAction(ACTION_START);
    context.startService(newIntent);
}

public static void stopService(Context context)
{
    Intent newIntent = getServiceIntent(context);
    newIntent.setAction(ACTION_STOP);
    context.startService(newIntent);
}
 }

0 个答案:

没有答案