我是Android的新手,我制作了Android Sms Aplication,
这不是错误,但它不能接收长短信(超过160个字符),
这是我的SMSReceiver代码,我的错误代码在哪里?请帮帮我..
package com.tugas_akhir;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import model.SMS;
import com.niusounds.sqlite.SQLiteDAO;
import android.annotation.SuppressLint;
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 SMSReceiver extends BroadcastReceiver
{
public SQLiteDAO dao;
public void onReceive(Context paramContext, Intent paramIntent)
{
Toast.makeText(paramContext, "SMS diterima", Toast.LENGTH_LONG).show();
Calendar localCalendar = Calendar.getInstance();
SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("HH:mm, dd MMM yyyy");
dao = SQLiteDAO.getInstance(paramContext, new Class[] { SMS.class });
Bundle localBundle = paramIntent.getExtras();
String str = "";
(SmsMessage[]) = null;
Object[] arrayOfObject = null;
SmsMessage[] arrayOfSmsMessage = null;
if (localBundle != null)
{
arrayOfObject = (Object[])localBundle.get("pdus");
arrayOfSmsMessage = new SmsMessage[arrayOfObject.length];
}
for (int i = 0; ; i++)
{
if (i >= arrayOfSmsMessage.length)
{
Toast.makeText(paramContext, "TERIMA SMS : " + str, Toast.LENGTH_SHORT).show();
return;
}
//insert into table
arrayOfSmsMessage[i] = SmsMessage.createFromPdu((byte[])arrayOfObject[i]);
SMS localSMS = new SMS();
str = arrayOfSmsMessage[i].getMessageBody().toString();
localSMS.setNoHp(arrayOfSmsMessage[i].getOriginatingAddress().toString());
localSMS.setPesan(arrayOfSmsMessage[i].getMessageBody().toString());
localSMS.setTime(localSimpleDateFormat.format(localCalendar.getTime()));
dao.insert(localSMS);
}
}
}
帮帮我......
答案 0 :(得分:-1)
尝试使用此课程接收消息
public class SmsReceiver extends BroadcastReceiver {
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 += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
String replyPhone = msgs[0].getOriginatingAddress();
String request = msgs[0].getMessageBody().toString();
//---display the new SMS message---
// Toast.makeText(context, str + " " + replyPhone + "\n" + request, Toast.LENGTH_LONG).show();
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(1000);
}
}
}