0

我已经成功地将一些代码组合在一起,将 SMS 发送到另一个包含两个字段Phone NoMessage Body. 但是,我希望创建一个包含具有 5 个文本字段的表单的应用程序。

例如:

Field 1 : Time and Date
Field 2 : Pick Up
Field 3 : Destination
Field 4 : Name 
Field 5 : Car Type

但是每次我尝试增加一个字符串并将其作为参数传递给函数时 - 它都会引发错误。我会错过什么?我的代码:

package com.example.taxiappnew;


import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class SmsMain extends Activity {

    Button btnSendSMS;
    EditText txtPhoneNo;
    EditText txtMessage;
    EditText txtPickup;
    String message1;

    /** Called when the activi|ty is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms_main);        

        btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
        txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
        txtMessage = (EditText) findViewById(R.id.txtMessage);
        txtPickup = (EditText) findViewById(R.id.txtPickup);

        btnSendSMS.setOnClickListener(new View.OnClickListener() 
        {


            public void onClick(View v) 
            {                
                String phoneNo = txtPhoneNo.getText().toString();
                String Pickup  = txtPickup.getText().toString();
                String message = txtMessage.getText().toString();  

                if (phoneNo.length()>0 && message.length()>0) 
                 if (Pickup.length()>0)
                  message1 = new StringBuilder().append("Pickup").append("message").toString();
                    sendSMS(phoneNo, message1);  

                    Toast.makeText(getBaseContext(), 
                        "Please enter both phone number and message.", 
                        Toast.LENGTH_SHORT).show();

            }
     }); 


    }

     //---sends an SMS message to another device---
    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 + phoneNumber", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case 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(){
            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));        



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

错误日志 ================================================= =====

12-08 17:11:33.870: E/SKIA(10331): FimgApiStretch:stretch failed
12-08 17:15:39.565: W/IInputConnectionWrapper(10331): showStatusIcon on inactive InputConnection
12-08 17:15:40.310: E/ActivityThread(10331): Activity com.example.taxiappnew.SmsMain has leaked IntentReceiver com.example.taxiappnew.SmsMain$3@41a43d88 that was originally registered here. Are you missing a call to unregisterReceiver()?
12-08 17:15:40.310: E/ActivityThread(10331): android.app.IntentReceiverLeaked: Activity com.example.taxiappnew.SmsMain has leaked IntentReceiver com.example.taxiappnew.SmsMain$3@41a43d88 that was originally registered here. Are you missing a call to unregisterReceiver()?
12-08 17:15:40.310: E/ActivityThread(10331):    at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:763)
12-08 17:15:40.310: E/ActivityThread(10331):    at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:567)
12-08 17:15:40.310: E/ActivityThread(10331):    at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1155)
12-08 17:15:40.310: E/ActivityThread(10331):    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1142)
12-08 17:15:40.310: E/ActivityThread(10331):    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1136)
12-08 17:15:40.310: E/ActivityThread(10331):    at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:348)
12-08 17:15:40.310: E/ActivityThread(10331):    at com.example.taxiappnew.SmsMain.sendSMS(SmsMain.java:106)
12-08 17:15:40.310: E/ActivityThread(10331):    at com.example.taxiappnew.SmsMain.access$0(SmsMain.java:64)
12-08 17:15:40.310: E/ActivityThread(10331):    at com.example.taxiappnew.SmsMain$1.onClick(SmsMain.java:51)
12-08 17:15:40.310: E/ActivityThread(10331):    at android.view.View.performClick(View.java:3644)
12-08 17:15:40.310: E/ActivityThread(10331):    at android.view.View$PerformClick.run(View.java:14313)
12-08 17:15:40.310: E/ActivityThread(10331):    at android.os.Handler.handleCallback(Handler.java:605)
12-08 17:15:40.310: E/ActivityThread(10331):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-08 17:15:40.310: E/ActivityThread(10331):    at android.os.Looper.loop(Looper.java:137)
12-08 17:15:40.310: E/ActivityThread(10331):    at android.app.ActivityThread.main(ActivityThread.java:4517)
12-08 17:15:40.310: E/ActivityThread(10331):    at java.lang.reflect.Method.invokeNative(Native Method)
12-08 17:15:40.310: E/ActivityThread(10331):    at java.lang.reflect.Method.invoke(Method.java:511)
12-08 17:15:40.310: E/ActivityThread(10331):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
12-08 17:15:40.310: E/ActivityThread(10331):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
12-08 17:15:40.310: E/ActivityThread(10331):    at dalvik.system.NativeStart.main(Native Method)
12-08 17:15:40.320: E/ActivityThread(10331): Activity com.example.taxiappnew.SmsMain has leaked IntentReceiver com.example.taxiappnew.SmsMain$2@41a436e0 that was originally registered here. Are you missing a call to unregisterReceiver()?
12-08 17:15:40.320: E/ActivityThread(10331): android.app.IntentReceiverLeaked: Activity com.example.taxiappnew.SmsMain has leaked IntentReceiver com.example.taxiappnew.SmsMain$2@41a436e0 that was originally registered here. Are you missing a call to unregisterReceiver()?
12-08 17:15:40.320: E/ActivityThread(10331):    at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:763)
12-08 17:15:40.320: E/ActivityThread(10331):    at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:567)
12-08 17:15:40.320: E/ActivityThread(10331):    at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1155)
12-08 17:15:40.320: E/ActivityThread(10331):    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1142)
12-08 17:15:40.320: E/ActivityThread(10331):    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1136)
12-08 17:15:40.320: E/ActivityThread(10331):    at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:348)
12-08 17:15:40.320: E/ActivityThread(10331):    at com.example.taxiappnew.SmsMain.sendSMS(SmsMain.java:76)
12-08 17:15:40.320: E/ActivityThread(10331):    at com.example.taxiappnew.SmsMain.access$0(SmsMain.java:64)
12-08 17:15:40.320: E/ActivityThread(10331):    at com.example.taxiappnew.SmsMain$1.onClick(SmsMain.java:51)
12-08 17:15:40.320: E/ActivityThread(10331):    at android.view.View.performClick(View.java:3644)
12-08 17:15:40.320: E/ActivityThread(10331):    at android.view.View$PerformClick.run(View.java:14313)
12-08 17:15:40.320: E/ActivityThread(10331):    at android.os.Handler.handleCallback(Handler.java:605)
12-08 17:15:40.320: E/ActivityThread(10331):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-08 17:15:40.320: E/ActivityThread(10331):    at android.os.Looper.loop(Looper.java:137)
12-08 17:15:40.320: E/ActivityThread(10331):    at android.app.ActivityThread.main(ActivityThread.java:4517)
12-08 17:15:40.320: E/ActivityThread(10331):    at java.lang.reflect.Method.invokeNative(Native Method)
12-08 17:15:40.320: E/ActivityThread(10331):    at java.lang.reflect.Method.invoke(Method.java:511)
12-08 17:15:40.320: E/ActivityThread(10331):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
12-08 17:15:40.320: E/ActivityThread(10331):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
12-08 17:15:40.320: E/ActivityThread(10331):    at dalvik.system.NativeStart.main(Native Method)
12-08 17:15:40.340: D/dalvikvm(10331): GC_CONCURRENT freed 254K, 4% free 13896K/14471K, paused 2ms+2ms
12-08 17:15:45.470: D/CLIPBOARD(11150): Hide Clipboard dialog at Starting input: finished by someone else... !
12-08 17:16:05.600: E/SKIA(11150): FimgApiStretch:stretch failed
4

0 回答 0