发送短信时包含食谱名称或微调器ID的Toast消息

时间:2012-09-28 04:02:06

标签: android sms messaging toast android-spinner

我的应用程序是用户发送祝福消息的地方。它有Spinner,其中包含配方'名字和他们的硬编码。我创建了一个Toast消息说" SMS已成功发送"。但我意识到我需要在Toast消息中包含某个食谱名称,以确认消息是否已发送给配方。

我已尝试过任何我知道的Toast消息,试图包含食谱的名称,但是当我运行应用程序时,我收到错误并且Toast消息失败。我将发布短信应用的完整代码。

public class SMSEvents extends Activity
{
    Button btnSMS;
    EditText messageTxt;
    IntentFilter intentFilter;
    TextView text, textName;
    Spinner sendTo;

    ArrayAdapter<String> adapter;
    String friends[] = {"Kanak Priya", "Joanne Liew", "Melissa Haiting", "Michelle Lam", "Teo Kin Hua", "David Yeo", "Nur Ashiqin", "Stephanie"};

    private int namesSpinnderId;

    private BroadcastReceiver intentReceiver = new BroadcastReceiver()
        {
            @Override
            public void onReceive(Context context, Intent intent)
            {
                // ---display the SMS received in the TextView---
                TextView text = (TextView) findViewById(R.id.text);
                text.setText(intent.getExtras().getString("sms"));                          
            }                       
    };

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

        sendTo = (Spinner) findViewById(R.id.sendTo);

        adapter = new ArrayAdapter<String>(this,
                R.layout.spinner_text, friends);
        sendTo.setAdapter(adapter);





        /*BuddyDBAdapter buddyDB = new BuddyDBAdapter(this);

        sendTo = (Spinner) findViewById(R.id.sendTo);
        Cursor friendsCursor = buddyDB.getAllNames();
        startManagingCursor(friendsCursor);*/

        /*String[] from = new String[]{BuddyDBAdapter.KEY_NAME};
        int[] to = new int[]{R.id.sendTo};

        SimpleCursorAdapter friendsAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, friendsCursor, from, to);
        friendsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sendTo.setAdapter(friendsAdapter);*/

       /* sendTo.setOnItemSelectedListener(new OnItemSelectedListener()
            {

                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
                {
                    Cursor c = (Cursor)parent.getItemAtPosition(pos);
                    namesSpinnderId = c.getInt(c.getColumnIndexOrThrow(BuddyDBAdapter.KEY_ROWID));                  
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0)
                {
                    // TODO Auto-generated method stub

                }

            });*/

        // ---intent to filter for SMS messages received---
        intentFilter = new IntentFilter();
        intentFilter.addAction("SMS_RECEIVED_ACTION");

        // ---register the receiver---
        registerReceiver(intentReceiver, intentFilter);

        //sendTo = (EditText) findViewById(R.id.sendTo);
        //textName = (TextView) findViewById(R.id.textName);


        sendTo = (Spinner) findViewById(R.id.sendTo);
        messageTxt = (EditText) findViewById(R.id.messageTxt);

        btnSMS = (Button) findViewById(R.id.btnSMS);
        btnSMS.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v)
                {
                    String contact_name = sendTo.getSelectedItem().toString();
                    String message = messageTxt.getText().toString();

                    if (contact_name.length() > 0 && message.length() > 0)
                        sendSMS(contact_name, message);
                    else
                        Toast.makeText(
                                getBaseContext(),
                                "Please enter your message",
                                Toast.LENGTH_SHORT).show();

                    /*Intent msgIntent = new Intent(android.content.Intent.ACTION_VIEW);
                    msgIntent.putExtra("sms_body", "default content");
                    msgIntent.setType("vnd.android-dir/mms-sms");
                    startActivity(msgIntent);*/
                }

            });

        Button btnBack = (Button) findViewById(R.id.btnMain);
        btnBack.setOnClickListener(new View.OnClickListener()
            {

                @Override
                public void onClick(View arg0)
                {
                    Intent menuIntent = new Intent(SMSEvents.this, MainPage.class);
                    startActivity(menuIntent);

                }
            });


    }




    @Override
    protected void onResume()
    {
        // ---register the receiver---
        // registerReceiver(intentReceiver, intentFilter);
        super.onResume();
    }

    @Override
    protected void onPause()
    {
        // ---unregister the receiver---
        // unregisterReceiver(intentReceiver);
        super.onPause();
    }

    @Override
    protected void onDestroy()
    {
        // ---unregister the receiver---
        unregisterReceiver(intentReceiver);
        super.onPause();
    }

    private void sendSMS(String contact_name, 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 is sent to" +contact_name successfully",
                                    Toast.LENGTH_SHORT).show();
                            break;

                        case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                            Toast.makeText(getBaseContext(), "Service 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()
            {
                @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));

        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage("5554", null, message, sentPI, deliveredPI);
        //sms.sendTextMessage(contact_name, null, message, sentPI, deliveredPI);

        /*try
        {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(contact_name, null, message, null, null);
            Toast.makeText(getApplicationContext(), "SMS is sent successfully!", Toast.LENGTH_LONG).show();
        }
        catch(Exception e)
        {
            Toast.makeText(getApplicationContext(), "SMS failed! Please try again later!", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }*/
    }
}

{I}尝试做的Toast.makeText(getBaseContext(), "SMS is sent to" +contact_name successfully", Toast.LENGTH_SHORT).show();包含配方名称。

如果有人知道如何做这个吐司,我会很感激=)。很抱歉要求一个简单的帮助,但我尝试了3种不同的方式来做这种Toast消息。

提前致谢! =)

1 个答案:

答案 0 :(得分:0)

我已经想出如何做这个Toast。

您在public void onReceive() private void sendSMS()

范围内对此Toast消息进行编码 像这样:

Toast.makeText(getBaseContext(), "SMS is sent to " +sendTo.getSelectedItem()+ " successfully.", Toast.LENGTH_SHORT).show();

getSelectedItem()正在从Spinner获取所选项目。如果您使用的是EditText或TextView,只需将getSelectedItem()替换为getText()

即可

:D,那些与我有类似问题的人,可以尝试这个答案,并希望它能解决他们类似的问题。 :d