欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中简单的电话管理与短信管理App编写实例

程序员文章站 2024-02-28 22:10:28
android电话管理器(telephonymanger)实例: telephonymanger是管理电话状态、网络信息的服务类。 添加权限:

android电话管理器(telephonymanger)实例:
telephonymanger是管理电话状态、网络信息的服务类。
添加权限:

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

逻辑功能:

package com.example.telephonystatus; 
 
import java.io.filenotfoundexception; 
import java.io.outputstream; 
import java.io.printstream; 
import java.util.arraylist; 
import java.util.date; 
import java.util.hashmap; 
import java.util.list; 
import java.util.map; 
 
import android.os.bundle; 
import android.app.activity; 
import android.content.context; 
import android.telephony.phonestatelistener; 
import android.telephony.telephonymanager; 
import android.view.menu; 
import android.widget.listview; 
import android.widget.simpleadapter; 
 
public class mainactivity extends activity { 
 
  private listview list1; 
 
  // 声明代表状态名的数组 
  private string[] statusname; 
  // 声明代表手机状态名的集合 
  private arraylist<string> statusvalues = new arraylist<string>(); 
 
  @override 
  protected void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.activity_main); 
    list1 = (listview) findviewbyid(r.id.list1); 
    // 获取系统的telephonymanager 
    telephonymanager tele = (telephonymanager) getsystemservice(context.telephony_service); 
    // 获取各种状态名的数组 
    statusname = getresources().getstringarray(r.array.statusnames); 
    // 获取sim卡的状态的数组 
    string[] simtype = getresources().getstringarray(r.array.simstate); 
    // 获取电话网络类型的数组 
    string[] phonetype = getresources().getstringarray(r.array.phonetype); 
    // 获取设备编号 
    statusvalues.add(tele.getdeviceid()); 
    // 获取系统平台的版本 
    statusvalues.add(tele.getdevicesoftwareversion() != null ? tele 
        .getdevicesoftwareversion() : "未知"); 
    // 获取网络运营代号 
    statusvalues.add(tele.getnetworkoperator()); 
    // 获取网络运营商的名称 
    statusvalues.add(tele.getnetworkoperatorname()); 
    // 获取手机网络的类型 
    statusvalues.add(phonetype[tele.getphonetype()]); 
    // 获取设为所在的位置 
    statusvalues.add(tele.getcelllocation() != null ? tele 
        .getcelllocation().tostring() : "未知"); 
    // 获取sim卡的国别 
    statusvalues.add(tele.getsimcountryiso()); 
    // 获取sim卡的序列号 
    statusvalues.add(tele.getsimserialnumber()); 
    // 获取sim卡的状态 
    statusvalues.add(simtype[tele.getsimstate()]); 
 
    list<map<string, object>> list = new arraylist<map<string, object>>(); 
    for (int i = 0; i < statusvalues.size(); i++) { 
      hashmap<string, object> hasp = new hashmap<string, object>(); 
      hasp.put("name", statusname[i]); 
      hasp.put("values", statusvalues.get(i)); 
      list.add(hasp); 
    } 
    simpleadapter simple = new simpleadapter(this, list, r.layout.items, 
        new string[] { "name", "values" }, new int[] { r.id.text1, 
            r.id.text2 }); 
    list1.setadapter(simple); 
    // 创建一个电话监听器 
        phonestatelistener listener = new phonestatelistener() { 
 
          // 监听电话呼叫状态 
          @override 
          public void oncallstatechanged(int state, string incomingnumber) { 
 
            switch (state) { 
            // 无任何状态 
            case telephonymanager.call_state_idle: 
              break; 
            case telephonymanager.call_state_offhook: 
              break; 
            // 来电响铃 
            case telephonymanager.call_state_ringing: 
              outputstream os = null; 
              try { 
                os = openfileoutput("phonelist", mode_append); 
              } catch (filenotfoundexception e) { 
                e.printstacktrace(); 
              } 
              printstream ps = new printstream(os); 
              // 讲电话号码记录到文件中 
              ps.println(new date() + "来电:" + incomingnumber); 
              ps.close(); 
              break; 
            default: 
              break; 
            } 
            super.oncallstatechanged(state, incomingnumber); 
          } 
 
        }; 
        tele.listen(listener, phonestatelistener.listen_call_state); 
  } 
 
  @override 
  public boolean oncreateoptionsmenu(menu menu) { 
    // inflate the menu; this adds items to the action bar if it is present. 
    getmenuinflater().inflate(r.menu.main, menu); 
    return true; 
  } 
 
} 

android短信管理器(smsmanager)实例
需要注册的权限:

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

群发短信功能:

package com.android.xiong.groupsend; 
 
import java.util.arraylist; 
import java.util.list; 
 
