在Android中收到的短信,并显示在TextView中 [英] received sms and show in textview in android

查看:143
本文介绍了在Android中收到的短信,并显示在TextView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的程序在Android和显示发送和接收短信收到的短信内容在文本视图。我希望收到短信的程序,而不是在收件箱中messege! 我知道必须从textview.settext(smsContentEtc)使用,但我不能使用,并实现它。 我把总共code。 sms.java用于发送短信和SmsReceiver.java对收到的短信。 请帮助我。

sms.java

 公共类短信延伸活动{

        按钮btnSendSMS;
        的EditText txtPhoneNo;
        的EditText txtMessage;
        / **第一次创建活动时调用。 * /
        @覆盖
        公共无效的onCreate(包savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.activity_main);
            btnSendSMS =(按钮)findViewById(R.id.btnSendSMS);
            txtPhoneNo =(EditText上)findViewById(R.id.txtPhoneNo);
            txtMessage =(EditText上)findViewById(R.id.txtMessage);
            btnSendSMS.setOnClickListener(新View.OnClickListener()
            {
                公共无效的onClick(视图v)
                {
                    字符串PHONENO = txtPhoneNo.getText()的toString()。
                    字符串消息= txtMessage.getText()的toString()。
                    如果(phoneNo.length()大于0&安培;&安培; message.length()大于0)
                        sendSMS(PHONENO,消息);
                    其他
                        Toast.makeText(getBaseContext(),
                            请同时输入电话号码和信息。
                            Toast.LENGTH_SHORT).show();
                }
            });
        }
        私人无效sendSMS(字符串phoneNumber的,字符串消息)
        {
            字符串SENT =SMS_SENT;
            字符串DELIVERED =SMS_DELIVERED;

            PendingIntent sentPI = PendingIntent.getBroadcast(此,0,
                新的意图(SENT),0);

            PendingIntent deliveredPI = PendingIntent.getBroadcast(此,0,
                新的意图(交付),0);

            // ---当短信已发送---
            registerReceiver(新BroadcastReceiver的(){
                @覆盖
                公共无效的onReceive(背景为arg0,意图ARG1){
                    开关(的getResult code())
                    {
                        案例Activity.RESULT_OK:
                            Toast.makeText(getBaseContext(),短信发送
                                    Toast.LENGTH_SHORT).show();
                            打破;
                        案例SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                            Toast.makeText(getBaseContext(),一般故障,
                                    Toast.LENGTH_SHORT).show();
                            打破;
                        案例SmsManager.RESULT_ERROR_NO_SERVICE:
                            Toast.makeText(getBaseContext(),无服务,
                                    Toast.LENGTH_SHORT).show();
                            打破;
                        案例SmsManager.RESULT_ERROR_NULL_PDU:
                            Toast.makeText(getBaseContext(),空的PDU,
                                    Toast.LENGTH_SHORT).show();
                            打破;
                        案例SmsManager.RESULT_ERROR_RADIO_OFF:
                            Toast.makeText(getBaseContext(),无线电关
                                    Toast.LENGTH_SHORT).show();
                            打破;
                    }
                }
            },新的IntentFilter(发送));

            // ---当SMS已交付---
            registerReceiver(新BroadcastReceiver的(){
                @覆盖
                公共无效的onReceive(背景为arg0,意图ARG1){
                    开关(的getResult code())
                    {
                        案例Activity.RESULT_OK:
                            Toast.makeText(getBaseContext(),SMS递送,
                                    Toast.LENGTH_SHORT).show();
                            打破;
                        案例Activity.RESULT_CANCELED:
                            Toast.makeText(getBaseContext(),短信未交付,
                                    Toast.LENGTH_SHORT).show();
                            打破;
                    }
                }
            },新的IntentFilter(交付));
            SmsManager的短信= SmsManager.getDefault();
            sms.sendTextMessage(phoneNumber的,空,消息,sentPI,deliveredPI);
        }
}
 

SmsReceiver的.java

 公共类SmsReceiver扩展的BroadcastReceiver
    {
        私人TextView的txtshow;
         @覆盖
            公共无效的onReceive(上下文的背景下,意图意图)
            {
                // ---得到通过短信---
                捆绑包= intent.getExtras();
                SmsMessage []封邮件= NULL;
                字符串str =;
                如果(捆绑!= NULL)
                {
                    // ---检索收到的短信---
                    [对象]的PDU =(对象[])bundle.get(的PDU);
                    封邮件=新SmsMessage [pdus.length]

                    的for(int i = 0; I< msgs.length;我++){
                        封邮件[I] = SmsMessage.createFromPdu((字节[])的PDU [I]);
                        STR + =短信来自+封邮件[I] .getOriginatingAddress();
                        STR + =;
                        STR + =封邮件[I] .getMessageBody()的toString()。
                        STR + =\ N的;

                    }
                    // ---显示新短信---
                   //Toast.makeText(context,STR,Toast.LENGTH_SHORT).show();
                    txtshow.setText(smsContentEtc);
                }
            }
        }
 

