嗨,我创建了一个SMS应用程序。在这个应用程序中,我提供电话号码和消息。当我单击发送按钮时。消息仅从 SIM2 发送。如何在发送消息之前选择特定的 SIM 卡,并且我想自动发送短信,请帮助我..
这是我的java文件
MainActivity.java
package com.example.message;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity
{
Button snd;
EditText no;
EditText msg;
String message,phoneno;
protected Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
snd=(Button)findViewById(R.id.button1);
no=(EditText)findViewById(R.id.editText1);
msg=(EditText)findViewById(R.id.editText2);
final SmsManager smsManager = SmsManager.getDefault();
snd.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
phoneno=String.valueOf(no.getText());
message=String.valueOf(msg.getText());
try
{
smsManager.sendTextMessage(phoneno,null,message,null,null);
Toast.makeText(context, "SMS Sent to " + phoneno,Toast.LENGTH_LONG).show();
}
catch (Exception e) {
Toast.makeText(context,"SMS faild, please try again later!",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是我的 XML 文件
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="140dp"
android:layout_marginLeft="38dp"
android:text="@string/btn" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:text="@string/title"
android:textSize="@dimen/tsize" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/editText2"
android:layout_alignLeft="@+id/button1"
android:layout_marginBottom="24dp"
android:ems="10"
android:inputType="number" >
<requestFocus /></EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="@dimen/msgheight"
android:layout_above="@+id/button1"
android:layout_alignLeft="@+id/editText1"
android:layout_marginBottom="60dp"
android:ems="10"
android:inputType="text"
android:maxLines="10"
android:scrollHorizontally="true" >
</EditText>
</RelativeLayout>
这是我的清单文件
消息 Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.message"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.message.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>