Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 类型SmsReciever-context的方法未定义_Android_Android Activity_Gps_Sms_Android Context - Fatal编程技术网

Android 类型SmsReciever-context的方法未定义

Android 类型SmsReciever-context的方法未定义,android,android-activity,gps,sms,android-context,Android,Android Activity,Gps,Sms,Android Context,我是Android开发新手。我正在开发一个应用程序,当接收到带有唯一文本字符串的短信时,GPS将启用并开始跟踪手机的位置。我遇到的问题是getSystemService方法。 我收到一个错误,即类型SmsReceiver的getSystemServiceString方法未定义,我认为这是因为它没有上下文。我尝试在我的代码中添加一个带有“ctx”的上下文,这样可以消除错误,但每次我在手机上运行它时,我的应用程序都会崩溃。接收短信的代码工作正常,如果在我的主课上,GPS位置跟踪代码工作正常 我仍然不

我是Android开发新手。我正在开发一个应用程序,当接收到带有唯一文本字符串的短信时,GPS将启用并开始跟踪手机的位置。我遇到的问题是getSystemService方法。 我收到一个错误,即类型SmsReceiver的getSystemServiceString方法未定义,我认为这是因为它没有上下文。我尝试在我的代码中添加一个带有“ctx”的上下文,这样可以消除错误,但每次我在手机上运行它时,我的应用程序都会崩溃。接收短信的代码工作正常,如果在我的主课上,GPS位置跟踪代码工作正常

我仍然不完全理解上下文,有人能帮我吗

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class SmsReceiver extends BroadcastReceiver {
    LocationManager lm;
    LocationListener loc;
    Context ctx;
    public SmsReceiver(Context c) { ctx = c; } 

    @Override
    public void onReceive(Context context, Intent intent) {
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;
        String str = "";            
        if (bundle != null){
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];            
            for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                                    
                str += msgs[i].getMessageBody().toString() + "\n";        
            }

            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
            if (msgs[0].getMessageBody().toString() == "Track"){
                enableGPS();
            }
        }                         
    }

    public void enableGPS() {  
        lm = (LocationManager)ctx.getSystemService(Context.LOCATION_SERVICE);
        loc = new mylocationlistener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, loc);
    }

    public void disableGPS() {
        lm.removeUpdates(loc);
    }

    private class mylocationlistener implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            String s = "";
            s += "\tTime: "        + location.getTime() + "\n";
            s += "\tLatitude:  " + location.getLatitude()  + "°\n";
            s += "\tLongitude: " + location.getLongitude() + "°\n";
            s += "\tAccuracy:  " + location.getAccuracy()  + " metres\n";
            s += "\tAltitude:  " + location.getAltitude()  + " metres\n";

            //Toast.makeText(SmsReceiver.this, s, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onProviderDisabled(String arg0) { }
        @Override
        public void onProviderEnabled(String arg0) { }
        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) { }
    }
}

您可以将上下文作为参数传递给enableGPS,并使用它调用getSystemService

如何创建SmsReceiver的实例?当然,请格式化您的postYes,从onReceive方法传入“上下文”。谢谢你的帮助