在Android中创建SMS应用程序?
我正在创建一个短信应用程序来发送和接收短信 我可以使用以下代码发送短信:在Android中创建SMS应用程序?,android,sms,Android,Sms,我正在创建一个短信应用程序来发送和接收短信 我可以使用以下代码发送短信: SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null,message , pi, null); 我想收到短信,并把它们放在我自己的收件箱。如何创建此收件箱?我想它的工作方式一样,作为一个正常的收件箱 Bundle bundle = intent.getExtras(); Object[] pdus = (
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null,message , pi, null);
我想收到短信,并把它们放在我自己的收件箱。如何创建此收件箱?我想它的工作方式一样,作为一个正常的收件箱
Bundle bundle = intent.getExtras();
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < messages.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
Log.v("SMSFun","Body: " + messages[i].getDisplayMessageBody());
Log.v("SMSFun","Address: " + messages[i].getDisplayOriginatingAddress());
//If say we wanted to do something based on who sent it
if (messages[i].getDisplayOriginatingAddress().contains("5556")) {
// we could launch an activity and pass the data
Intent newintent = new Intent(ctx, SecretMessage.class);
newintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
newintent.putExtra("address", messages[i].getDisplayOriginatingAddress());
newintent.putExtra("message", messages[i].getDisplayMessageBody());
ctx.startActivity(newintent);
}
}
Bundle=intent.getExtras();
Object[]pdus=(Object[])bundle.get(“pdus”);
SmsMessage[]messages=新SmsMessage[pdus.length];
for(int i=0;i
如何在收件箱中存储收到的短信
在安卓系统中是否可以监听短信的特定端口号?您需要做的是注册一个
BroadcastReceiver
对象。查看文章以了解更多信息
如果您想从主收件箱中隐藏SMS消息,则需要从SMS ContentProvider中删除它们,并使用您自己的SQLite数据库来存储它们。还要确保在内容提供程序中将它们标记为已读,以从托盘中删除通知 我不认为你可以将短信放在不同的收件箱中,也不需要监听端口来获取短信,而是使用
广播接收器
我建议您通过开源应用程序更好地了解sms的工作原理 U可以使用SMSMAnager类发送和接收消息。
您可以实现自定义接收器,在收到消息时,它将通知用户消息已到达。。
在这里,我附上代码,我写了发送和接收消息使用自定义广播接收器,它可能会有用的美国。
注意:这是针对上面版本1.6的。因此,请确保您最好在2.0或2.2中执行此操作
仔细检查并尝试实施
公共类短信扩展活动{
Button btnSendSMS;
EditText txtPhoneNo;
EditText txtMessage;
Button addcontact;
EditText phonePhoneno;
private static final int CONTACT_PICKER_RESULT = 1001;
private static final String DEBUG_TAG = "";
String phoneNo="";
String phonenofromcontact="";
String finallistofnumberstosendmsg ="";
/** 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);
txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
txtMessage = (EditText) findViewById(R.id.txtMessage);
addcontact =(Button) findViewById(R.id.addphonenofromcontact);
addcontact.setOnClickListener(new View.OnClickListener()
{
public void onClick(View V)
{
Intent ContactPickerIntent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(ContactPickerIntent, CONTACT_PICKER_RESULT);
}
}
);
btnSendSMS.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String message = txtMessage.getText().toString();
phoneNo = txtPhoneNo.getText().toString();
String phoneNo1= phonePhoneno.getText().toString();
// Sending message to both the written and added contact...
finallistofnumberstosendmsg +=phoneNo1 + phoneNo;
String phoneFinal= phoneNo + finallistofnumberstosendmsg;
//StringTokenizer st=new StringTokenizer(finallistofnumberstosendmsg,",");
StringTokenizer st=new StringTokenizer(phoneFinal,",");
while (st.hasMoreElements())
{
String tempMobileNumber = (String)st.nextElement();
if(tempMobileNumber.length()>0 && message.trim().length()>0) {
sendSMS(tempMobileNumber, message);
}
else
{
Toast.makeText(getBaseContext(),
"Please enter both phone number and message.",
Toast.LENGTH_SHORT).show();
}
}
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK)
{
switch (requestCode)
{
case CONTACT_PICKER_RESULT:
Cursor cursor=null;
try
{
Uri result = data.getData();
Log.v(DEBUG_TAG, "Got a contact result: " + result.toString());
// get the contact id from the Uri
String id = result.getLastPathSegment();
// query for everything contact number
cursor = getContentResolver().query(
Phone.CONTENT_URI, null,
Phone.CONTACT_ID + "=?",
new String[]{id}, null);
cursor.moveToFirst();
int phoneIdx = cursor.getColumnIndex(Phone.DATA);
if (cursor.moveToFirst())
{
phonenofromcontact = cursor.getString(phoneIdx);
finallistofnumberstosendmsg +=","+phonenofromcontact;
Log.v(DEBUG_TAG, "Got phone no : " + phonenofromcontact);
}
else
{
Log.w(DEBUG_TAG, "No results");
}
}
catch(Exception e)
{
Log.e(DEBUG_TAG, "Failed to get contact number", e);
}
finally
{
if (cursor != null)
{
cursor.close();
}
}
phonePhoneno= (EditText)findViewById(R.id.Phonenofromcontact);
phonePhoneno.setText(finallistofnumberstosendmsg);
//phonePhoneno.setText(phonenofromcontact);
if(phonenofromcontact.length()==0)
{
Toast.makeText(this, "No contact number found for this contact",
Toast.LENGTH_LONG).show();
}
break;
}
}
else
{
Log.w(DEBUG_TAG, "Warning: activity result not ok");
}
}
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);
}
}
//此类通知和接收消息
公共类SmsReceiver扩展了BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++)
{
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
@覆盖
公共void onReceive(上下文、意图){
//---获取传入的SMS消息---
Bundle=intent.getExtras();
SmsMessage[]msgs=null;
字符串str=“”;
if(bundle!=null)
{
//---检索收到的SMS消息---
Object[]pdus=(Object[])bundle.get(“pdus”);
msgs=新SMS消息[PDU.length];
对于(int i=0;i,您可以使用intent将此已接收广播发送到您的主要活动。在您的主要活动上,接收此intent并将数据作为收件箱附加到列表视图中
还是那条粗糙但漫长的路
添加此数据(收到的号码和消息)到SQL数据库,在主活动中从数据库中提取数据并附加到名为inbox的列表视图中。这样,即使手机关闭或应用程序关闭,您的收件箱数据也会被保存。您应该为这个问题选择一个可接受的答案。感谢展示它可以开发。请告诉我@Chris Thompso好吗N