Skip to content

Commit

Permalink
floatview
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexLiuSheng committed May 25, 2016
1 parent 6e1a73b commit 31c9d46
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 39 deletions.
94 changes: 57 additions & 37 deletions app/src/main/java/com/allenliu/floatview/CallShowReceiver.java
Expand Up @@ -5,7 +5,11 @@
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

/**
Expand All @@ -15,48 +19,64 @@ public class CallShowReceiver extends BroadcastReceiver {
private String TAG="CALLSHOWRECEIVER";
private boolean viewIsShow=false;
private FloatView floatView;
private PhoneStateListener phoneStateListener;
@Override
public void onReceive(Context context, Intent intent) {
// if(intent.getAction().equals("android.intent.action.PHONE_STATE")){
// if(intent.getAction().equals("com.allenliu.floatview.REGISTER_CALLSHOW"))
initPhoneStateListener(context,intent);
// }
}
private void initPhoneStateListener(final Context context, final Intent intent) {
PhoneStateListener phoneStateListener=new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state){
//挂机或没有电话时的状态
case TelephonyManager.CALL_STATE_IDLE:
if(floatView!=null){
floatView.removeFromWindow();
viewIsShow=false;
}
break;
//响铃中
case TelephonyManager.CALL_STATE_RINGING:
// Log.v(TAG,"SHOW");
if(!viewIsShow) {
viewIsShow=true;
floatView= new FloatView(context, 0, 0, R.layout.callshow_layout);
TextView textView = (TextView) floatView.findViewById(R.id.tv_phone);
textView.setText(incomingNumber + "来电了");
floatView.addToWindow();
}
break;
//接听电话了
case TelephonyManager.CALL_STATE_OFFHOOK:
if(floatView!=null){
floatView.removeFromWindow();
viewIsShow=false;
}
break;
if(phoneStateListener==null) {
phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
//挂机或没有电话时的状态
case TelephonyManager.CALL_STATE_IDLE:
if (floatView != null) {
floatView.removeFromWindow();
viewIsShow = false;
}
break;
//响铃中
case TelephonyManager.CALL_STATE_RINGING:
// Log.v(TAG,"SHOW");
if (!viewIsShow) {
viewIsShow = true;
if (floatView == null)
floatView = new FloatView(context, 0, 0, R.layout.callshow_layout);
TextView textView = (TextView) floatView.findViewById(R.id.tv_phone);
ImageView imageView = (ImageView) floatView.findViewById(R.id.iv);
ViewGroup.LayoutParams params = imageView.getLayoutParams();
params.width = getScreenWidth(context);
// params.height=400;
imageView.setLayoutParams(params);
textView.setText(incomingNumber + "来电了");

floatView.addToWindow();
}
break;
//接听电话了
case TelephonyManager.CALL_STATE_OFFHOOK:
if (floatView != null) {
floatView.removeFromWindow();
viewIsShow = false;
}
break;
}
}
}
};
//注册监听,
TelephonyManager manager= (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
manager.listen(phoneStateListener,PhoneStateListener.LISTEN_CALL_STATE);
};
//注册监听,
TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
manager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
private int getScreenWidth(Context context) {
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.widthPixels;
}
}
9 changes: 7 additions & 2 deletions app/src/main/res/layout/callshow_layout.xml
Expand Up @@ -2,12 +2,17 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#ffffff"
android:padding="@dimen/activity_horizontal_margin"
android:layout_height="match_parent">
<TextView
android:layout_width="500dp"
android:layout_width="match_parent"
android:layout_gravity="center"
android:textColor="#000000"
android:gravity="center"
android:layout_height="wrap_content"
android:id="@+id/tv_phone"/>
<ImageView
android:layout_width="match_parent"
android:background="@mipmap/demo"
android:id="@+id/iv"
android:layout_height="wrap_content" />
</LinearLayout>
Binary file added app/src/main/res/mipmap-xhdpi/demo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 31c9d46

Please sign in to comment.