2015-10-28 129 views
-2

我通過android應用向用戶發送OTP短信。對於發送短信,我正在使用SMS頻道。所以我只是打網址發送短信。我希望我的應用在SMS發送給用戶時得到通知,以便我可以自動讀取該OTP。在Android中通過短信通道獲取SMS短信發送通知

+0

這個問題是相當堆棧溢出過於簡單,在我看來 - 我們通常喜歡看一些研究,或者一些代碼的開始。 Android文檔有何說法? (另外,請儘量避免在你的問題中提出緊急要求 - 這裏幫助的人是志願者,他們會在閒暇時回答)。 – halfer

回答

0

請參閱參考 -

main.xml 


<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<Button 
    android:id="@+id/btnSendSMS" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Send SMS" /> 
</LinearLayout> 

SMS1Activity.java

package selva.sms; 

import android.app.Activity; 
import android.app.PendingIntent; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.telephony.gsm.SmsManager; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 
public class SMS1Activity extends Activity 
{ 
Button btnSendSMS; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState); 

setContentView(R.layout.main); 
btnSendSMS = (Button) findViewById(R.id.btnSendSMS); 
btnSendSMS.setOnClickListener(new View.OnClickListener() 
{ 
public void onClick(View v) 
{ 
sendSMS("5556", "Hi You got a message!"); 
} 
}); 
} 
//---sends an SMS message to another device--- 

private void sendSMS(String phoneNumber, String message) 
{ 
String SENT = "SMS_SENT"; 
String DELIVERED = "SMS_DELIVERED"; 
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, 
new Intent(SENT), 0); 
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,new Intent(DELIVERED), 0); 
//---when the SMS has been sent--- 
registerReceiver(new BroadcastReceiver(){ 
@Override 
public void onReceive(Context arg0, Intent arg1) { 
switch (getResultCode()) 
{ 
case Activity.RESULT_OK: 
Toast.makeText(getBaseContext(), "SMS sent", 
Toast.LENGTH_SHORT).show(); 
break; 
case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
Toast.makeText(getBaseContext(), "Generic failure", 
Toast.LENGTH_SHORT).show(); 
break; 
case SmsManager.RESULT_ERROR_NO_SERVICE: 
Toast.makeText(getBaseContext(), "No service", 
Toast.LENGTH_SHORT).show(); 
break; 
case SmsManager.RESULT_ERROR_NULL_PDU: 
Toast.makeText(getBaseContext(), "Null PDU", 
Toast.LENGTH_SHORT).show(); 
break; 
case SmsManager.RESULT_ERROR_RADIO_OFF: 
Toast.makeText(getBaseContext(), "Radio off", 
Toast.LENGTH_SHORT).show(); 
break; 
} 
} 
}, new IntentFilter(SENT)); 
//---when the SMS has been delivered--- 
registerReceiver(new BroadcastReceiver(){ 
@Override 
public void onReceive(Context arg0, Intent arg1) { 
switch (getResultCode()) 
{ 
case Activity.RESULT_OK: 
Toast.makeText(getBaseContext(), "SMS delivered", 
Toast.LENGTH_SHORT).show(); 
break; 
case Activity.RESULT_CANCELED: 
Toast.makeText(getBaseContext(), "SMS not delivered", 
Toast.LENGTH_SHORT).show(); 
break; 
} 
} 
}, new IntentFilter(DELIVERED)); 
SmsManager sms = SmsManager.getDefault(); 
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); 
} 
} 

cortsey-代碼androidprogramz.blogspot

+0

正如我所說我使用短信通道而不是短信管理器。我只需要打一個網址到短信。 –