当前位置: 移动技术网 > IT编程>移动开发>Android > Android API开发之SMS短信服务处理和获取联系人的方法

Android API开发之SMS短信服务处理和获取联系人的方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

庐江人事网,安徽交管e点通,莱姿蔓

本文实例讲述了android api开发之sms短信服务处理和获取联系人的方法。分享给大家供大家参考,具体如下:

android api支持开发可以发送和接收sms消息的应用程序。目前我们开发过程中使用的android模拟器还不支持发送sms,但它可以接收sms。现在我们来探索一下android对sms的支持,我们将会构建一个小小的应用程序来监听移动设备(或模拟器)上接收到的sms消息,并将它显示出来。
我们来定义一个intent接收器来处理sms接收事件:

package com.wissen.sms.receiver;
public class smsreceiver extends broadcastreceiver {
  @override
  public void onreceive(context context, intent intent) {
    // todo
  }
}
package com.wissen.sms.receiver;
public class smsreceiver extends broadcastreceiver {
@override
public void onreceive(context context, intent intent) {
// todo
}
}

我们需要对这个intent接收器进行配置以使它能获取sms接收事件, android.provider.telephony.sms_received 这个事件状态表示了sms已被接收。我们可以在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>

为了能让我们的应用能接收sms,我们得先进行权限的指定,可以在androidmanifest.xml中如下配置:

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

现在,我们的intent接收器就可以在android设备接收到sms的时候被调用了,余下的事情就是去获取和显示接收到的sms消息文本了:

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 first message
   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 first message
toast toast = toast.maketext(context, "received sms: " + smsmessage[0].getmessagebody(),        toast.length_long);
toast.show();
}

android设备接收到的sms是以pdu形式的(protocol description unit)。android.telephony.gsm.smsmessage这个类可以储存sms的相关信息,我们也可以从接收到的pdu中创建新的smsmessage实例,toast界面组件可以以系统通知的形式来显示接收到的sms消息文本。

运行程序:

现在让我们来在模拟器中运行这个应用程序,以及发送sms消息到这个模拟器上。我们可以在eclipse的android插件所提供的ddms视图(dalvik debug monitor service)中发送sms消息到模拟器上(在'emulator control'面板中;另外需要指定电话电话号码,不过可以是任意的)

发出广播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);

需要再写一个广播接收器:

public class musicreceiver extends broadcastreceiver {
 @override
 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();
 }
}
public class musicreceiver extends broadcastreceiver {
@override
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();
}
}

获取联系人信息:

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

在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>

更多关于android相关内容感兴趣的读者可查看本站专题:《android短信与电话操作技巧汇总》、《android文件操作技巧汇总》、《android操作sqlite数据库技巧总结》、《android操作json格式数据技巧总结》、《android数据库操作技巧总结》、《android编程之activity操作技巧总结》、《android编程开发之sd卡操作方法汇总》、《android开发入门与进阶教程》、《android资源操作技巧汇总》、《android视图view技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网