[英]Sending SMS Message in Android Eclipse
我正在創建一個簡單的應用程序,每次用戶打開該應用程序時都會發送短信,但我無法發送預配對消息。 這是我的代碼:
MainActivity.java:
//The contact number is save in a number.text, so i get it first
FileInputStream fIn = null;
InputStreamReader isr = null;
String number = null;
try{
char[] inputBuffer = new char[1024];
String data = null;
fIn = openFileInput("number.txt");
isr = new InputStreamReader(fIn);
isr.read(inputBuffer);
data = new String(inputBuffer);
number = data;
isr.close();
fIn.close();
if(number == null || number.length() < 10){
//Do nothing
}else{
smsBody = "Hello World!";
try {
// Get the default instance of the SmsManager
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number, null, smsBody, null, null);//Im not able to send this.
Toast.makeText(getApplicationContext(), "Message Sent to " + number + ".",
Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Toast.makeText(getApplicationContext(),"Sending message failed!",
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
}catch(IOException e){
//Do nothing
}
我已經在manifiedt文件中設置了權限。 這是我的許可代碼。
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
//Im adding some features so i need this
<uses-permission android:name="android.permission.RECEIVE_MMS"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
吐司消息顯示該消息已經發送,但是我無法接收。 但是當我將MainActivity.java
更改為這樣時:
//The contact number is save in a number.text, so i get it first
FileInputStream fIn = null;
InputStreamReader isr = null;
String number = null;
try{
char[] inputBuffer = new char[1024];
String data = null;
fIn = openFileInput("number.txt");
isr = new InputStreamReader(fIn);
isr.read(inputBuffer);
data = new String(inputBuffer);
number = data;
isr.close();
fIn.close();
if(number == null || number.length() < 10){
//Do nothing
}else{
smsBody = "Hello World!";
try {
// Get the default instance of the SmsManager
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("48612156715", null, smsBody, null, null);//I change this from smsManager.sendTextMessage(number, null, smsBody, null, null); where the 48612115 is my example number.
Toast.makeText(getApplicationContext(), "Message Sent to " + number + ".",
Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Toast.makeText(getApplicationContext(),"Sending message failed!",
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
}catch(IOException e){
//Do nothing
}
現在,我可以接收到消息了,我需要先獲取number.text文件的值,然后將其放入變量中,然后將其用作數字,但它不起作用。
可以是:
您的硬編碼號碼48612115有8位數字,並且您要檢查的數字是否少於10,並且在if
塊中什么也不做。
if(number == null || number.length() < 10)
{
// do nothing
}
else
{
// send message
}
您應該檢查語法是否正確。
希望能幫助到你!
好的,感謝大家,我為自己的問題找到了解決方案。 :-)這是代碼:
String phoneNumber = "";
String smsBody = "Example Message";
try {
InputStream instream = new FileInputStream(
"data/data/com.example.test/files/number.txt");
if (instream != null) {
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
if ((line = buffreader.readLine()) != null)
phoneNumber = line;
try {
// Get the default instance of the SmsManager
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, smsBody, null, null);
Toast.makeText(getApplicationContext(), "Message Sent to " + phoneNumber + ".",
Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Toast.makeText(getApplicationContext(),"Sending Message failed!",
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
instream.close();
}
} catch (IOException e) {
Toast.makeText(getApplicationContext(),e,
Toast.LENGTH_LONG).show();
}
謝謝大家!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.