Java Android短信应用程序意外调用默认值;新讯息;单击按钮时来自消息传递应用程序的布局

Java Android短信应用程序意外调用默认值;新讯息;单击按钮时来自消息传递应用程序的布局,java,android,xml,sms,android-activity,Java,Android,Xml,Sms,Android Activity,制作一个短信应用程序,目前进展顺利,但昨晚在尝试实施一项新活动后遇到了问题 当我点击版面上的“发送”按钮时,它会发送短信,并触发一个祝酒词,通知我发送的短信。在我尝试将onClick属性添加到XML中之前,它一直工作得很好,这导致了启动新活动的意图。。。就在那时,这种情况开始发生: 它没有启动我创建的新活动,而是将我发送到手机附带的默认“消息”应用程序的“新消息”布局/活动。我不明白这一点,所以拿走了(除非我一次又一次地错过了一些东西)我在工作时改变的一切 然而:当我点击这个按钮时,它仍然在发送

制作一个短信应用程序,目前进展顺利,但昨晚在尝试实施一项新活动后遇到了问题

当我点击版面上的“发送”按钮时,它会发送短信,并触发一个祝酒词,通知我发送的短信。在我尝试将onClick属性添加到XML中之前,它一直工作得很好,这导致了启动新活动的意图。。。就在那时,这种情况开始发生:

它没有启动我创建的新活动,而是将我发送到手机附带的默认“消息”应用程序的“新消息”布局/活动。我不明白这一点,所以拿走了(除非我一次又一次地错过了一些东西)我在工作时改变的一切

然而:当我点击这个按钮时,它仍然在发送我到那个新的消息活动

有人能告诉我在我的代码中我告诉应用程序启动该活动的位置吗?谢谢大家!

TextActivity.java

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class TextActivity extends Activity {

Button buttonSend;
EditText textPhoneNo;
EditText textSMS;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.text_field);

    buttonSend = (Button) findViewById(R.id.bSendText);
    textPhoneNo = (EditText) findViewById(R.id.etPhoneNumber);
    textSMS = (EditText) findViewById(R.id.etTypeMessage);

    buttonSend.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            String phoneNo = textPhoneNo.getText().toString();
            String sms = textSMS.getText().toString();

            Intent sendIntent = new Intent(Intent.ACTION_VIEW);
            sendIntent.putExtra("SMS Body", sms);
            sendIntent.setType("vnd.android-dir/mms-sms");
            startActivity(sendIntent);

            try {
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(phoneNo, null, sms, null, null);
                Toast.makeText(getApplicationContext(), "SMS Sent!",
                        Toast.LENGTH_LONG).show();

            } catch (Exception e) {
                Toast.makeText(getApplicationContext(),
                        "SMS failed, please try again later!",
                        Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
        }
    });
}

// STARTING The Contact Selection Process
private static final int CONTACT_PICKER_RESULT = 1001;

// References the Select Contact Button with onClick="clickHandle"
public void clickHandle(View view) {
    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
            Contacts.CONTENT_URI);
    startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}

// Interpreting the results from the Contact_picker
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
        case CONTACT_PICKER_RESULT:
            Cursor cursor = null;
            String phone = "";
            String name = "";
            try {
                Uri result = data.getData();

                // get the contact id from the Uri
                String id = result.getLastPathSegment();

                // query for everything phone
                cursor = getContentResolver().query(Phone.CONTENT_URI,
                        null, Phone.CONTACT_ID + "=?", new String[] { id },
                        null);

                int phoneIdx = cursor.getColumnIndex(Phone.DATA);
                int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);

                // let's just get the first phone
                if (cursor.moveToFirst()) {
                    phone = cursor.getString(phoneIdx);
                    name = cursor.getString(nameIdx);

                } else {

                }
            } catch (Exception e) {

            } finally {
                if (cursor != null) {
                    cursor.close();
                }
                EditText phoneEntry = (EditText) findViewById(R.id.etPhoneNumber);
                phoneEntry.setText(phone);

                TextView nameEntry = (TextView) findViewById(R.id.tvContactSelected);
                nameEntry.setText(name);

                if (phone.length() == 0) {
                    Toast.makeText(this, "No phone found for contact.",
                            Toast.LENGTH_LONG).show();
                }
            }
            break;
        }

    } else {

    }
}
}
我的XML,text_field.XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@color/burntorange" >

    <Button
        android:id="@+id/bSelectContact"
        android:layout_width="50dp"
        android:layout_height="45dp"
        android:layout_gravity="center"
        android:layout_margin="4dp"
        android:background="@color/gray"
        android:inputType="phone"
        android:onClick="clickHandle"
        android:text="+"
        android:textColor="@color/white"
        android:textSize="35dp" />

    <TextView
        android:id="@+id/tvContactSelected"
        android:layout_width="290dp"
        android:layout_height="45dp"
        android:layout_gravity="top"
        android:layout_margin="4dp"
        android:background="@color/gray"
        android:gravity="center"
        android:padding="10dp"
        android:text="No Contact Selected"
        android:textSize="20dp" />
</LinearLayout>

<EditText
    android:id="@+id/etPhoneNumber"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="2dp"
    android:gravity="center"
    android:hint="Person&apos;s Phone Number"
    android:padding="10dp" >
</EditText>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_margin="1dp"
        android:background="@color/burntorange"
        android:descendantFocusability="beforeDescendants"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="1dp" >

        <ScrollView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/svEnterMessage"
            android:layout_width="269dp"
            android:layout_height="wrap_content"
            android:paddingBottom="4dp" >

            <EditText
                android:id="@+id/etTypeMessage"
                android:layout_width="260dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="2dp"
                android:layout_marginRight="4dp"
                android:layout_marginTop="4dp"
                android:background="@drawable/outline"
                android:hint="Type Message Here"
                android:minHeight="35dp"
                android:padding="6dp"
                android:textColor="@color/white"
                android:inputType="textMultiLine" >"
            </EditText>
        </ScrollView>

        <Button
            android:id="@+id/bSendText"
            android:layout_width="75dp"
            android:layout_height="36dp"
            android:layout_marginBottom="2dp"
            android:layout_marginLeft="2dp"
            android:background="@color/gray"
            android:text="Send"
            android:layout_gravity="bottom"
            android:textColor="@color/white" />

    </LinearLayout>

  </RelativeLayout>

</LinearLayout>

"
有人能告诉我在我的代码中我告诉应用程序启动该活动的位置吗

这看起来像是启动了默认的消息传递应用程序:

Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("SMS Body", sms);
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);

如果你不想加载默认应用程序,你想在这里做什么?

好吧,我想我正在使用默认应用程序,但在我单击“发送”之前。它使用应用程序发送,但实际上从未将我带到屏幕上。它只是发送,说“发送短信”,然后就是这样。但无法理解为什么它会改变。(显然仍然是新的:P)好的,
try{}
块中的代码看起来应该立即发送SMS。所以我的问题是:为什么您要同时执行这两个操作?(也许您只是想删除
sendIntent
代码?)哦,我的…。是的。就是这样。只是必须删除它。一定是在尝试所有东西时添加了这个意图。应该是显而易见的哈哈。好吧,谢谢你,山姆。我在这方面正在慢慢进步。