main.xml中

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>

    < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    >
    <的TextView
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=输入收件人的电话号码
        />

    <的EditText
        机器人:ID =@ + ID / txtPhoneNo
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT/>

    <的TextView
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=消息
        />

    <的EditText
        机器人:ID =@ + ID / txtMessage
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =150像素
        机器人:重力=顶/>

    <按钮
        机器人:ID =@ + ID / btnSendSMS
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文=发送短信
        />

    <的TextView
        机器人:ID =@ + ID / txt_Show
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:freezesText =@布尔/ R
        机器人:文本=TextView的/>

< / LinearLayout中>
< / RelativeLayout的>
 

的Manifest.xml

 <舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.sms
    安卓版code =1
    机器人:VERSIONNAME =1.0>

    <用途-SDK
        安卓的minSdkVersion =7
        机器人:targetSdkVersion =15/>


    <应用机器人:图标=@可绘制/ ic_launcher机器人:标签=@字符串/ APP_NAME>
        <活动机器人:短信NAME =
                  机器人:标签=@字符串/ APP_NAME>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>
                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
         <接收机器人:SmsReceiverNAME =>
            <意向滤光器>
                <作用机器人:名称=
                    android.provider.Telephony.SMS_RECEIVED/>
            &所述; /意图滤光器>
        < /接收器>
    < /用途>
    <使用-权限的Andr​​oid:名称=android.permission.SEND_SMS>
    < /使用-许可>
    <使用-权限的Andr​​oid:名称=android.permission.RECEIVE_SMS>
    < /使用-许可>
< /舱单>
 

解决方案

您不能使用的setText或视图的喜欢,任何其他方法

  

txtshow.setText(smsContentEtc);

为了修改文本视图,首先你应该使用R.findViewById和初始化你的观点,但因为你的类不从活动扩展您不能使用它。相反,我建议您使用共享preferences

另外,如果你不想在收件箱中的短信,你可以使用<一个href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#abortBroadcast%28%29"相对=nofollow> AbortBroadcast

I write program for sending and receiving sms in android and display received content sms in text view. I want to received sms in program, not in inbox messege!!! I know must use from textview.settext("smsContentEtc") but I cant use and implements it. I put total code. sms.java for send sms and SmsReceiver.java for received sms. please help me.

sms.java

public class SMS extends Activity {

        Button btnSendSMS;
        EditText txtPhoneNo;
        EditText txtMessage;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);          
            btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
            txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
            txtMessage = (EditText) findViewById(R.id.txtMessage);         
            btnSendSMS.setOnClickListener(new View.OnClickListener() 
            {
                public void onClick(View v) 
                {                
                    String phoneNo = txtPhoneNo.getText().toString();
                    String message = txtMessage.getText().toString();                 
                    if (phoneNo.length()>0 && message.length()>0)                
                        sendSMS(phoneNo, message);                
                    else
                        Toast.makeText(getBaseContext(), 
                            "Please enter both phone number and message.", 
                            Toast.LENGTH_SHORT).show();
                }
            });        
        } 
        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 .java

 public class SmsReceiver extends BroadcastReceiver
    {
        private TextView txtshow;
         @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();
                    txtshow.setText("smsContentEtc");                  
                }                         
            }
        }

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Enter the phone number of recipient"
        />     

    <EditText
        android:id="@+id/txtPhoneNo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"         
        android:text="Message"
        />     

    <EditText
        android:id="@+id/txtMessage"
        android:layout_width="fill_parent"
        android:layout_height="150px"
        android:gravity="top" />

    <Button 
        android:id="@+id/btnSendSMS"  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:text="Send SMS"
        />

    <TextView
        android:id="@+id/txt_Show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:freezesText="@bool/r"
        android:text="TextView" />

</LinearLayout>
</RelativeLayout>

Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sms"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="15" />


    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
        <activity android:name=".SMS"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <receiver android:name=".SmsReceiver"> 
            <intent-filter > 
                <action android:name=
                    "android.provider.Telephony.SMS_RECEIVED" /> 
            </intent-filter> 
        </receiver>
    </application>
    <uses-permission android:name="android.permission.SEND_SMS">
    </uses-permission>
    <uses-permission android:name="android.permission.RECEIVE_SMS">
    </uses-permission>
</manifest>

解决方案

You can't use setText or any other method of view's like that

txtshow.setText("smsContentEtc");

In order to modify your text view, first you should use R.findViewById and initialize your view, but since your class doesn't extends from Activity you can't use it. Instead I suggest you to use Shared Preferences

Also if you don't want sms in inbox, you can use AbortBroadcast

这篇关于在Android中收到的短信,并显示在TextView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