2010-09-01 18 views
9

मैं एसएमएस भेजने और प्राप्त करने के लिए एक एसएमएस एप्लिकेशन बना रहा हूं।एंड्रॉइड में एक एसएमएस एप्लीकेशन बनाना?

मैं निम्नलिखित कोड का उपयोग कर एसएमएस भेजने के लिए कर रहा हूँ:

SmsManager sms = SmsManager.getDefault(); 
sms.sendTextMessage(phoneNumber, null,message , pi, null); 

मैं एसएमएस प्राप्त करने और उन्हें अपने खुद के इनबॉक्स में डाल करना चाहते हैं। मैं इस इनबॉक्स को कैसे बना सकता हूं? मैं इसे सामान्य इनबॉक्स के समान काम करना चाहता हूं।

Bundle bundle = intent.getExtras();  
Object[] pdus = (Object[]) bundle.get("pdus"); 
SmsMessage[] messages = new SmsMessage[pdus.length];  
for (int i = 0; i < messages.length; i++) { 

    messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]); 
    Log.v("SMSFun","Body: " + messages[i].getDisplayMessageBody()); 
    Log.v("SMSFun","Address: " + messages[i].getDisplayOriginatingAddress()); 
    //If say we wanted to do something based on who sent it  
    if (messages[i].getDisplayOriginatingAddress().contains("5556")) { 

     // we could launch an activity and pass the data 
     Intent newintent = new Intent(ctx, SecretMessage.class);  
     newintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     newintent.putExtra("address", messages[i].getDisplayOriginatingAddress()); 
     newintent.putExtra("message", messages[i].getDisplayMessageBody()); 
     ctx.startActivity(newintent); 
    } 
} 

मैं आने वाले एसएमएस को इनबॉक्स में कैसे स्टोर करूं?

क्या एंड्रॉइड में एसएमएस के लिए विशेष पोर्ट नंबर सुनने के लिए संभव है?

+0

आपको इस प्रश्न के लिए एक स्वीकृत उत्तर चुनना चाहिए। धन्यवाद –

उत्तर

2

आपको क्या करना है BroadcastReceiver ऑब्जेक्ट पंजीकृत करें। अधिक जानकारी के लिए this आलेख देखें।

यदि आप प्राथमिक इनबॉक्स से एसएमएस संदेश छिपाना चाहते हैं, तो आपको उन्हें एसएमएस सामग्री प्रदाता से हटा देना होगा और उन्हें स्टोर करने के लिए अपने स्वयं के SQLite डेटाबेस का उपयोग करना होगा। ट्रे से अधिसूचना को हटाने के लिए सामग्री प्रदाता में पढ़ने के रूप में उन्हें चिह्नित करना भी सुनिश्चित करें।

+0

इसे कैसे विकसित किया जा सकता है कृपया मुझे बताएं @ क्रिस थॉम्पसन –

4

मुझे नहीं लगता कि आप विभिन्न इनबॉक्स में एसएमएस डाल सकते हैं और आप BroadcastReceiver का उपयोग करने वाले एसएमएस प्राप्त करने के लिए पोर्ट को नहीं सुनते हैं।

मैं आपको सामान्य स्रोतों के लिए काम करने के तरीके के बारे में बेहतर विचार प्राप्त करने के लिए ओपन सोर्स smspopup ऐप के माध्यम से जाने की सलाह दूंगा।

4

यू एसएमएसमैंजर वर्ग का उपयोग कर संदेश भेज और प्राप्त कर सकता है। यू कस्टम रिसीवर को कार्यान्वित कर सकता है जिस पर संदेश प्राप्त हुआ है, यह संदेश को सूचित करेगा कि संदेश आ गया है .. यहां मैं कोड जोड़ रहा हूं जिसे मैंने कस्टम ब्रॉडकास्ट रिसीवर का उपयोग करके संदेश भेजने और प्राप्त करने के लिए लिखा है, यह आपके लिए उपयोगी हो सकता है। नोट: यह ऊपर संस्करण 1.6 के लिए है .. इसलिए सुनिश्चित करें कि आप इसे 2.0 या 2.2 में अधिमानतः करते हैं।

इसके माध्यम से जाओ और इसे लागू करने के लिए प्रयास करें ..

