我正在尝试开发一个简单的位置跟踪器。但是,我收到以下错误:
The method abortBroadcast() is undefined for the type SMSReceiver
。我无法弄清楚。我收到此错误的行是this.abortBroadcast
。请帮我解决这个问题。
以下是代码:
package com.example.whereareyou;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.BroadcastReceiver;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
public class SMSReceiver {
LocationManager lm;
LocationListener locationListener;
String senderTel;
public void onReceive(Context context, Intent intent)
{
Bundle bundle= intent.getExtras();
SmsMessage[] msgs= null;
String str="";
if(bundle!=null)
{
senderTel="";
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]);
if(i==0)
{
senderTel=msgs[i].getOriginatingAddress();
}
str+=msgs[i].getMessageBody().toString();
}
if(str.startsWith("where are you?"))
{
lm=(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
locationListener= new MyLocationListener();
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 1000, locationListener);
this.abortBroadcast();
}
}
}
private class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
if(loc!=null)
{
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(senderTel, null, "http://maps.google.com/maps?q=" + loc.getLatitude() + "," + loc.getLongitude(), null, null);
lm.removeUpdates(locationListener);
}
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
}
答案 0 :(得分:1)
如果您想以abortBroadcast()
方式使用BroadcastReceiver
,则SMSReceiver
类应扩展BroadcastReceiver
。
public class SMSReceiver extends BroadcastReceiver {