我在尝试发送邮件时收到错误消息

时间:2012-12-21 10:33:41

标签: android

send.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    //URI uri=uri.parse("sms://+919500518057");
    sendSms("9500518057","message");
    //startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("sms:"+ "+919500518057")));
    }

    private void sendSms(String phno, String message) {
        // TODO Auto-generated method stub
        Log.v("PhoneNumber",phno);
        Log.v("MEssage", message);
        PendingIntent pi=PendingIntent.getActivity(this, 0, new Intent(this, Object.class), 0);
        SmsManager sms=SmsManager.getDefault();
        sms.sendTextMessage(phno, null, message, pi, null);
    }
});

在清单文件中:

<uses-permission android:name="android.permission.SEND_SMS"/>

我收到的错误如下:

PendingIntent类型中的方法getActivity(Context,int,Intent,int)不适用于参数(new View.OnClickListener(){},int,Intent,int) 在清单文件中,我输入以下代码

6 个答案:

答案 0 :(得分:1)

从您的主要方式调用此方法:

private void sendSMS() {
        SMSSend smsSend = new SMSSend();

        smsSend.execCommand();
    }

然后以下两个班级将负责发送短信。

public class SMSSend {

    private final String CLASS_NAME = this.getClass().getSimpleName();
    private final static String SEND_SMS_FAILURE = "Send SMS command executed and status is failure";
    private String phoneNumber;
    private String messageText;

    /**
     * constructor is defined.
     * 
     * @param CommandProcessor
     *            cmdProcessor
     */
    public SMSSend() {
        super();
    }

    /**
     * execCommand method
     * 
     * It is an overridden here and declared in CommandHandler (base class)
     */
    public void execCommand() {
        try {
            new Thread() {
                public void run() {
                    sendSMS();
                }
            }.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /**
     * sendSMS method
     * 
     * It retrieves the command parameters and sends the sms.
     * 
     * @param Command
     *            command
     * @return Status
     */
    private void sendSMS() {
        try {
            SMSSendHelper smsHelper = new SMSSendHelper();
            phoneNumber = "0123456789";
            messageText = "Message Text smaple";
            if (null == phoneNumber || phoneNumber.length() < 2) {
            } else {
                smsHelper.sendSMS(phoneNumber, messageText);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }

    }

}




/**
 * SMSSend class
 * 
 * It is responsible for sending SMSs.
 *  
 */
public class SMSSendHelper {
    private final String CLASS_NAME = this.getClass().getSimpleName();
    private String SENT = "SMS_SENT";
    private String DELIVERED = "SMS_DELIVERED";
    private PendingIntent sentPI;
    private BroadcastReceiver sentReceiver;
    private BroadcastReceiver deliveredReceiver;
    private PendingIntent deliveredPI;
    private static final int SMS_LENGHT = 160;
    private static String status = "";

    /**
     * Constructor is defined
     * 
     * @param Status
     *            smsstatus
     */
    public SMSSendHelper() {
        sentPI = PendingIntent.getBroadcast(ConfigClass.getAppContext(), 0,
                new Intent(SENT), 0);

        deliveredPI = PendingIntent.getBroadcast(ConfigClass.getAppContext(),
                0, new Intent(DELIVERED), 0);
        sentReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {

                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    try {
                        synchronized (lock) {
                            lock.notifyAll();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    setSendSMSSataus("SMS Sent");
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    try {
                        synchronized (lock) {
                            lock.notifyAll();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    setSendSMSSataus("Generic failure");
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    try {
                        synchronized (lock) {
                            lock.notifyAll();
                        }
                    } catch (Exception e) {
                    }
                    setSendSMSSataus("No service");
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    try {
                        synchronized (lock) {
                            lock.notifyAll();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    setSendSMSSataus("Null PDU");
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    try {
                        synchronized (lock) {
                            lock.notifyAll();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    setSendSMSSataus("Radio off");
                    break;
                default:
                    try {
                        synchronized (lock) {
                            lock.notifyAll();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;

                }
                ConfigClass.getAppContext().unregisterReceiver(sentReceiver);
            }
        };
        // ---when the SMS has been sent---
        ConfigClass.getAppContext().registerReceiver(sentReceiver,
                new IntentFilter(SENT));

        deliveredReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    break;
                case Activity.RESULT_CANCELED:
                    break;
                }
                ConfigClass.getAppContext().unregisterReceiver(
                        deliveredReceiver);
            }
        };

        ConfigClass.getAppContext().registerReceiver(deliveredReceiver,
                new IntentFilter(DELIVERED));
    }

    /**
     * setSendSMSSataus method
     * 
     * @param setSendSMSSataus
     *            currentSataus
     */
    public static void setSendSMSSataus(String currentSataus) {
        status = currentSataus;

    }

    /**
     * getSendSMSSataus method
     * 
     * @return String status
     */
    public String getSendSMSSataus() {
        int i = sentReceiver.getResultCode();
        return status;
    }

    /**
     * sendSMS method
     * 
     * @param String
     *            phoneNumber
     * @param String
     *            message
     * @return Status
     */
    public void sendSMS(String phoneNumber, String message) {
        if (message == null) {
            message = " ";
        }

        SmsManager sms = SmsManager.getDefault();

        if (message.length() > SMS_LENGHT) {
            sendLongSMS(sms, message, phoneNumber);
        } else {
            Log.d("E++++D",phoneNumber+"======>>>>>SOS");

            sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
        }
        try {
            synchronized (lock) {
                lock.wait();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void sendLongSMS(SmsManager sms, String message, String phoneNumber) {
        ArrayList<String> parts = sms.divideMessage(message);
        int numParts = parts.size();
        ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
        ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
        for (int i = 0; i < numParts; i++) {
            sentIntents.add(sentPI);
            deliveryIntents.add(deliveredPI);
        }

        sms.sendMultipartTextMessage(phoneNumber, null, parts, sentIntents,
                deliveryIntents);
    }

    private Object lock = new Object();
}

答案 1 :(得分:0)

你使用了错误的this。交换这个:

PendingIntent pi=PendingIntent.getActivity(this, 0, new Intent(this, Object.class), 0);

为此:

PendingIntent pi=PendingIntent.getActivity(YourActivity.this, 0, new Intent(YourActivity.this, Object.class), 0);

现在你正在使用OnClickListener.this。 :)

答案 2 :(得分:0)

尝试使用:

PendingIntent pi=PendingIntent.getActivity(mContext, 0, new Intent(mContext, Object.class), 0);

在你的全局变量中添加:

private Context mContext;

在超级调用后使用:

在onCreate()中初始化它
mContext = this;

在创建this时仅使用PendingIntent,您不是指Activity的当前实例。您指的是OnClickListener内部类

的当前实例

答案 3 :(得分:0)

我用它来发送消息: -

private void sendSMS(String phoneNumber, String message)
{        

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, null, null);     
} 

答案 4 :(得分:0)

PendingIntent需要Context作为第一个参数。在活动中,您可以使用PendingIntent.getActivity(this, ..)将活动上下文传递给方法。

然而,这在setOnClickListener中不起作用(就像你一样)。 OnclickListener this内部引用setOnClickListener(不是有效的上下文对象),而不是活动。

如果您想将活动传递给getActivity,可以使用PendingIntent.getActivity(<NameOfYourActivity>.this, ..)

e.g。 PendingIntent.getActivity(MyActivity.this, ..)

答案 5 :(得分:0)

private void sendSMS(String phoneNumber, String message)
    {        
        PendingIntent pi = PendingIntent.getActivity(this, 0,
            new Intent(this, SMS.class), 0);                
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, pi, null);        
    }