import android.app.activity; 
import android.app.alertdialog; 
import android.app.pendingintent; 
import android.content.dialoginterface; 
import android.content.intent; 
import android.database.cursor; 
import android.os.bundle; 
import android.provider.contactscontract; 
import android.telephony.smsmanager; 
import android.view.menu; 
import android.view.view; 
import android.view.viewgroup; 
import android.view.view.onclicklistener; 
import android.widget.baseadapter; 
import android.widget.button; 
import android.widget.checkbox; 
import android.widget.edittext; 
import android.widget.listview; 
import android.widget.toast; 
 
public class mainactivity extends activity { 
 
  private button bt1, bt2; 
  private edittext ed1, ed2; 
  private smsmanager smanger; 
  list<string> sendlist = new arraylist<string>(); 
 
  @override 
  protected void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.activity_main); 
    bt1 = (button) findviewbyid(r.id.bt1); 
    bt2 = (button) findviewbyid(r.id.bt2); 
    ed1 = (edittext) findviewbyid(r.id.ed1); 
    ed2 = (edittext) findviewbyid(r.id.ed2); 
    // 获取smsmanger 
    smanger = smsmanager.getdefault(); 
    bt1.setonclicklistener(new onclicklistener() { 
 
      @override 
      public void onclick(view v) { 
        for (string send : sendlist) { 
          // 创建pendintent对象 
          pendingintent ped = pendingintent.getactivity( 
              mainactivity.this, 0, new intent(), 0); 
          // 发送信息 
          smanger.sendtextmessage(send, null, ed2.gettext() 
              .tostring(), ped, null); 
        } 
        // 提示消息发送完毕 
        toast.maketext(mainactivity.this, "短信群发完", toast.length_long) 
            .show(); 
      } 
    }); 
    bt2.setonclicklistener(new onclicklistener() { 
 
      @override 
      public void onclick(view v) { 
        // 查看联系人的电话号码 
        final cursor cursor = getcontentresolver().query( 
            contactscontract.commondatakinds.phone.content_uri, 
            null, null, null, null); 
        baseadapter adapter = new baseadapter() { 
 
          @override 
          public view getview(int position, view convertview, 
              viewgroup parent) { 
            cursor.movetoposition(position); 
            checkbox rb = new checkbox(mainactivity.this); 
            // 获取联系人的电话号码 并去掉中间的中画、空格 
            string number = cursor 
                .getstring( 
                    cursor.getcolumnindex(contactscontract.commondatakinds.phone.number)) 
                .replace("-", ""); 
            rb.settext(number); 
            // 如果该号码已经加入发送人名单,默认勾选该号码 
            if (sendlist.contains(number)) { 
              rb.setchecked(true); 
            } 
            return rb; 
          } 
 
          @override 
          public long getitemid(int position) { 
            // todo auto-generated method stub 
            return position; 
          } 
 
          @override 
          public object getitem(int position) { 
            // todo auto-generated method stub 
            return position; 
          } 
 
          @override 
          public int getcount() { 
            // todo auto-generated method stub 
            return cursor.getcount(); 
          } 
        }; 
        // 加载list.xml布局文件对应的view 
        view selectview = getlayoutinflater().inflate(r.layout.item, 
            null); 
        final listview listview = (listview) selectview 
            .findviewbyid(r.id.list1); 
        listview.setadapter(adapter); 
        new alertdialog.builder(mainactivity.this).setview(selectview).setpositivebutton("确定", new dialoginterface.onclicklistener() { 
           
          @override 
          public void onclick(dialoginterface dialog, int which) { 
            //清空sendlist集合 
            sendlist.clear(); 
            //遍历listview组件的每个列表项 
            for(int i=0;i<listview.getcount();i++){ 
              checkbox checkbox=(checkbox)listview.getchildat(i); 
              //如果该列表项被勾选 
              if(checkbox.ischecked()){ 
                //添加到该列表项中 
                sendlist.add(checkbox.gettext().tostring()); 
                ed1.append(checkbox.gettext().tostring()+","); 
              } 
            } 
             
          } 
        }).show(); 
      } 
    }); 
  } 
 
  @override 
  public boolean oncreateoptionsmenu(menu menu) { 
    // inflate the menu; this adds items to the action bar if it is present. 
    getmenuinflater().inflate(r.menu.main, menu); 
    return true; 
  } 
 
} 

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:orientation="vertical" 
  tools:context=".mainactivity" > 
   
  <edittext  
    android:id="@+id/ed1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/> 
  <edittext  
    android:id="@+id/ed2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    /> 
  <button  
    android:id="@+id/bt2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="获取联系人"/> 
  <button  
    android:id="@+id/bt1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="发送信息"/> 

</linearlayout> 

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:orientation="vertical" > 
 
 
  <listview 
    android:id="@+id/list1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
  </listview> 
 
 
</linearlayout>