来去电悬浮窗提示号码,联系人,联系人头像

2019-04-14 18:12发布

本篇内容是本人整理,测试,调整,网上的相关代码之后得到的。如果侵犯您的相关权益,请通知本人,本人会立即申明或者删除,谢谢! 1.添加来去电广播接收。 建立继承BroadcastReceiver的类PhoneCall(类名随意)。 public class PhoneCall extends BroadcastReceiver {


//private static boolean mIncomingFlag = false;
private static String mIncomingNumber = null;


@Override
public void onReceive(Context context, Intent intent) {
// 如果是拨打电话
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
//mIncomingFlag = false;
String phoneNumber = intent
.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Intent mintent = new Intent(context, PhoneService.class);
mintent.putExtra("phoneNum", phoneNumber);
context.startService(mintent);
} else {
// 如果是来电
TelephonyManager tManager = (TelephonyManager) context
.getSystemService(Service.TELEPHONY_SERVICE);
switch (tManager.getCallState()) {
case TelephonyManager.CALL_STATE_RINGING:
//mIncomingFlag = true;
mIncomingNumber = intent.getStringExtra("incoming_number");
Intent mintent = new Intent(context, PhoneService.class);
mintent.putExtra("phoneNum", mIncomingNumber);
context.startService(mintent);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
case TelephonyManager.CALL_STATE_IDLE:
//if (mIncomingFlag) {
Intent endintent = new Intent(context, PhoneService.class);
context.stopService(endintent);
//}
break;
}
}
}
}
接着在AndroidManifest.xml    文件中注册  
             
                 
                 
           
   
再添加相关权限

