热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在Android平台上制作一个SMS应用程序-MakinganSMSApplicationontheAndroidPlatform

IamcreatinganSMSApplicationtosendandreceiveSMS.我正在创建一个SMS应用程序来发送和接收SMS。Iamabletose

I am creating an SMS Application to send and receive SMS.

我正在创建一个SMS应用程序来发送和接收SMS。

I am able to send SMS using the following code:

我可以发送短信使用以下代码:

SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null,message , pi, null); 

I would like to receive SMS and put them in my own inbox. How do I create this inbox? I would like it to work the same way as a normal inbox.

我想收到短信并把它们放在我自己的收件箱里。如何创建这个收件箱?我希望它和普通的收件箱一样工作。

Bundle bundle = intent.getExtras();     
Object[] pdus = (Object[]) bundle.get("pdus"); 
SmsMessage[] messages = new SmsMessage[pdus.length];    
for (int i = 0; i 

How do I store incoming SMS in the inbox?

如何在收件箱中存储收到的短信?

Is it possible in Android to listen to particular port number for SMS?

Android中有可能监听特定的SMS端口号吗?

4 个解决方案

#1


4  

I don't think you can place sms in different inboxes and you don't listen to a port to get SMS you use a BroadcastReceiver.

我不认为你可以把短信放在不同的收件箱里,你也不听一个端口来接收你使用的广播接收器的短信。

I would recommend you to go through the open source smspopup app to get a better idea of how things work for sms in general.

我建议您通过开放源码的smspopup应用程序了解sms是如何工作的。

#2


4  

U can send and recieve message using SMSMAnager class. U can implement custom Reciever that on msg recieved it will notify user that message has arrived.. Here i am attaching code that i have written to send and receive message using Custom Broadcast reciever, It might be useful to u. Note: This is for the version 1.6 above..So make sure that u do it in 2.0 or 2.2 preferably.

可以使用SMSMAnager类发送和接收消息。你可以实现自定义接收,在收到msg后会通知用户消息已经到达。在这里,我附上了我写的用来发送和接收自定义广播接收消息的代码,它可能对u有用。所以要确保你在2。2或2。2中做得更好。

Go through it and try to implement it..

通过它并尝试实现它。

public class SMS extends Activity {

公共类SMS扩展活动

 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);       
}

}

}

// This class notify and receive messages

//此类通知和接收消息

public class SmsReceiver extends BroadcastReceiver {

公共类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

}

}

Thanks... rakesh

谢谢……拉克什

#3


2  

What you need to do is register a BroadcastReceiver object. Check out this article for more information.

您需要做的是注册一个BroadcastReceiver对象。更多信息请参阅本文。

If you want to hide SMS messages from the primary inbox, you'll need to delete them from the SMS ContentProvider and use your own SQLite database to store them. Also be sure to mark them as read in the content provider to remove the notification from the tray.

如果您想从主收件箱中隐藏SMS消息,您需要从SMS内容提供程序中删除它们,并使用您自己的SQLite数据库来存储它们。还要确保将它们标记为在内容提供程序中读取,以便从托盘中删除通知。

#4


0  

You can send your this received broadcast to your main activity using intent. And on your main activity receive this intent and append the data to the list view as a inbox.

您可以使用意图将接收到的这个广播发送到您的主活动。在您的主活动上接收此意图并将数据附加到列表视图作为收件箱。

OR the bast but lengthy way

或者是一条漫长的路

Add this data (received number and message) to the SQL database and on the main activity fatch the data from data base and append to the list view called inbox.In this way your inbox data will be saved even if the phone is switched off or if application is shut down.

将该数据(接收到的数字和消息)添加到SQL数据库中,并在主活动fatch中添加来自数据库的数据,并添加到名为inbox的列表视图。这样,即使手机关机或者应用程序关机,你的收件箱数据也会被保存下来。


推荐阅读
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 探讨如何高效使用FastJSON进行JSON数据解析,特别是从复杂嵌套结构中提取特定字段值的方法。 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • XNA 3.0 游戏编程:从 XML 文件加载数据
    本文介绍如何在 XNA 3.0 游戏项目中从 XML 文件加载数据。我们将探讨如何将 XML 数据序列化为二进制文件,并通过内容管道加载到游戏中。此外,还会涉及自定义类型读取器和写入器的实现。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • RecyclerView初步学习(一)
    RecyclerView初步学习(一)ReCyclerView提供了一种插件式的编程模式,除了提供ViewHolder缓存模式,还可以自定义动画,分割符,布局样式,相比于传统的ListVi ... [详细]
author-avatar
摩羯法国反弹
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有