热门标签 | 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的列表视图。这样,即使手机关机或者应用程序关机,你的收件箱数据也会被保存下来。


推荐阅读
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • FeatureRequestIsyourfeaturerequestrelatedtoaproblem?Please ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
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社区 版权所有