0
嗨,我創建一個短信 Application.in此應用程序我給電話號碼和message.when我單擊發送按鈕。該消息由SIM2發送only.how我該選擇特定的SIM之前發送的消息,我也想自動發送短信,請幫助我..如何發送短信從特定的SIM在Android
這是我的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文件
activit y_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>