在片段中发送sms应用程序
我正在使用碎片创建一个android。我已经创建了一个短信碎片,用于向特定号码发送短信,但当它运行良好时,但当我键入消息然后按send时,应用程序崩溃并关闭。帮帮我在片段中发送sms应用程序,sms,Sms,我正在使用碎片创建一个android。我已经创建了一个短信碎片,用于向特定号码发送短信,但当它运行良好时,但当我键入消息然后按send时,应用程序崩溃并关闭。帮帮我 public class SMS extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Overr
public class SMS extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// View v = inflater.inflate(R.layout.fragment, null);
View v = inflater.inflate(R.layout.sms, container, false);
return v;
}
public void sendsms(View v)
{
//refernce the edittext
EditText message=(EditText) v.findViewById(R.id.Messege);
//get the phone the number and the message
String number="0712345678";
String msg=message.getText().toString();
//use the sms manager to send message
SmsManager sm=SmsManager.getDefault();
sm.sendTextMessage(number, null, msg, null, null);
Toast.makeText(getActivity(),"Messege sent",Toast.LENGTH_LONG).show();
}
}
下面是我使用的xml布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="@drawable/log"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send your feedback" />
<EditText
android:id="@+id/Messege"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:hint="Enter your messege"
android:layout_marginTop="15dp"
android:ems="10" >
</EditText>
<Button
android:id="@+id/send"
android:onClick="sendsms"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send SMS" />
</LinearLayout>
onClick()方法在UI线程中运行。您不想从UI线程发送SMS消息。尝试将sendSMS代码包装到AsyncTask中,或创建一个新线程来运行该代码