2015-04-29 5 views
0

Я пытаюсь установить тело SMS в EditText, когда я его получу. Я использую BroadcastReceiver для захвата действия SMS_RECEIVED и регистрации его с помощью registerReceiver().Чтение SMS в android, когда действие SMS_RECEIVED захвачено BroadCastReceiver

Проблема Когда я получаю sms, ничего не происходит, и EditText не обновляется.

То, что я попытался для отладки

Я попытался установить EditText без BroadcastReceiver в OnCreate() метод только и обновляется с текстом сообщ, который уже получил от конкретного числа.

public class CardActivity extends Activity { 

EditText txtCardId; 
final String SMS_URI_INBOX = "content://sms/inbox"; 
IntentFilter filter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.card); 
    btnProceed = (Button) findViewById(R.id.btnProceed); 
    txtCardId = (EditText) findViewById(R.id.txtCardId); 
    filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED"); 
    registerReceiver(mBroadcastReceiver,filter); 

    }); 
} 

private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     try { 
      context = getApplicationContext(); 
      Uri uri = Uri.parse(SMS_URI_INBOX); 
      String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" }; 
      Cursor cur = getContentResolver().query(uri, projection, "address='+919016832405'", null, "date desc"); 
      if (cur.moveToFirst()) { 
       int index_Body = cur.getColumnIndex("body"); 
       String strbody; 
       do { 
        strbody = cur.getString(index_Body); 
       } while (cur.moveToNext()); 
       txtCardId.setText(strbody); 

       if (!cur.isClosed()) { 
        cur.close(); 
        cur = null; 
       } 
      } else { 
       txtCardId.setText("nothing"); 
      } // end if 
     } 
     catch (SQLiteException ex) { 
      Log.d("SQLiteException", ex.getMessage()); 
     } 
    }}; 
} 

ответ

0

я нашел место, где я ошиблась

Я использую READ_SMS разрешения в menifest, а мне нужно использовать RECEIVE_SMS. Это сработало.

Смежные вопросы