2011-02-15 8 views
17

क्या कोई इस से मेरी सहायता कर सकता है?एंड्रॉइड में आने वाले संदेश में नए की संदेश सामग्री को कैसे पढ़ा जाए?

मैं एंड्रॉयड में एक नया आने वाले एसएमएस के संदेश के मुख्य भाग, प्रोग्राम के रूप में पढ़ना चाहते हैं।

मैं कुछ लेकिन वह करने की कोशिश की किसी भी सामग्री नहीं लौटाता:

Uri uri = Uri.parse("content://sms/inbox"); 
     ContextWrapper context = null;  
     Cursor c = context.getContentResolver().query(uri, null, null ,null,null);  
     String body = null; 
     String number=null; 
     if(c.moveToFirst()) {   
      body = c.getString(c.getColumnIndexOrThrow("body")).toString(); 
      number = c.getString(c.getColumnIndexOrThrow("address")).toString(); 
     } 
     c.close(); 

मेरी कोड में अपने किसी भी त्रुटि कृपया किसी भी एक मुझे जवाब दे है कृपया क्योंकि पूरी तरह से मैं इस

के लिए एक महीने के बर्बाद किया
+0

अच्छा यहाँ लेख: http://mobdev.olin.edu/mobdevwiki/FrontPage/Tutorials/SMS%20Messaging –

+0

http://stackoverflow.com/questions/14097500/get-inbox-messages-from-android-device -to-show-in-custom-listview # यह मेरे लिए काम करता है। यह आदमी StackOverflow – texeratx

उत्तर

21

मैंने अपनी कक्षा वेबसाइट पर इसके बारे में कुछ नमूना कार्यक्रम पोस्ट किए हैं। यहाँ उदाहरण Read SMS Example है यहाँ कोड का एक टुकड़ा है। मूल रूप से आप SMS_Receive सुनने के लिए एक प्रसारण रिसीवर पंजीकृत कर सकते हैं और निम्न जांच सकते हैं।

Intent intent = getIntent(); 
    Bundle bundle = intent.getBundleExtra("mySMS"); 

    if (bundle != null) { 
     Object[] pdus = (Object[])bundle.get("pdus"); 
     SmsMessage sms = SmsMessage.createFromPdu((byte[])pdus[0]); 
     Log.i("mobile.cs.fsu.edu", "smsActivity : SMS is <" + sms.getMessageBody() +">"); 

     //strip flag 
     String message = sms.getMessageBody(); 
     while (message.contains("FLAG")) 
      message = message.replace("FLAG", ""); 

     TextView tx = (TextView) findViewById(R.id.TextBox); 
     tx.setText(message);    
    } else 
     Log.i("mobile.cs.fsu.edu", "smsActivity : NULL SMS bundle"); 
+0

पर यहाँ anwser कार्य ठीक धन्यवाद फ्रैंक – Krishna

+1

इस कोड को इतना सरल और सुरुचिपूर्ण है। धन्यवाद! – Simon

+0

एक स्थिर 'getMessagesFromIntent (इरादा)' विधि 'SmsMessage'-array (एपीआई स्तर 1 9 के बाद) को वापस करने की विधि भी है, इसलिए यदि आपका लक्ष्य स्तर पर्याप्त है तो "pdus" बाइट-सरणी का मैन्युअल रीडिंग/कास्टिंग आवश्यक नहीं हो सकता है : https://developer.android.com/reference/android/provider/Telephony.Sms.Intents.html#getMessagesFromIntent(android.content.Intent) – Blacklight

4

नीचे जो आने वाले संदेश और सूची दृश्य में प्रदर्शन को पढ़ने के कोड का टुकड़ा है, डॉन 'मैनिफ़ेस्ट फ़ाइल में अनुमति जोड़ने के लिए भूल जाते हैं:

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

यहाँ कोड:

listitem=(ListView)findViewById(R.id.ListView); 

     Uri mSmsQueryUri = Uri.parse("content://sms/inbox"); 
     List<String> messages = new ArrayList<String>(); 

     Cursor cursor = null; 
     try { 
      cursor = getContentResolver().query(mSmsQueryUri, null, null, null, null); 
      if (cursor == null) { 
       Log.i(TAG, "cursor is null. uri: " + mSmsQueryUri); 

      } 
      for (boolean hasData = cursor.moveToFirst(); hasData; hasData = cursor.moveToNext()) { 
       final String body = cursor.getString(cursor.getColumnIndexOrThrow("body")); 
       messages.add(body); 
      } 
     } catch (Exception e) { 
      Log.e(TAG, e.getMessage()); 
     } finally { 
      cursor.close(); 
     } 

     listitem.setAdapter(new ArrayAdapter<String>(ReadMessage.this, android.R.layout.simple_list_item_1,messages)); 
+0

सभी संदेशों को प्राप्त करने के बजाय केवल आज संदेश प्राप्त करने के लिए वैसे भी है? –

0

यहां इस उदाहरण मैं कि कैसे हाल प्राप्त (आवक) इनबॉक्स से एसएमएस पढ़ने के लिए और TextView में यह दिखाने के लिए आप का प्रदर्शन करेंगे में। या

https://github.com/adorsys/sms-parser-android

compile 'de.adorsys.android:smsparser:0.0.3' 

यह का उपयोग करके आप या तो पूरा संदेश पढ़ सकते हैं, के विशिष्ट भागों:

fstmsgBtn.setOnClickListener(new OnClickListener() 

    { public void onClick(View v) 
    { 
     Uri my_uri = Uri.parse("content://sms/inbox");   
     Cursor readFstSms =v.getContext().getContentResolver().query(my_uri, null, null ,null,null); 
     if(readFstSms.moveToFirst()) 
     { 
      String msg_body = c.getString(c.getColumnIndexOrThrow("body")).toString(); 
      //String sender_number = c.getString(c.getColumnIndexOrThrow("address")).toString(); 
      readtxt.setText(msg_body); 
     } 
     readFstSms.close(); 
    } 
    }); 
0
listitem=(ListView)findViewById(R.id.list_view); 

      Uri mSmsQueryUri = Uri.parse("content://sms/inbox"); 
      List<String> messages = new ArrayList<String>(); 

      Cursor cursor = null; 
      try { 
       cursor = getContentResolver().query(mSmsQueryUri, null, null, null, null); 
       if (cursor == null) { 
        // Log.i(TAG, "cursor is null. uri: " + mSmsQueryUri); 

       } 
       for (boolean hasData = cursor.moveToFirst(); hasData; hasData = cursor.moveToNext()) { 
        final String body = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString(); 
        final String sender_no= cursor.getString(cursor.getColumnIndexOrThrow("address")).toString(); 
        final String date= cursor.getString(cursor.getColumnIndexOrThrow("date")); 
        final String type =cursor.getString(cursor.getColumnIndexOrThrow("type")); 


        messages.add(body); 
        messages.add(sender_no); 
        messages.add(date); 
        messages.add(type); 
       } 
      } catch (Exception e) { 
       //Log.e(TAG, e.getMessage()); 
      } finally { 
       cursor.close(); 
      } 

      listitem.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,messages)); 
    } 
} 
1

एक बहुत ही आसान समाधान इस एसएमएस पार्सर लाइब्रेरी का उपयोग किया जाएगा प्राप्त संदेश। आप फोन नंबर भी सेट कर सकते हैं जिससे संदेश आ जाएगा।

आप यह कैसे काम करता है या इसके उपयोग GitHub भंडार मैं उपरोक्त जाँच के बारे में अधिक जानकारी की जरूरत है।

संबंधित मुद्दे