SoFunction
Updated on 2025-04-05

Android API development method for processing and obtaining contacts in SMS SMS service

This article describes the method of processing and obtaining contacts by SMS SMS service in Android API development. Share it for your reference, as follows:

The Android API supports the development of applications that can send and receive SMS messages. Currently, the Android emulator we used during the development process does not support sending SMS, but it can receive SMS. Now let's explore Android's support for SMS, and we will build a small application to listen to SMS messages received on mobile devices (or emulators) and display them.
Let's define an Intent receiver to handle SMS reception events:

package ;
public class SMSReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
    // TODO
  }
}
package ;
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO
}
}

We need to configure this Intent receiver to get SMS reception events. .SMS_RECEIVED This event status indicates that the SMS has been received. We can configure the following:

<receiver android:name="." android:enabled="true">
<intent-filter>
<action android:name=".SMS_RECEIVED" />
</intent-filter>
</receiver>
<receiver android:name="." android:enabled="true">
<intent-filter>
<action android:name=".SMS_RECEIVED" />
</intent-filter>
</receiver>

In order to enable our application to receive SMS, we must first specify permissions, which can be configured as follows:

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

Now our Intent receiver can be called when the Android device receives SMS. The rest is to get and display the received SMS message text:

public void onReceive(Context context, Intent intent) {
    Bundle bundle = ();
    Object messages[] = (Object[]) ("pdus");
    SmsMessage smsMessage[] = new SmsMessage[];
    for (int n = 0; n &lt; ; n++) {
        smsMessage[n] = ((byte[]) messages[n]);
    }
   // show first message
   Toast toast = (context, "Received SMS: " + smsMessage[0].getMessageBody(),        Toast.LENGTH_LONG);
   ();
}
public void onReceive(Context context, Intent intent) {
Bundle bundle = ();
Object messages[] = (Object[]) ("pdus");
SmsMessage smsMessage[] = new SmsMessage[];
for (int n = 0; n &lt; ; n++) {
smsMessage[n] = ((byte[]) messages[n]);
}
// show first message
Toast toast = (context, "Received SMS: " + smsMessage[0].getMessageBody(),        Toast.LENGTH_LONG);
();
}

The SMS received by the Android device is in the form of a pdu (protocol description unit). This class can store SMS-related information, and we can also create a new SmsMessage instance from the received pdu. The Toast interface component can display the received SMS message text in the form of system notifications.

Run the program:

Now let's run this application in the simulator and send SMS messages to the simulator. We can send SMS messages to the emulator in the DDMS view (Dalvik Debug Monitor Service) provided by the Android plug-in of eclipse (in the 'Emulator Control' panel; also need to specify the phone number, but it can be arbitrary)

Methods to send broadcast Intent:

public static final String MUSIC_ACTION="";
Intent intent=new Intent();
(MUSIC_ACTION);
("music_path", songPath);
(intent);
public static final String MUSIC_ACTION="";
Intent intent=new Intent();
(MUSIC_ACTION);
("music_path", songPath);
(intent);

You need to write another broadcast receiver:

public class MusicReceiver extends BroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent intent) {
 Bundle bundle=();
 String music_path=("music_path");
 Toast toast=(context, "Playing music:"+music_path, Toast.LENGTH_LONG);
 ();
 }
}
public class MusicReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle=();
String music_path=("music_path");
Toast toast=(context, "Playing music:"+music_path, Toast.LENGTH_LONG);
();
}
}

Get contact information:

public class ContactsList extends ListActivity {
 private ListAdapter mAdapter;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 (savedInstanceState);
 Cursor c=().query(.CONTENT_URI, null, null, null, null);
 (c);
 String[] columns=new String[]{};
 int[] names=new int[]{};////////////////
 mAdapter = new SimpleCursorAdapter(this, .song_item, c, columns, names);
 (mAdapter);
 }
 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
 (l, v, position, id);
 Intent i=new Intent(Intent.ACTION_CALL);
 Cursor c = (Cursor) (position);
   long phoneID = ((.PRIMARY_PHONE_ID));
   ((.CONTENT_URI, phoneID));
   (i);
 }
}
public class ContactsList extends ListActivity {
private ListAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
Cursor c=().query(.CONTENT_URI, null, null, null, null);
(c);
String[] columns=new String[]{};
int[] names=new int[]{};////////////////
mAdapter = new SimpleCursorAdapter(this, .song_item, c, columns, names);
(mAdapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
(l, v, position, id);
Intent i=new Intent(Intent.ACTION_CALL);
Cursor c = (Cursor) (position);
long phoneID = ((.PRIMARY_PHONE_ID));
((.CONTENT_URI, phoneID));
(i);
}
}

Add in:

<uses-permission android:name=".READ_CONTACTS"/>
<activity android:name=".ContactsList"
     android:label="@string/app_name">
  <intent-filter>
    <action android:name="" />
    <category android:name="" />
  </intent-filter>
</activity>

For more information about Android related content, please check out the topic of this site:A summary of Android SMS and Telephone Operation Tips》、《Android file operation skills summary》、《Summary of Android's SQLite database skills》、《Summary of Android operating json format data skills》、《Android database operation skills summary》、《Android programming activity operation skills summary》、《A summary of SD card operation methods for Android programming and development》、《Android development introduction and advanced tutorial》、《Android resource operation skills summary》、《Android View View Tips Summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.