我对如何判断是否已从设备发送短信感兴趣。
为了在收到短信时收到通知,我们使用广播公司:
android.provider.Telephony.SMS_RECEIVED
重要的是,我不会从我的应用程序发送短信,我应该在设备发送短信时收听。
可能我应该听一些内容提供商(以某种方式与SMS相关)并对此更改作出反应。有什么想法我能做到吗?
答案 0 :(得分:16)
是的,可以使用 ContentObserver
以下是我的外发短信示例:
首先使用 content:// sms /
注册ContetObserver public class Smssendservice extends Service{
@Override
public void onCreate() {
SmsContent content = new SmsContent(new Handler());
// REGISTER ContetObserver
this.getContentResolver().
registerContentObserver(Uri.parse("content://sms/"), true, SMSObserver);
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
<强> SMSObserver.class 强>
public class SMSObserver extends ContentObserver {
private Handler m_handler = null;
public SMSObserver(SMSLogger handler){
super(handler);
m_handler = handler;
}
@Override
public void onChange(boolean selfChange) {
super.onChange(bSelfChange);
Uri uriSMSURI = Uri.parse("content://sms");
Cursor cur = this.getContentResolver().query(uriSMSURI, null, null,
null, null);
cur.moveToNext();
String protocol = cur.getString(cur.getColumnIndex("protocol"));
if(protocol == null) {
//the message is sent out just now
}
else {
//the message is received just now
}
}
}
}
答案 1 :(得分:7)
使用以下方法发送短信以及检查短信是否被传送。
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String phoneNo = "Phone number to sent";
String message = "Your message";
if (phoneNo.length() > 0 && message.length() > 0) {
TelephonyManager telMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
int simState = telMgr.getSimState();
switch (simState) {
case TelephonyManager.SIM_STATE_ABSENT:
displayAlert();
break;
case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
// do something
break;
case TelephonyManager.SIM_STATE_PIN_REQUIRED:
// do something
break;
case TelephonyManager.SIM_STATE_PUK_REQUIRED:
// do something
break;
case TelephonyManager.SIM_STATE_READY:
// do something
sendSMS(phoneNo, message); // method to send message
break;
case TelephonyManager.SIM_STATE_UNKNOWN:
// do something
break;
}
} else {
Toast.makeText(getBaseContext(),
"Please enter both phone number and message.",
Toast.LENGTH_SHORT).show();
}
}
private void displayAlert() {
// TODO Auto-generated method stub
new AlertDialog.Builder(YourActivity.this)
.setMessage("Sim card not available")
.setCancelable(false)
// .setIcon(R.drawable.alert)
.setPositiveButton("ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
Log.d("I am inside ok", "ok");
dialog.cancel();
}
})
.show();
}
});
private void sendSMS(String phoneNumber, String message) {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(YourActivity.this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(YourActivity.this,
0, new Intent(DELIVERED), 0);
// ---when the SMS has been sent---
final String string = "deprecation";
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(YourActivity.this, "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(YourActivity.this, "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(YourActivity.this, "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(YourActivity.this, "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(YourActivity.this, "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(YourActivity.this, "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
答案 2 :(得分:0)
我想到的一件事是使用logcat日志。每次发送/接收SMS时都有一个特定的条目。只需在调试和测试中连接手机,您就会看到具体的条目。
我应该提到这个方法是从您的应用程序完成的,因此您可以将其用于您的代码。
拉一下this。可能还有其他方法,但这似乎相当容易和可行。