在Android中通过短信通道获取SMS短信发送通知

问题描述:

我通过android应用向用户发送OTP短信。对于发送短信,我正在使用SMS频道。所以我只是打网址发送短信。我希望我的应用在SMS发送给用户时得到通知,以便我可以自动读取该OTP。在Android中通过短信通道获取SMS短信发送通知

+0

这个问题是相当堆栈溢出过于简单,在我看来 - 我们通常喜欢看一些研究,或者一些代码的开始。 Android文档有何说法? (另外,请尽量避免在你的问题中提出紧急要求 - 这里帮助的人是志愿者,他们会在闲暇时回答)。 – halfer

请参阅参考 -

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

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