代码之家  ›  专栏  ›  技术社区  ›  Buda Gavril

android:注册应用程序接收短信

  •  7
  • Buda Gavril  · 技术社区  · 15 年前

    我如何注册我的应用程序,以便当我收到短信时,我的应用程序出现在对话框中,使用完成操作。我已经输入了一个意图代码

    <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <action android:name="android.intent.action.SENDTO"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:scheme="sms"/>
    </intent-filter>
    

    但没用。。。我应该用接收器吗?请注意,我插入此代码的活动不是主活动。谢谢

    4 回复  |  直到 15 年前
        1
  •  8
  •   Vinothkumar Arputharaj    14 年前

    使用以下代码。

    <activity android:name=".SMSNewActivity" >
                <intent-filter>
                    <action android:name="android.intent.action.VIEW" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <data android:mimeType="vnd.android-dir/mms-sms" />
                </intent-filter>
                <intent-filter>
                    <action android:name="android.intent.action.VIEW" />
                    <action android:name="android.intent.action.SENDTO" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.BROWSABLE" />
                    <data android:scheme="sms" />
                    <data android:scheme="smsto" />
                </intent-filter>
                <intent-filter>
                    <action android:name="android.intent.action.SEND" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <data android:mimeType="text/plain" />
                </intent-filter>
            </activity>
            <activity android:name=".SMSMainListActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
                 <intent-filter>
                    <action android:name="android.intent.action.SEARCH" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
    
        2
  •  3
  •   Shardul    15 年前

    它在文档中没有很好的记录。

    你可以在 AndDev

    以下是一些摘录:

    1. 您必须使用以下权限,并将其包含在您的AndroidManifest中

    <uses-permission android:name="android.permission.RECEIVE_SMS" />

    2您不能在您的活动中声明相同的意图过滤器,但要在接收者中进行过滤,包括在您的清单之后

    <receiver android:name=".SMSReceiver"> <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver>

    3创建一个扩展的类 android.content.IntentReceiver 重写类listen for action的onReceiveIntent方法 android.provider.Telephony.SMS_RECEIVED 不是SDK的一部分

    下面是更多的代码摘录:

        // @Override
    public void onReceiveIntent(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION)) {
            // if(message starts with SMStretcher recognize BYTE)
            StringBuilder sb = new StringBuilder();
    
            /* The SMS-Messages are 'hiding' within the extras of the Intent. */
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                /* Get all messages contained in the Intent*/
                SmsMessage[] messages = 
                    Telephony.Sms.Intents.getMessagesFromIntent(intent);
    
                /* Feed the StringBuilder with all Messages found. */
                for (SmsMessage currentMessage : messages){
                    sb.append("Received compressed SMSnFrom: ");
                    /* Sender-Number */
                    sb.append(currentMessage.getDisplayOriginatingAddress());
                    sb.append("n----Message----n");
                    /* Actual Message-Content */
                    sb.append(currentMessage.getDisplayMessageBody());
                }
            }
            /* Logger Debug-Output */
            Log.i(LOG_TAG, "[SMSApp] onReceiveIntent: " + sb);
    
            /* Show the Notification containing the Message. */ 
            Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
    
        3
  •  2
  •   BoD    14 年前

    看这本书的免费章节 开始Android应用程序开发 ,其中详细显示了如何接收(和发送)短信:

    http://p2p.wrox.com/content/articles/creating-android-apps-send-and-receive-sms-messages-and-send-email

        4
  •  0
  •   NickT    15 年前

    你需要这个

    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    

    在AndroidManifest.xml中