0

我已经阅读了之前发布的所有文档和问题,但我找不到自己的答案。通过编写以下代码,我想在附近时发送短信。

首先,我想检查一下意图是否只创建短信而不发送?如果没有,有没有办法可以发送短信以获取接近警报。其次,由于某些原因,proximityalert 没有启动,代码有问题吗?

谢谢。

Intent smsintent = new Intent(android.content.Intent.ACTION_VIEW);

smsintent.putExtra("address", "96424127"); 
smsintent.putExtra("sms_body", "child alert");
smsintent.setType("vnd.android-dir/mms-sms"); 

PendingIntent pendintent = PendingIntent.getService(this, 0, smsintent, 0);
lm.addProximityAlert(103.8497215, 1.3630663, 1000, 100000000, pendintent);`

4

2 回答 2

0

您应该使用registerReceiverPendingIntent类的方法来发送短信。下面我发布我的代码以发送短信: -

  private void sendSMS(String phoneNumber, String message)
    {        
        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";

        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
            new Intent(SENT), 0);

        PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
            new Intent(DELIVERED), 0);

        //---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS sent", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case android.telephony.SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case android.telephony.SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case android.telephony.SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case android.telephony.SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off", 
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }, new IntentFilter(SENT));

        //---when the SMS has been delivered---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS delivered", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(getBaseContext(), "SMS not delivered", 
                                Toast.LENGTH_SHORT).show();
                        break;                        
                }
            }
        }, new IntentFilter(DELIVERED));        

        android.telephony.SmsManager sms = android.telephony.SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);        
    }
于 2012-07-16T05:36:48.630 回答
0

试试这个 smsintent.setData(Uri.parse("sms:"));

于 2012-07-16T02:58:30.780 回答