0

Android開発は初めてです。一意のテキスト文字列を含む SMS メッセージを受信すると、GPS が有効になり、電話の位置の追跡を開始するアプリを開発しています。私が抱えている問題は、getSystemService()方法にあります。エラーが表示"The method getSystemService(String) is undefined for the type SmsReceiver"されます。これは、コンテキストがないためだと思います。「ctx」を使用してコード内にコンテキストを追加しようとしましたが、エラーは削除されましたが、電話で実行するたびにアプリケーションがクラッシュしました。SMS を受信するためのコードは問題なく動作し、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) { }
    }
}
4

1 に答える 1

0

Context を引数として渡しenableGPS、それを使用して呼び出すことができますgetSystemService

于 2010-11-13T20:52:14.853 に答える