我正在尝试创建一个名为broadcastMensajes的类,它扩展了BroadcastReceiver,我使用的是此处提供的代码:http://androidexample.com/Incomming_SMS_Broadcast_Receiver_-_Android_Example/index.php?view=article_discription&aid=62&aaid=87
Eclipse强调几乎所有代码,并说"Syntax error on tokens, delete these tokens"
。
"Syntax error on token(s), misplaced construct(s)"
内的if
处 try
代码还可以,为什么会这样?
这是代码:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
public class broadcastMensajes extends BroadcastReceiver {
AccionesExecuter Ejecutor = new AccionesExecuter();
final SmsManager smsManager = SmsManager.getDefault();
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);
}
}
}
错误日志:
Description Resource Path Location Type
Syntax error on token(s), misplaced construct(s) broadcastMensajes.java /Actions/src/com/nahue/actions line 27 Java Problem
Syntax error on tokens, delete these tokens broadcastMensajes.java /Actions/src/com/nahue/actions line 14 Java Problem
Syntax error on tokens, delete these tokens broadcastMensajes.java /Actions/src/com/nahue/actions line 15 Java Problem
Syntax error on tokens, delete these tokens broadcastMensajes.java /Actions/src/com/nahue/actions line 16 Java Problem
Syntax error on tokens, delete these tokens broadcastMensajes.java /Actions/src/com/nahue/actions line 20 Java Problem
Syntax error on tokens, delete these tokens broadcastMensajes.java /Actions/src/com/nahue/actions line 27 Java Problem
Syntax error on tokens, delete these tokens broadcastMensajes.java /Actions/src/com/nahue/actions line 29 Java Problem
Syntax error on tokens, delete these tokens broadcastMensajes.java /Actions/src/com/nahue/actions line 31 Java Problem
Syntax error on tokens, delete these tokens broadcastMensajes.java /Actions/src/com/nahue/actions line 31 Java Problem
Syntax error on tokens, delete these tokens broadcastMensajes.java /Actions/src/com/nahue/actions line 33 Java Problem
Syntax error on tokens, delete these tokens broadcastMensajes.java /Actions/src/com/nahue/actions line 34 Java Problem
Syntax error on tokens, delete these tokens broadcastMensajes.java /Actions/src/com/nahue/actions line 36 Java Problem
Syntax error on tokens, delete these tokens broadcastMensajes.java /Actions/src/com/nahue/actions line 37 Java Problem
Syntax error on tokens, delete these tokens broadcastMensajes.java /Actions/src/com/nahue/actions line 43 Java Problem
Syntax error, insert ";" to complete Statement broadcastMensajes.java /Actions/src/com/nahue/actions line 43 Java Problem
Syntax error, insert "}" to complete Block broadcastMensajes.java /Actions/src/com/nahue/actions line 43 Java Problem
Syntax error, insert "}" to complete ClassBody broadcastMensajes.java /Actions/src/com/nahue/actions line 18 Java Problem
Syntax error, insert "}" to complete MethodBody broadcastMensajes.java /Actions/src/com/nahue/actions line 18 Java Problem
Syntax error, insert "else Statement" to complete IfStatement broadcastMensajes.java /Actions/src/com/nahue/actions line 43 Java Problem
答案 0 :(得分:1)
添加所有受支持的导入,例如:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
public class BroadcastMensajes extends BroadcastReceiver {
AccionesExecuter Ejecutor = new AccionesExecuter();
final SmsManager smsManager = SmsManager.getDefault();
@Override
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);
}
}
}