2.在上面的代码中我们启动了一个服务service,并且启动时将去电号码或者来电号码传递给了service 添加类PhoneService(类名随意)继承自Service public class PhoneService extends Service {
// 定义浮动窗口布局
LinearLayout mFloatLayout;
WindowManager.LayoutParams wmParams;
// 创建浮动窗口设置布局参数的对象
WindowManager mWindowManager;
// 悬浮窗按钮,显示名称
Button mFloatView;
// 悬浮窗按钮,显示号码
Button mFloatNum;
// 悬浮窗按钮,显示信息
Button mFloatInfo;
// 悬浮窗按钮,接通电话
Button GetPhone;
// 悬浮窗按钮,拒接电话
Button EndPhone;
// 悬浮窗按钮,显示图像
ImageView mFloatIm;
// 悬浮窗显示的电话号码信息
String infonum = "";
// 悬浮窗显示的名称信息
String infoname = "";
// 悬浮窗显示的提示信息
String infotip=""; //获取到所有联系人信息,根据号码得到姓名  其中ContactInfo 为自定类 private List infos = new ArrayList();
private static final String TAG = "FxService";

/*
* (non-Javadoc) //只在服务第一次启动的时候调用,并且在onStartCommand前面 所以 不能在这里创建窗口,不然收不到信息
*/
@Override
public void onCreate() {
super.onCreate();
// Log.i(TAG, "oncreat");
// createFloatView();
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
infonum = intent.getStringExtra("phoneNum");// 得到来电号码
infos = ApplicationSS.getInstance().getInfos();//在Application类中定义,其内容后面给出
for (int i = 0; i < infos.size(); i++) {
if (infos.get(i).getPhone().equals(infonum)) {
infoname = infos.get(i).getName();
break;
}
}

if (infoname == "") {
infoname = "陌生号码";
}
createFloatView();//创建窗口
return super.onStartCommand(intent, flags, startId);
}


@Override
public IBinder onBind(Intent intent) {
return null;
}


private boolean IsRoaming() {
// 如果手机处于漫游状态,则显示提示信息,否则隐藏提示
ServiceState serviceState = new ServiceState();
boolean roaming = serviceState.getRoaming();
return roaming;
}


private void createFloatView() {
wmParams = new WindowManager.LayoutParams();
// 获取WindowManagerImpl.CompatModeWrapper
mWindowManager = (WindowManager) getApplication().getSystemService(
getApplication().WINDOW_SERVICE);
// 设置window type
wmParams.type = LayoutParams.TYPE_PHONE; // 不在状态栏之上
// 设置窗口类型在所有窗口之上,包括状态栏,设置为此属性,则不能获得焦点,点击事件
// wmParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
// 设置图片格式,效果为背景透明
wmParams.format = PixelFormat.RGBA_8888;
// 设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作)
wmParams.flags =
// LayoutParams.FLAG_NOT_TOUCH_MODAL |
LayoutParams.FLAG_NOT_FOCUSABLE
// LayoutParams.FLAG_NOT_TOUCHABLE
;


// 调整悬浮窗显示的停靠位置为左侧置顶
wmParams.gravity = Gravity.START | Gravity.TOP;


// 以屏幕左上角为原点,设置x、y初始值
wmParams.x = 0;
wmParams.y = 0;


/*
* // 设置悬浮窗口长宽数据 wmParams.width = 200; wmParams.height = 80;
*/


// 设置悬浮窗口长宽数据
wmParams.width = WindowManager.LayoutParams.MATCH_PARENT;
wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;


LayoutInflater inflater = LayoutInflater.from(getApplication());
// 获取浮动窗口视图所在布局
mFloatLayout = (LinearLayout) inflater.inflate(R.layout.float_layout,
null);
// 设置颜 {MOD}
mFloatLayout.setBackgroundColor(getResources().getColor(
R.color.peachpuff));
// 添加mFloatLayout
mWindowManager.addView(mFloatLayout, wmParams);


/*
* Log.i(TAG, "mFloatLayout-->left" + mFloatLayout.getLeft());
* Log.i(TAG, "mFloatLayout-->right" + mFloatLayout.getRight());
* Log.i(TAG, "mFloatLayout-->top" + mFloatLayout.getTop()); Log.i(TAG,
* "mFloatLayout-->bottom" + mFloatLayout.getBottom());
*/


// 浮动窗口按钮
mFloatView = (Button) mFloatLayout.findViewById(R.id.float_id);
mFloatView.setBackground(getResources().getDrawable(R.drawable.shape));// 设置shape样式
mFloatView.setTextSize(35);// 设置字体信息
mFloatView.setTextColor(Color.BLACK);
mFloatView.setText(infonum);// 设置信息
mFloatNum = (Button) mFloatLayout.findViewById(R.id.float_num);
mFloatNum.setBackground(getResources().getDrawable(R.drawable.shape));// 设置shape样式
mFloatNum.setTextSize(35);// 设置字体信息
mFloatNum.setTextColor(Color.BLACK);
mFloatNum.setText(infoname);// 设置信息
GetPhone = (Button) mFloatLayout.findViewById(R.id.float_getphone);
GetPhone.setBackground(getResources().getDrawable(R.drawable.selector));// 设置shape样式
GetPhone.setTextSize(35);// 设置字体信息
Point size = new Point();
mWindowManager.getDefaultDisplay().getSize(size);// 得到屏幕大小
GetPhone.setWidth(size.x / 2);
GetPhone.setTextColor(Color.BLACK);
EndPhone = (Button) mFloatLayout.findViewById(R.id.float_endphone);
EndPhone.setBackground(getResources().getDrawable(R.drawable.selector));// 设置shape样式
EndPhone.setTextSize(35);// 设置字体信息
EndPhone.setWidth(size.x / 2);
EndPhone.setTextColor(Color.BLACK);
if (IsRoaming()) {
mFloatInfo = (Button) mFloatLayout.findViewById(R.id.float_info);
mFloatInfo.setVisibility(View.VISIBLE);
mFloatInfo.setBackground(getResources().getDrawable(
R.drawable.shape));// 设置shape样式
mFloatInfo.setTextSize(35);// 设置字体信息
mFloatInfo.setTextColor(Color.BLACK);
mFloatInfo.setText(infotip);// 设置信息
}


// View.MeasureSpec.makeMeasureSpec(int size,int mode)根据提供的大小值和模式创建一个测量值
/*
* 模式 UNSPECIFIED 无限制 parent view不约束child AT_MOST 最多的 wrap_content child
* view可以在parent view范围内取值 EXACTLY 准确的 fill_parent(例如50dip) parent
* view为child view指定固定大小
*/
mFloatLayout.measure(View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
// Log.i(TAG, "Width/2--->" + mFloatView.getMeasuredWidth() / 2);
// Log.i(TAG, "Height/2--->" + mFloatView.getMeasuredHeight() / 2);
// 设置监听浮动窗口的触摸移动
// 设置监听浮动窗口的触摸移动   代码被注释,取消注释即可运行
/*
* mFloatView.setOnTouchListener(new OnTouchListener() {

* @Override public boolean onTouch(View v, MotionEvent event) { // TODO
* Auto-generated method stub //getRawX是触摸位置相对于屏幕的坐标,getX是相对于按钮的坐标
* wmParams.x = (int) event.getRawX() - mFloatView.getMeasuredWidth()/2;
* //Log.i(TAG, "Width/2--->" + mFloatView.getMeasuredWidth()/2);
* Log.i(TAG, "RawX" + event.getRawX()); Log.i(TAG, "X" + event.getX());
* //25为状态栏的高度 wmParams.y = (int) event.getRawY() -
* mFloatView.getMeasuredHeight()/2 - 25; // Log.i(TAG, "Width/2--->" +
* mFloatView.getMeasuredHeight()/2); Log.i(TAG, "RawY" +
* event.getRawY()); Log.i(TAG, "Y" + event.getY()); //刷新
* mWindowManager.updateViewLayout(mFloatLayout, wmParams); return
* false; } });
*/
/*
* mFloatView.setOnClickListener(new OnClickListener() {

* @Override public void onClick(View v) {
* Toast.makeText(PhoneService.this, "onClick",
* Toast.LENGTH_SHORT).show(); //endCall(getApplicationContext()); } });
*/
EndPhone.setOnTouchListener(new OnTouchListener() {
// 挂断
@Override
public boolean onTouch(View v, MotionEvent event) {
// mActLayout.setVisibility(View.INVISIBLE);//隐藏显示,但是占据布局
switch (event.getAction()) {


case MotionEvent.ACTION_DOWN: {
// 按下时触发
}


case MotionEvent.ACTION_MOVE: {
// 移动时触发
}


case MotionEvent.ACTION_UP: {
// 触摸后触发
EndPhone.setVisibility(View.INVISIBLE);
GetPhone.setVisibility(View.INVISIBLE);
endCall(getApplicationContext());
// stopSelf();
// 防止接收不到广播,点击挂断后结束service
Intent intent = new Intent(getApplicationContext(),
PhoneService.class);
// getApplication().stopService(intent);
getApplication().stopService(intent);
// GetPhone.performClick();//模拟点击按钮
}
}
return false;
}
});
/*
* GetPhone.setOnClickListener(new OnClickListener() {

* @Override public void onClick(View v) {
* endCall(getApplicationContext()); } });
*/
}


/**
* 挂断电话
*/
public static synchronized void endCall(Context ctx) {
TelephonyManager mTelMgr = (TelephonyManager) ctx
.getSystemService(Service.TELEPHONY_SERVICE);
Class c = TelephonyManager.class;
try {
Method getITelephonyMethod = c.getDeclaredMethod("getITelephony",
(Class[]) null);
getITelephonyMethod.setAccessible(true);
ITelephony iTelephony = null;
// System.out.println("End call.");
iTelephony = (ITelephony) getITelephonyMethod.invoke(mTelMgr,
(Object[]) null);
iTelephony.endCall();
} catch (Exception e) {
e.printStackTrace();
// System.out.println("Fail to answer ring call.");
}
}


@Override
public void onDestroy() {
if (mFloatLayout != null) {
mWindowManager.removeView(mFloatLayout);
}
super.onDestroy();
}

}
其中用到的类 ContactInfo   通讯录联系人信息
public class ContactInfo {
private String name;
private String phone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
ApplicationSS  继承自Application   ,需要在AndroidManifest.xml文件的application节点中添加android:name="包名.ApplicationSS",其中包名用自己工程的
public class  ApplicationSS extends Application {
/** 手机联系人 必须new出来 */
private List infos;
public List getInfos() {
return infos;
}
private static ApplicationSS mInstance;


public static ApplicationSS getInstance() {
return mInstance;
}
//使用工具类
private HelpTool mHelpTool;
@Override
public void onCreate() {
infos = new ArrayList();
mHelpTool = new HelpTool();
infos = mHelpTool.readContacts(this);
mInstance=this;
super.onCreate();
}
}
上面又用到工具类  HelpTool 新建类HelpTool,然后实现成员函数 /**
* 获取所有联系人内容

* @param context
* @param address
* @return
*/

public List readContacts(Context context) {
List infos = new ArrayList();
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null);
while (cursor.moveToNext()) {
ContactInfo contactInfo = new ContactInfo();
String displayName = cursor.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = cursor.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactInfo.setName(displayName);
contactInfo.setPhone(number);
infos.add(contactInfo);
contactInfo = null;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
return infos;
}

其中用到的xml配置文件 res/layout目录下   float_layout.xml文件主要配置悬浮窗UI界面, 其中使用到的图片请自行添加并更改图片文件名
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >


            android:id="@+id/float_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="你好" />


            android:id="@+id/float_num"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="你好" />


            android:id="@+id/float_info"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="你好"
        android:visibility="gone" />


            android:id="@+id/float_im"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/ali" />


            android:id="@+id/float_actphone"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >


                    android:id="@+id/float_endphone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="拒接" />
                    android:id="@+id/float_getphone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="接听" />
   


按钮样式和各种状态的效果  按下、获取焦点等 res/drawable/目录下 selector.xml


     
     
     
     

shape.xml

android:shape="rectangle">

android:angle="45" />

android:bottom="7dp" />











shapepress.xml

android:shape="rectangle">

android:angle="45" />

android:bottom="7dp" />












其中用到了color.xml,来自网上 res/value/color.xml





#FFFFFF


#FFFFF0


#FFFFE0


#FFFF00


#FFFAFA


#FFFAF0


#FFFACD


#FFF8DC


#FFF5EE


#FFF0F5


#FFEFD5


#FFEBCD


#FFE4E1


#FFE4C4


#FFE4B5


#FFDEAD


#FFDAB9


#FFD700


#FFC0CB


#FFB6C1


#FFA500


#FFA07A


#FF8C00


#FF7F50


#FF69B4


#FF6347


#FF4500


#FF1493


#FF00FF


#FF00FF


#FF0000


#FDF5E6


#FAFAD2


#FAF0E6


#FAEBD7


#FA8072


#F8F8FF


#F5FFFA


#F5F5F5


#F5F5DC


#F5DEB3


#F4A460


#F0FFFF


#F0FFF0


#F0F8FF


#F0E68C


#F08080


#EEE8AA


#EE82EE


#E9967A


#E6E6FA


#E0FFFF


#DEB887


#DDA0DD


#DCDCDC


#DC143C


#DB7093


#DAA520


#DA70D6


#D8BFD8


#D3D3D3


#D3D3D3


#D2B48C


#D2691E


#CD853F


#CD5C5C


#C71585


#C0C0C0


#BDB76B


#BC8F8F


#BA55D3


#B8860B


#B22222


#B0E0E6


#B0C4DE


#AFEEEE


#ADFF2F


#ADD8E6


#A9A9A9


#A9A9A9


#A52A2A


#A0522D


#9932CC


#98FB98


#9400D3


#9370DB


#90EE90


#8FBC8F


#8B4513


#8B008B


#8B0000


#8A2BE2


#87CEFA


#87CEEB


#808080


#808080


#808000


#800080


#800000


#7FFFD4


#7FFF00


#7CFC00


#7B68EE


#778899


#708090


#6B8E23


#6A5ACD


#696969


#66CDAA


#6495ED


#5F9EA0


#556B2F


#4B0082


#48D1CC


#483D8B


#4682B4


#4169E1


#40E0D0


#3CB371


#32CD32


#2F4F4F


#2F4F4F


#2E8B57


#228B22


#20B2AA


#1E90FF


#191970


#00FFFF


#00FFFF


#00FF7F


#00FF00


#00FA9A


#00CED1


#00BFFF


#008B8B


#008080


#008000


#006400


#0000FF


#0000CD


#00008B


#000080


#000000




关于挂断电话,采用了反射机制。接通电话4.4以上尚未找到好代码,本人目前在反编译其他相关软件,如果成果会更新 src目录下   com.android.internal.telephony  其中有ITelephony.aidl  ,这里面内容很简单 package com.android.internal.telephony;


interface ITelephony {
    boolean endCall();


    //void answerRingingCall();
} 本人会上传相关文件,只需要添加到工程的src目录下即可 http://download.csdn.net/detail/silenceshining/8657819