我的服务启动一个线程,然后使用Handler.postDelayed()循环 – 我最初使用sleep(30000),但我认为这可能已经阻止了locationUpdates.所有服务都是启动此线程 – 并且onStartCommand返回STICKY以使其保持运行:
public class TST extends Thread { final int DefaultNetworkTick = 120000; // 2 minutes final int DefaultLocationTick = 20000; //600000; boolean CustomLocationUpdateTick = false; // If the network requests faster updates int CustomLocationUpdateTickMs = 600000; // Default 10 minutes public boolean kill = false; public Context context; Handler handler = new Handler(); Date nextNetworkReadTick; @OverrIDe public voID run() { Log.i("TST","Start run() iteration"); // Set the ticks to the appropriate values // Network gets read every 2 minutes nextNetworkReadTick = new Date(); nextNetworkReadTick.setTime(nextNetworkReadTick.getTime() + DefaultNetworkTick); Log.i("TST","Set next Network tick to " + nextNetworkReadTick.toString()); Looper.prepare(); Log.i("TST","Performing location update setup..."); SetupLocationListenerDefault(); handler.postDelayed(runFunc,2000); Looper.loop(); } // We do our networking and stuff in here public Runnable runFunc = new Runnable(){ public voID run() { Log.i("TST","run() iteration"); if( new Date().after(nextNetworkReadTick) ) { // Set next tick nextNetworkReadTick = new Date(); nextNetworkReadTick.setTime(nextNetworkReadTick.getTime() + DefaultNetworkTick); UpdateNetwork(); } handler.postDelayed(runFunc,1000); } }; private voID UpdateNetwork() { // Todo auto-generated method stub } LocationManager locMgr; PendingIntent locationUpdateIntentPending; Intent locationUpdateIntent; LocationListener locationListener = new LocationListener() { @OverrIDe public voID onLocationChanged(Location arg0) { // Todo auto-generated method stub Log.i("TST","OnLocationChanged: " + arg0.toString()); } @OverrIDe public voID onProvIDerDisabled(String provIDer) { // Todo auto-generated method stub Log.i("TST","OnProvIDerDisabled: " + provIDer); } @OverrIDe public voID onProvIDerEnabled(String provIDer) { // Todo auto-generated method stub Log.i("TST","OnProvIDerEnabled: " + provIDer); } @OverrIDe public voID onStatusChanged(String provIDer,int status,Bundle extras) { // Todo auto-generated method stub Log.i("TST","OnStatusChanged"); } }; private voID SetupLocationListenerDefault() { // reset location manager if( locMgr != null ) { locMgr.removeUpdates(locationListener); locMgr = null; } locMgr = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); String provIDer = locMgr.getBestProvIDer(criteria,true); locMgr.requestLocationUpdates(provIDer,DefaultLocationTick,locationListener); }}解决方法 重新启动我的手机并开始获得更新 – 似乎AndroID已经卡在某个地方! 总结
以上是内存溢出为你收集整理的Android服务未收到位置更新全部内容,希望文章能够帮你解决Android服务未收到位置更新所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)