如何自动回复文字?

时间:2011-06-23 00:27:41

标签: android sms user-activity

好的,所以这是我的源代码..出现问题并且出现Force关闭错误。 这是代码,有人可以告诉我是否需要进行任何更改..

我有两个发送消息的类和一个接收的类......我们去了......

我的第一个类有一个发送文本的方法..

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.widget.Toast;

public class InfoSender extends Activity {
private smsListener smsReceiver;
public String phoneNumber;
public String message; 

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_menu);
}

    public void send(String phoneNumber, String message){

        phoneNumber = smsListener.phnNumber;

          PendingIntent pi = PendingIntent.getActivity(this, 0,
                    new Intent(this, InfoSender.class), 0);                
                SmsManager sms = SmsManager.getDefault();
                sms.sendTextMessage(phoneNumber, null, message, pi, null);

            Toast.makeText(getApplicationContext(), "Details about house "+housenumber+"sent", 6);



            //Do nothing
        }

    }

如果文本说某件事情,则接收并自动响应的第二类......

@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;
    String msg = "";

    if(bundle != null){

    }
    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]);
        msg += "Sms from "+msgs[i].getDisplayOriginatingAddress();

        phnNumber = msgs[i].getOriginatingAddress();
        msgBody = msgs[i].getMessageBody().toString(); 
    }

        if(msgBody.equalsIgnoreCase("0")){
            String phoneNumber = phnNumber;
            String message = sender.message;

        sender.send(phoneNumber, message);
    }
    }

}

1 个答案:

答案 0 :(得分:0)

没有任何日志,很难说,但很可能你有权限。您需要在清单中设置“发送,接收,读取和写入”权限。