在我的代码中,我有一个 java 文件,其中包含:
package com.myapp.basic;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SMSReceiverActivity extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Parse the SMS.
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
// Retrieve the SMS.
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]);
// In case of a particular App / Service.
//if(msgs[i].getOriginatingAddress().equals("+91XXX"))
//{
//str += "SMS from " + msgs[i].getOriginatingAddress();
//str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
//}
}
if (str != "") { // remove the last \n
str = str.substring(0, str.length()-1);
}
// Need to find a better way to check if the activity is running
try {
((Police_ViewActivity) context.getApplicationContext()).handle_incoming_help_message(str); // crash here
} catch(Exception e) {
System.out.println(e.getMessage());
}
System.out.println("ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ");
}
}
}
当 try 块中的代码发生时,它会崩溃并且我收到此错误:08-27 22:45:11.345: I/System.out(27606): android.app.Application 无法转换为 com.escortme.basic。 Police_ViewActivity
我怎样才能解决这个问题 ?