我创建的应用程序是在收到特定消息时获取消息字符串并将消息字符串传递给上传消息方法并从给定URL获取数据并回复相同的数字,但此应用程序有时崩溃并显示强制关闭和短信一次又一次地回复我应该做什么让我的应用程序不会一次又一次地回复短信而不会崩溃。
Incomingsms.java
public class IncomingSms extends BroadcastReceiver {
String senderNum;
String message;
String url;
String casetype;
String no;
String caseyear;
String result;
public void onReceive(Context context, Intent intent) {
Bundle myBundle = intent.getExtras();
SmsMessage [] messages = null;
String strMessage = "";
String msgFrom = "";
String msgText = "";
if (myBundle != null)
{
Object [] pdus = (Object[]) myBundle.get("pdus");
messages = new SmsMessage[pdus.length];
for (int i = 0; i < messages.length; i++)
{
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
strMessage += "SMS From: " + messages[i].getOriginatingAddress();
msgFrom += messages[i].getOriginatingAddress();
strMessage += " : ";
strMessage += messages[i].getMessageBody();
msgText += messages[i].getMessageBody();
strMessage += "\n";
}
Toast.makeText(context, strMessage, Toast.LENGTH_SHORT).show();
try {
String regex = "[\\s;.,:'!?()-]";
String text =msgText ;
String[] sms = text.split(regex);
for(int i = 0; i < sms.length; i++)
{
casetype = sms[0];
no = sms[1];
caseyear = sms[2];
}
}
catch (Exception e)
{
System.out.println(e);
Toast.makeText(context, "wrong msg" , Toast.LENGTH_SHORT).show();
}
url = "http://www.allahabadhighcourt.in/casestatus/caseDetailA.jsp?type=" +
casetype + "&num=" + no + "&year=" + caseyear;
try
{
result = uploadMessage(context,url);
}
catch (Exception e)
{
e.printStackTrace();
}
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(msgFrom, null, result, null, null);
}
}
public String uploadMessage( Context context, String url)
{
//System.out.println(url);
try{
Document doc = Jsoup.connect(url).timeout(20*1000).get();
if (!doc.hasText()){
System.out.println("doc empty");
}
Element pending = doc.select("table td:eq(0)").first();
Element nextDate = doc.select("table td:eq(0)").get(10);
//Element date1 = doc.select("table td:eq(0)").get(11);
Element date = doc.select("table td:eq(1)").last();
String data = pending.text()+"\n" + nextDate.text()+"\n"+ date.text();
return data ;
}catch(Exception ex){
System.out.println("" + ex.getMessage());
return "Nodata" ;
}
}
}
BroadcastNewSms.java
public class BroadcastNewSms extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.androidexample_broadcast_newsms);
}
}
Android Manifest
<uses-permission android:name="android.permission.RECEIVE_SMS" >
</uses-permission><uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" >
</uses-permission><uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission><uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission><receiver android:name="com.androidexample.broadcastreceiver.IncomingSms" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
答案 0 :(得分:0)
让您的广播接收器启动服务(因为这是推荐。广播接收器必须是短暂的。它们只是触发器。您不能让它们做太多的工作)。
让该服务使用AsyncTask(因为没有使用单独的线程导致偶尔的ANR - 应用程序无响应并且在您的情况下关闭力量。)