सार्वजनिक वर्ग एसएमएस गतिविधि फैली {

Button btnSendSMS; 
EditText txtPhoneNo; 
EditText txtMessage; 
Button addcontact; 
EditText phonePhoneno; 


private static final int CONTACT_PICKER_RESULT = 1001; 
private static final String DEBUG_TAG = ""; 

String phoneNo=""; 
String phonenofromcontact=""; 
String finallistofnumberstosendmsg =""; 


/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    btnSendSMS = (Button) findViewById(R.id.btnSendSMS); 
    txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo); 
    txtMessage = (EditText) findViewById(R.id.txtMessage); 
    addcontact =(Button) findViewById(R.id.addphonenofromcontact); 


    addcontact.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View V) 
     { 
      Intent ContactPickerIntent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI); 
      startActivityForResult(ContactPickerIntent, CONTACT_PICKER_RESULT);    
     } 
    } 
    ); 

    btnSendSMS.setOnClickListener(new View.OnClickListener() 
    { 

     public void onClick(View v) 
     {     
      String message = txtMessage.getText().toString(); 

      phoneNo = txtPhoneNo.getText().toString(); 
      String phoneNo1= phonePhoneno.getText().toString(); 

      // Sending message to both the written and added contact... 

      finallistofnumberstosendmsg +=phoneNo1 + phoneNo; 
      String phoneFinal= phoneNo + finallistofnumberstosendmsg; 

      //StringTokenizer st=new StringTokenizer(finallistofnumberstosendmsg,","); 

      StringTokenizer st=new StringTokenizer(phoneFinal,","); 
      while (st.hasMoreElements()) 
      { 
       String tempMobileNumber = (String)st.nextElement(); 
       if(tempMobileNumber.length()>0 && message.trim().length()>0) { 
        sendSMS(tempMobileNumber, message); 
       } 
       else 
       { 
        Toast.makeText(getBaseContext(), 
          "Please enter both phone number and message.", 
          Toast.LENGTH_SHORT).show(); 
       } 
      } 
      } 
    }); 
    } 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) 
    { 
     switch (requestCode) 
     { 
     case CONTACT_PICKER_RESULT: 
      Cursor cursor=null; 
      try 
      { 
       Uri result = data.getData(); 
       Log.v(DEBUG_TAG, "Got a contact result: " + result.toString()); 

       // get the contact id from the Uri  
       String id = result.getLastPathSegment(); 

       // query for everything contact number 
       cursor = getContentResolver().query( 
         Phone.CONTENT_URI, null, 
         Phone.CONTACT_ID + "=?", 
         new String[]{id}, null); 

       cursor.moveToFirst(); 
       int phoneIdx = cursor.getColumnIndex(Phone.DATA); 
       if (cursor.moveToFirst()) 
       { 
        phonenofromcontact = cursor.getString(phoneIdx); 
        finallistofnumberstosendmsg +=","+phonenofromcontact; 
        Log.v(DEBUG_TAG, "Got phone no : " + phonenofromcontact); 
       } 
       else 
       {         
        Log.w(DEBUG_TAG, "No results"); 
       } 
      } 
      catch(Exception e) 
      { 
       Log.e(DEBUG_TAG, "Failed to get contact number", e); 
      } 
      finally 
      { 
       if (cursor != null) 
       { 
        cursor.close(); 
       } 
      } 
      phonePhoneno= (EditText)findViewById(R.id.Phonenofromcontact); 
      phonePhoneno.setText(finallistofnumberstosendmsg); 
      //phonePhoneno.setText(phonenofromcontact); 
      if(phonenofromcontact.length()==0) 
      { 
       Toast.makeText(this, "No contact number found for this contact", 
         Toast.LENGTH_LONG).show(); 
      } 
      break; 
     } 
    } 
    else 
    { 
     Log.w(DEBUG_TAG, "Warning: activity result not ok"); 
    } 
} 

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);  
} 

}

// इस वर्ग को सूचित करें और प्राप्त संदेशों

सार्वजनिक वर्ग एसएमएसआरसीवर ब्रॉडकास्ट रिसीवर का विस्तार करता है {

@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(); 
    } 
} 

}

धन्यवाद ... राकेश

0

आप अपने इस इरादे का उपयोग कर अपने मुख्य गतिविधि के लिए प्रसारण प्राप्त भेज सकते हैं। और आपकी मुख्य गतिविधि पर यह इरादा प्राप्त होता है और डेटा को इनबॉक्स के रूप में सूची दृश्य में जोड़ता है।

या बास्ट लेकिन लंबा रास्ता

मुख्य गतिविधि fatch डेटा बेस से डेटा और सूची दृश्य इनबॉक्स कहा जाता है को संलग्न SQL डेटाबेस के लिए और पर इस डेटा (प्राप्त संख्या और संदेश) जोड़ें।इस तरह से आपका इनबॉक्स डेटा सहेजा जाएगा भले ही फोन बंद हो या एप्लिकेशन बंद हो जाए।

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