实现发送短信的代码?

时间:2012-04-12 20:26:24

标签: java android

我编写的代码旨在发送短信,它可以与模拟器一起成功运行,但在真实设备上失败了!社区是否可以帮助我识别我的代码或任何相关信息和链接的任何问题以帮助我?

public class SmsActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
        final Button sendSms = (Button) findViewById (R.id.smsBut);
        sendSms.setOnClickListener(new View.OnClickListener() {         
            public void onClick(View v) {
            // TODO Auto-generated method stub              
            sendSMS("5556", "'hello frinds");
            sendSMS("*********", "'hello frinds");// **= real number
            }
        });

    }// end onCr

    public void sendSMS (String phoneNo, String msg) {      
        PendingIntent pi = PendingIntent.getActivity(this, 0,
            new Intent(this, SmsActivity.class), 0);                
        SmsManager sms = SmsManager.getDefault();       
        sms.sendTextMessage(phoneNo, null, msg, null, null);
    }    
}

这是清单:

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="sms.ecp.ccc"
        android:versionCode="1"
        android:versionName="1.0">
            <uses-sdk android:minSdkVersion="10" />
            <uses-permission android:name="android.permission.SEND_SMS" />
            <application android:icon="@drawable/ic_launcher"android:
                label="@string/app_name" >
            <activity android:name=".SmsActivity" android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    </manifest>   

1 个答案:

答案 0 :(得分:1)

更改最后一行

sms.sendTextMessage(phoneNo, null, msg, pi, null);