Android监听手机电话状态与发送邮件通知来电号码的方法(基于PhoneStateListene实现)
作者:马到成功 时间:2022-12-07 09:03:44
本文实例讲述了Android监听手机电话状态与发送邮件通知来电号码的方法。分享给大家供大家参考,具体如下:
在android中可以用PhoneStateListener来聆听手机电话状态(比如待机、通话中、响铃等)。本例是通过它来监听手机电话状态,当手机来电时,通过邮件将来电号码发送到用户邮箱的例子。具体程序如下:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.TextView;
public class A07Activity extends Activity {
private TextView tv;//用来显示电话状态
private String emailReceiver="16*****85@qq.com"; //邮箱地址
private String emailSubject="你有来电信息,请查收!"; //作为邮件主题
/** Called when the activity is first created. */
@SuppressWarnings("static-access")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView)findViewById(R.id.tv);
PhoneCallListener pcl=new PhoneCallListener();
TelephonyManager tm=(TelephonyManager)getSystemService(TELEPHONY_SERVICE);
tm.listen(pcl, pcl.LISTEN_CALL_STATE);
}
public class PhoneCallListener extends PhoneStateListener{
public void onCallStateChanged(int state,String incomingNumber){ //需要重写onCallStateChanged方法
switch(state){
case TelephonyManager.CALL_STATE_IDLE:
tv.setText("CALL_STATE_IDLE");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
tv.setText("CALL_STATE_OFFHOOK");
break;
case TelephonyManager.CALL_STATE_RINGING:
tv.setText("来电号码"+incomingNumber); //如果有人打来电话,就会自动发送邮件到邮箱通知用户来电号码
//设置来电时发送邮件
Intent i=new Intent(android.content.Intent.ACTION_SEND);
i.setType("plain/text");
i.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{emailReceiver.toString()});
i.putExtra(android.content.Intent.EXTRA_SUBJECT, emailSubject.toString());
i.putExtra(android.content.Intent.EXTRA_TEXT, "来电电话"+incomingNumber);
startActivity(Intent.createChooser(i, "来电信息"));
break;
default:
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
}
其中还需要在AndroidManifest.xml中添加几个相应的权限:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.a07"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".A07Activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
</manifest>
希望本文所述对大家Android程序设计有所帮助。
标签:Android,监听手机状态,发送邮件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java全面细致讲解Cookie与Session及kaptcha验证码的使用
2021-09-03 23:10:56
IDEA中JetBrains Mono字体的正确安装姿势
2022-03-12 12:21:40
![](https://img.aspxhome.com/file/2023/4/87194_0s.jpg)
SpringBoot、mybatis返回树结构的数据实现
2022-05-12 18:56:08
![](https://img.aspxhome.com/file/2023/5/77365_0s.png)
C#笔记之EF Code First 数据模型 数据迁移
2022-07-03 05:27:58
![](https://img.aspxhome.com/file/2023/6/78166_0s.jpg)
纯注解版spring与mybatis的整合过程
2022-12-14 00:25:06
![](https://img.aspxhome.com/file/2023/7/119797_0s.jpg)
C# 委托的三种调用示例(同步调用 异步调用 异步回调)
2023-06-09 10:29:56
详解Kotlin的空指针处理
2022-06-03 06:57:58
![](https://img.aspxhome.com/file/2023/6/81956_0s.jpg)
Android人脸识别Demo竖屏YUV方向调整和图片保存(分享)
2022-11-24 00:47:42
![](https://img.aspxhome.com/file/2023/7/138517_0s.jpg)
Android如何判断页面是否全屏
2021-08-31 14:03:22
flutter 中监听滑动事件
2021-10-05 11:39:00
SpringMVC 参数绑定相关知识总结
2022-06-05 12:50:54
关于Mybatis-Plus Update更新策略问题
2022-04-14 19:29:24
![](https://img.aspxhome.com/file/2023/6/61276_0s.jpg)
RocketMQ4.5.2 修改mqnamesrv 和 mqbroker的日志路径操作
2023-11-28 14:03:18
![](https://img.aspxhome.com/file/2023/0/60370_0s.png)
Java经典面试题最全汇总208道(六)
2023-11-23 17:52:45
![](https://img.aspxhome.com/file/2023/4/59064_0s.jpg)
Android实现左滑删除控件
2023-03-02 21:36:49
![](https://img.aspxhome.com/file/2023/6/106826_0s.gif)
Android调用摄像头拍照开发教程
2023-05-24 11:33:33
![](https://img.aspxhome.com/file/2023/3/137593_0s.png)
spring MVC实现简单登录功能
2023-11-13 05:20:48
![](https://img.aspxhome.com/file/2023/3/59563_0s.jpg)
Android利用MPAndroidChart绘制曲线图表的基础教程
2022-02-03 18:16:18
![](https://img.aspxhome.com/file/2023/1/120051_0s.gif)
如何解决SpringBoot集成百度UEditor图片上传后直接访问404
2022-03-03 19:27:13
![](https://img.aspxhome.com/file/2023/5/128235_0s.png)
解决ThingsBoard编译报错问题:Failure to find org.gradle:gradle-tooling-api:jar:6.3
2021-11-20 16:24:22
![](https://img.aspxhome.com/file/2023/7/62117_0s.png)