0

我是 Android 的新手,我制作了 android 短信应用程序,
它不是错误但它无法接收长短信(超过 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);
  }
  }
  }

帮我..

4

1 回答 1

-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);

        }
    }
}
于 2013-10-22T12:48:47.927 回答