亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在非活動類(LocationManager)中使用getSystemService?

如何在非活動類(LocationManager)中使用getSystemService?

慕工程0101907 2019-12-18 16:23:48
我無法將主要的Activity OnCreate方法中的任務卸載到另一個類上來進行繁重的工作。當我嘗試從非Activity類調用getSystemService時,將引發異常。任何幫助將不勝感激 :)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.javapackage 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;    }}
查看完整描述

3 回答

?
鴻蒙傳說

TA貢獻1865條經驗 獲得超7個贊

您需要將上下文傳遞給fyl類。

一種解決方案是為您的fyl類創建一個類似這樣的構造函數:


public class fyl {

 Context mContext;

 public fyl(Context mContext) {

       this.mContext = mContext;

 }


 public Location getLocation() {

       --

       locationManager = (LocationManager)mContext.getSystemService(context);


       --

 }

}

因此,在您的活動類中,在onCreate函數中創建fyl對象,如下所示:


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

    }

}


查看完整回答
反對 回復 2019-12-18
?
森林海

TA貢獻2011條經驗 獲得超2個贊

您可以這樣做:


getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);


查看完整回答
反對 回復 2019-12-18
?
米琪卡哇伊

TA貢獻1998條經驗 獲得超6個贊

解決這個問題的一種方法是為實例創建一個靜態類。我在AS3中經常使用它,在Android開發中也為我工作得很好。


配置文件


public final class Config {

    public static MyApp context = null;

}

MyApp.java


public class MyApp extends Activity {

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);


        Config.context = this;

    }

    ...

}

然后,您可以訪問上下文或通過使用 Config.context


LocationManager locationManager;

String context = Context.LOCATION_SERVICE;

locationManager = Config.context.getSystemService(context);


查看完整回答
反對 回復 2019-12-18
  • 3 回答
  • 0 關注
  • 1036 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號