-1

好的,这是我的源代码.. 一直出错并出现强制关闭错误。这是代码,有人可以告诉我是否需要进行任何更改..

我有两门课,一门发送消息,一门接收……我们开始吧……

我的第一堂课有一种发送文本的方法..

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

}

4

1 回答 1

0

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

于 2011-06-23T03:04:55.970 回答