2010-11-23 12 views
7

cómo puedo registrar mi aplicación para que cuando reciba un sms mi aplicación aparezca en el diálogo Complete la acción usando. He puesto un código de intenciónandroid: registrar la aplicación para recibir sms

<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> 

pero no funciona ... ¿Debo usar el receptor? Tenga en cuenta que la actividad en la que he insertado este código no es actividad principal. Gracias

Respuesta

8

utilizar el siguiente código.

<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> 
0

Necesitará este

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

en los AndroidManifest.xml

+0

que tienen permisos –

+0

Send_SMS, READ_SMS, WRITE_SMS, RECEIVE_SMS, BROADCAST_SMS –

3

No es bien documentados en el doc.

Puede encontrar información sobre AndDev

Éstos son algunos extractos:

  1. debe utilizar siguiente permiso, incluirla en su AndroidManifest

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

2 Usted no debe declarar un filtro de intención en su actividad para el mismo, pero filtrarla en un receptor, incluir seguir el manifiesto

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

3 Cree una clase que amplíe android.content.IntentReceiver y reemplazar el método onReceiveIntent de la clase escucha para la acción android.provider.Telephony.SMS_RECEIVED no forma parte del SDK

heres algunos más extracto de código:

// @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(); 
+0

sólo puedo utilizar android.content.IntentSender, android.content.IntentFilter y android.content.Intent ... –

+0

TestSMS clase pública se extiende BroadcastReceiver { private final String ACTION = "android.provider.Telephony.SMS_RECEIVED"; @ Override OnReceive (contexto Contexto, la intención Intención) public void {if ( intent.getAction() es igual a (ACCION).) { System.out.println ("Tengo un SMS"); } } } pero aún no está funcionando. Me avisan solo cuando recibo un mensaje, pero quiero que mi aplicación esté en diálogo Complete la acción usando ... –

+0

FYI, IntentReceiver fue eliminado del Android SDK y fue reemplazado por BroadcastReceiver. –

Cuestiones relacionadas