第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

慕工程0101907 2019-12-18 16:23:48
我無法將主要的Activity OnCreate方法中的任務(wù)卸載到另一個類上來進(jìn)行繁重的工作。當(dāng)我嘗試從非Activity類調(diào)用getSystemService時,將引發(fā)異常。任何幫助將不勝感激 :)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貢獻(xiàn)1865條經(jīng)驗 獲得超7個贊

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

一種解決方案是為您的fyl類創(chuàng)建一個類似這樣的構(gòu)造函數(shù):


public class fyl {

 Context mContext;

 public fyl(Context mContext) {

       this.mContext = mContext;

 }


 public Location getLocation() {

       --

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


       --

 }

}

因此,在您的活動類中,在onCreate函數(shù)中創(chuàng)建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);

    }

}


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

TA貢獻(xiàn)2011條經(jīng)驗 獲得超2個贊

您可以這樣做:


getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);


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

TA貢獻(xiàn)1998條經(jīng)驗 獲得超6個贊

解決這個問題的一種方法是為實例創(chuàng)建一個靜態(tài)類。我在AS3中經(jīng)常使用它,在Android開發(fā)中也為我工作得很好。


配置文件


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


查看完整回答
反對 回復(fù) 2019-12-18
  • 3 回答
  • 0 關(guān)注
  • 1034 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號