手機監聽弹出浮动窗体
描述就不多说了...直接上代码了
package com.iaiai.activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
*
* <br/>
* Title: TelListener.java<br/>
* E-Mail: [email protected]<br/>
* QQ: 176291935<br/>
* Http: iaiai.iteye.com<br/>
* Create time: 2013-8-16 上午10:44:27<br/>
* <br/>
*
* @author 丸子
* @version 0.0.1
*/
public class TelListener extends PhoneStateListener {
private Context context;
private WindowManager wm;
private LinearLayout layout;
private TextView tv;
public TelListener(Context context) {
this.context = context;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
// TODO Auto-generated method stub
super.onCallStateChanged(state, incomingNumber);
if (state == TelephonyManager.CALL_STATE_RINGING) {
wm = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.format = PixelFormat.RGBA_8888;
params.gravity=Gravity.LEFT|Gravity.TOP; //调整悬浮窗口至左上角
layout = new LinearLayout(context);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
layout.setBackgroundColor(Color.WHITE);
tv = new TextView(context);
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv.setText("这是悬浮窗口,来电号码:" + incomingNumber);
layout.addView(tv);
wm.addView(layout, params);
} else if (state == TelephonyManager.CALL_STATE_IDLE) {
if (wm != null) {
wm.removeView(layout);
}
}
}
}
package com.iaiai.activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
/**
*
* <br/>
* Title: PhoneService.java<br/>
* E-Mail: [email protected]<br/>
* QQ: 176291935<br/>
* Http: iaiai.iteye.com<br/>
* Create time: 2013-8-16 上午10:49:06<br/>
* <br/>
*
* @author 丸子
* @version 0.0.1
*/
public class PhoneService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.i("**************","启动服务");
TelephonyManager telM = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
telM.listen(new TelListener(this), PhoneStateListener.LISTEN_CALL_STATE);
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
}
package com.iaiai.activity;
import android.app.AlertDialog;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.WindowManager;
/**
*
* <br/>
* Title: PhoneStatReceiver.java<br/>
* E-Mail: [email protected]<br/>
* QQ: 176291935<br/>
* Http: iaiai.iteye.com<br/>
* Create time: 2013-8-16 上午10:15:24<br/>
* <br/>
*
* @author 丸子
* @version 0.0.1
*/
public class PhoneStatReceiver extends BroadcastReceiver {
private static final String TAG = "**********";
private static boolean incomingFlag = false;
private static String incoming_number = null;
@Override
public void onReceive(Context context, Intent intent) {
Intent interfaceService = new Intent(context, PhoneService.class);
context.startService(interfaceService);
// // 如果是拨打电话
// if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
// incomingFlag = false;
// String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
// Log.i(TAG, "call OUT:" + phoneNumber);
// AlertDialog alertDialog = new AlertDialog.Builder(context).setPositiveButton("确定", null)
// .setTitle(R.string.app_name).setMessage("打出:" + phoneNumber).create();
// alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); // 窗口可以获得焦点,响应操作
// alertDialog.show();
// } else {
// // 如果是来电
// TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
//
// switch (tm.getCallState()) {
// case TelephonyManager.CALL_STATE_RINGING:
// incomingFlag = true;// 标识当前是来电
// incoming_number = intent.getStringExtra("incoming_number");
// Log.i(TAG, "RINGING :" + incoming_number);
// AlertDialog alertDialog = new AlertDialog.Builder(context).setPositiveButton("确定", null)
// .setTitle(R.string.app_name).setMessage("来电:" + incoming_number).create();
// alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); // 窗口可以获得焦点,响应操作
// alertDialog.show();
// break;
// case TelephonyManager.CALL_STATE_OFFHOOK:
// if (incomingFlag) {
// Log.i(TAG, "incoming ACCEPT :" + incoming_number);
// AlertDialog alertDialog1 = new AlertDialog.Builder(context).setPositiveButton("确定", null)
// .setTitle(R.string.app_name).setMessage("接通:" + incoming_number).create();
// alertDialog1.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); // 窗口可以获得焦点,响应操作
// alertDialog1.show();
// }
// break;
//
// case TelephonyManager.CALL_STATE_IDLE:
// if (incomingFlag) {
// Log.i(TAG, "incoming IDLE");
// AlertDialog alertDialog2 = new AlertDialog.Builder(context).setPositiveButton("确定", null)
// .setTitle(R.string.app_name).setMessage("挂断").create();
// alertDialog2.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); // 窗口可以获得焦点,响应操作
// alertDialog2.show();
// }
// break;
// }
// }
}
}
package com.iaiai.activity;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Intent;
import android.os.Bundle;
/**
*
*
* <br/>
* Title: PhoneListenerActivity.java<br/>
* E-Mail: [email protected]<br/>
* QQ: 176291935<br/>
* Http: iaiai.iteye.com<br/>
* Create time: 2013-8-16 上午11:23:34<br/>
* <br/>
*
* @author 丸子
* @version 0.0.1
*/
public class PhoneListenerActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
service();
}
private void service() {
if (!serviceIsStart(
((ActivityManager) getSystemService(ACTIVITY_SERVICE)).getRunningServices(Integer.MAX_VALUE),
"com.iaiai.activity.PhoneService")) {
Intent intent = new Intent(this, PhoneService.class);
startService(intent);
}
}
// 通过Service的类名来判断是否启动某个服务
private boolean serviceIsStart(List<ActivityManager.RunningServiceInfo> mServiceList, String className) {
for (int i = 0; i < mServiceList.size(); i++) {
if (className.equals(mServiceList.get(i).service.getClassName())) {
return true;
}
}
return false;
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.iaiai.activity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<!-- 访问网络 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- 验证网络状态 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!-- 写入外部存储 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 读取电话状态访问电话状态 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- 获取WiFi状态 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<!-- 系统对话框 -->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".PhoneListenerActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".PhoneStatReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter >
<!-- <action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" /> -->
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
<service android:name=".PhoneService" android:enabled="true" android:process=":remote" />
</application>
</manifest>