来电监听
来源:互联网 发布:linux常用软件下载 编辑:程序博客网 时间:2024/06/10 07:21
在main.xml中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00ff66">
</LinearLayout>
在PhoneBroadcastReceiver.java中:
package com.li.phone;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class PhoneBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, PhoneService.class));
}
}
在PhoneService.java中:
package com.li.phone;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
public class PhoneService extends Service {
private TelephonyManager telephony = null;
@Override
public void onCreate() { // 服务创建的时候操作
super.onCreate();
this.telephony = (TelephonyManager) super
.getSystemService(Context.TELEPHONY_SERVICE);
this.telephony.listen(new PhoneStateListenerImpl(),
PhoneStateListener.LISTEN_CALL_STATE); // 设置监听操作
}
private class PhoneStateListenerImpl extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE: // 挂断电话
break;
case TelephonyManager.CALL_STATE_RINGING: // 领音响起
System.out.println("拨入电话号码:"
+ incomingNumber
+ ",拨入时间:"
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(new Date()));
break;
case TelephonyManager.CALL_STATE_OFFHOOK: // 接听电话
break;
}
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
在MyPhoneDemo.java中:
package com.li.phone;
import android.app.Activity;
import android.os.Bundle;
public class MyPhoneDemo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
修改AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.li.phone"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyPhoneDemo"
android:label="@string/title_activity_my_phone_demo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".PhoneService" />
<receiver android:name=".PhoneBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
- 来电监听
- 来电监听
- symbian 监听来电状态
- symbian 监听来电状态
- 监听手机来电
- 监听来电状态
- 监听手机来电
- 监听手机来电
- Android来电监听
- 监听来电情况(静音)
- android 手机来电监听
- Service-监听手机来电
- 监听手机来电PhoneStateListener
- 来电监听及录音
- 监听手机来电
- 监听手机来电
- Android中监听来电
- iOS监听来电
- AGI 参考
- 少壮不努力
- hdu2428(单调性+二分)
- Struts2 Interceptors详解
- 笔试面试中常见的排序算法
- 来电监听
- nyoj-28-大数阶乘
- [Editing]Get current active view reference instance
- poj 1562 Oil Deposits
- 进程控制天字第1号系统调用——fork
- 实现Servlet接口——理解Servlet的生命周期
- 来去电监听
- floyd求最小环
- 对struct访问成员的理解