Android API Development SMS SMS service processing and obtaining contact person's method _android

Source: Internet
Author: User
Tags sqlite database

This article describes the development of the Android API SMS SMS service processing and access to the method of contact person. Share to everyone for your reference, specific as follows:

The Android API supports the development of applications that can send and receive SMS messages. The Android emulator used in our development process is not yet supported for SMS, but it can receive SMS. Now let's explore Android's support for SMS, and we'll build a small application that listens to SMS messages received on a mobile device (or emulator) and displays it.
Let's define a intent receiver to handle SMS receive events:

Package com.wissen.sms.receiver;
public class Smsreceiver extends Broadcastreceiver {
  @Override
  the public void onreceive (context context, Intent Intent) {
    //TODO
  }
}
package com.wissen.sms.receiver;
public class Smsreceiver extends Broadcastreceiver {
@Override
the public void onreceive (context context, Intent Intent) {
//TODO
}
}

We need to configure this intent receiver so that it can get SMS receive events, Android.provider.Telephony.SMS_RECEIVED This event status indicates that SMS has been received. We can configure the following in Androidmanifest.xml:

<receiver android:name= ". Receiver. Smsreceiver "android:enabled=" true ">
<intent-filter>
<action android:name=" Android.provider.Telephony.SMS_RECEIVED "/>
</intent-filter>
</receiver>
< Receiver Android:name= ". Receiver. Smsreceiver "android:enabled=" true ">
<intent-filter>
<action android:name=" Android.provider.Telephony.SMS_RECEIVED "/>
</intent-filter>
</receiver>

In order for our applications to receive SMS, we have to specify the permissions, which can be configured in Androidmanifest.xml as follows:

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

Now, our intent receiver can be invoked when the Android device receives SMS, and the rest is to get and display the text of the SMS message received:

public void OnReceive (context context, Intent Intent) {Bundle Bundle = Intent.getextras ();
    Object messages[] = (object[]) bundle.get ("PDUs");
    Smsmessage smsmessage[] = new Smsmessage[messages.length];
    for (int n = 0; n < messages.length n++) {Smsmessage[n] = SMSMESSAGE.CREATEFROMPDU ((byte[)) messages[n]);        }//Show the Toast Toast = Toast.maketext (Context, "Received SMS:" + smsmessage[0].getmessagebody (),
   Toast.length_long);
Toast.show ();
public void OnReceive (context context, Intent Intent) {Bundle Bundle = Intent.getextras ();
Object messages[] = (object[]) bundle.get ("PDUs");
Smsmessage smsmessage[] = new Smsmessage[messages.length]; for (int n = 0; n < messages.length n++) {Smsmessage[n] = SMSMESSAGE.CREATEFROMPDU ((byte[)) messages[n]);//Show F Irst message Toast Toast = Toast.maketext (Context, Received SMS: "+ smsmessage[0].getmessagebody (), toast.length_
LONG);
Toast.show ();

 }

The SMS received by the Android device is in PDU form (Protocol description unit). Android.telephony.gsm.SmsMessage This class can store information about SMS, we can also create a new Smsmessage instance from the received PDU, Toast Interface component can display the received SMS message text in the form of system notification.

To run the program:

Now let's run the application in the emulator and send SMS messages to the emulator. We can send SMS messages to the emulator in the Ddms view (Dalvik Debug Monitor Service) provided by the Eclipse's Android plug-in (in ' Emulator Control Panel; You also need to specify a phone number, but it can be arbitrary.

How to issue broadcast intent:

public static final String music_action= "Com.mythlink.MUSIC";
Intent intent=new Intent ();
Intent.setaction (music_action);
Intent.putextra ("Music_path", Songpath);
This.sendbroadcast (intent);
public static final String music_action= "Com.mythlink.MUSIC";
Intent intent=new Intent ();
Intent.setaction (music_action);
Intent.putextra ("Music_path", Songpath);
This.sendbroadcast (Intent);

Need to write another broadcast receiver:

public class Musicreceiver extends Broadcastreceiver {
 @Override
 the public void onreceive (context context, Intent Intent) {
 Bundle bundle=intent.getextras ();
 String music_path=bundle.getstring ("Music_path");
 Toast Toast=toast.maketext (Context, "Playing music:" +music_path, Toast.length_long);
 Toast.show ();
 }
The public class Musicreceiver extends Broadcastreceiver {
@Override
the public void onreceive Intent Intent) {
Bundle bundle=intent.getextras ();
String music_path=bundle.getstring ("Music_path");
Toast Toast=toast.maketext (Context, "Playing music:" +music_path, Toast.length_long);
Toast.show ();
}


Get contact information:

public class Contactslist extends Listactivity {private ListAdapter madapter;
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 Cursor c=this.getcontentresolver (). query (Contacts.People.CONTENT_URI, NULL, NULL, NULL, NULL);
 This.startmanagingcursor (c);
 String[] Columns=new String[]{contacts.people.name}; Int[] names=new int[]{r.id.song};////////////////madapter = new Simplecursoradapter (this, R.layout.song_item, C,
 columns, names);
 This.setlistadapter (Madapter); @Override protected void Onlistitemclick (ListView l, View v, int position, long id) {Super.onlistitemclick (L, V, POS
 Ition, id);
 Intent i=new Intent (Intent.action_call);
   Cursor C = (Cursor) madapter.getitem (position);
   Long Phoneid = C.getlong (C.getcolumnindex (Contacts.People.PRIMARY_PHONE_ID));
   I.setdata (Contenturis.withappendedid (Contacts.Phones.CONTENT_URI, Phoneid));
 This.startactivity (i); } public class Contactslist extends Listactivity {PrivaTe ListAdapter madapter;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Cursor c=this.getcontentresolver (). query (Contacts.People.CONTENT_URI, NULL, NULL, NULL, NULL);
This.startmanagingcursor (c);
String[] Columns=new String[]{contacts.people.name}; Int[] names=new int[]{r.id.song};////////////////madapter = new Simplecursoradapter (this, R.layout.song_item, C,
columns, names);
This.setlistadapter (Madapter); @Override protected void Onlistitemclick (ListView l, View v, int position, long id) {Super.onlistitemclick (L, V, Positi
on, id);
Intent i=new Intent (Intent.action_call);
Cursor C = (Cursor) madapter.getitem (position);
Long Phoneid = C.getlong (C.getcolumnindex (Contacts.People.PRIMARY_PHONE_ID));
I.setdata (Contenturis.withappendedid (Contacts.Phones.CONTENT_URI, Phoneid));
This.startactivity (i);

 }
}

Add in Androidmanifest.xml:

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

More interested readers of Android-related content can view this site: "Android SMS and phone Operation tips Summary", "Android File Operating skills summary", "Android operation SQLite Database Skills Summary", " Android operations JSON format Data tips summary, "Android Database Operation skills Summary", "Android programming activity Operation Skills Summary", "Android programming development of SD card operation method Summary", "Android Development and advanced tutorial , "The Android resource Operation tips Summary", the Android View tips summary, and the "Android Control usage Summary"

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.