问题描述
我无法从主活动卸载任务onCreate方法到另一个类做繁重的任务。
当我尝试从一个异常被抛出非Activity类调用getSystemService。
任何帮助将是很大的AP preciated:)
lmt.java:
包com.atClass.lmt;
进口android.app.Activity;
进口android.os.Bundle;
进口android.widget.TextView;
进口android.location.Location;
公共类LMT延伸活动{
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
五年之后lfyl =新的五年之后();
位置位置= lfyl.getLocation();
串latLongString = lfyl.updateWithNewLocation(位置);
TextView的myLocationText =(TextView中)findViewById(R.id.myLocationText);
myLocationText.setText(您当前的位置是:\ N+ latLongString);
}
}
fyl.java
包com.atClass.lmt;
进口android.app.Activity;
进口android.os.Bundle;
进口android.location.Location;
进口android.location.LocationManager;
进口android.os.Bundle;
进口android.widget.TextView;
进口android.content.Context;
公共类五年之后{
公共场所的getLocation(){
LocationManager locationManager;
字符串情形= Context.LOCATION_SERVICE;
locationManager =(LocationManager)getSystemService(上下文);
字符串提供商= LocationManager.GPS_PROVIDER;
位置位置= locationManager.getLastKnownLocation(供应商);
返回的位置;
}
公共字符串updateWithNewLocation(位置定位){
字符串latLongString;
如果(位置!= NULL){
双纬度= location.getLatitude();
双LNG = location.getLongitude();
latLongString =纬度:+纬度+\ nLong:+ LNG;
}其他{
latLongString =没有位置;
}
返回latLongString;
}
}
您需要将您的上下文传递给你五年之后类..
一个解决办法是让这样的构造你的五年之后
类:
公共类五年之后{
语境mContext;
公共五年之后(上下文mContext){
this.mContext = mContext;
}
公共场所的getLocation(){
-
locationManager =(LocationManager)mContext.getSystemService(上下文);
-
}
}
因此,在您的活动类中创建五年之后在这样的的onCreate
函数的对象:
包com.atClass.lmt;
进口android.app.Activity;
进口android.os.Bundle;
进口android.widget.TextView;
进口android.location.Location;
公共类LMT延伸活动{
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
五年之后lfyl =新的五年之后(本); //这里上下文被传递
位置位置= lfyl.getLocation();
串latLongString = lfyl.updateWithNewLocation(位置);
TextView的myLocationText =(TextView中)findViewById(R.id.myLocationText);
myLocationText.setText(您当前的位置是:\ N+ latLongString);
}
}
I'm having trouble offloading tasks from the main Activities OnCreate method onto another class to do the heavy lifting.
When I try to call getSystemService from the non-Activity class an exception is thrown.
Any help would be greatly appreciated :)
lmt.java:
package com.atClass.lmt;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.location.Location;
public class lmt extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fyl lfyl = new fyl();
Location location = lfyl.getLocation();
String latLongString = lfyl.updateWithNewLocation(location);
TextView myLocationText = (TextView)findViewById(R.id.myLocationText);
myLocationText.setText("Your current position is:\n" + latLongString);
}
}
fyl.java
package com.atClass.lmt;
import android.app.Activity;
import android.os.Bundle;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.content.Context;
public class fyl {
public Location getLocation(){
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
return location;
}
public String updateWithNewLocation(Location location) {
String latLongString;
if (location != null){
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
}else{
latLongString = "No Location";
}
return latLongString;
}
}
You need to pass your context to your fyl class..
One solution is make a constructor like this for your fyl
class:
public class fyl {
Context mContext;
public fyl(Context mContext) {
this.mContext = mContext;
}
public Location getLocation() {
--
locationManager = (LocationManager)mContext.getSystemService(context);
--
}
}
So in your activity class create the object of fyl in onCreate
function like this:
package com.atClass.lmt;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.location.Location;
public class lmt extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fyl lfyl = new fyl(this); //Here the context is passing
Location location = lfyl.getLocation();
String latLongString = lfyl.updateWithNewLocation(location);
TextView myLocationText = (TextView)findViewById(R.id.myLocationText);
myLocationText.setText("Your current position is:\n" + latLongString);
}
}
这篇关于我怎样才能在非活动类(LocationManager)使用getSystemService?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!