我们正在尝试在我们的应用程序中实现类似true-caller的弹出窗口。 它成功地工作,当我们得到任何呼叫它显示弹出窗口。 但是当呼叫断开时,则弹出窗口不会被删除。 我们尝试了所有,但没有工作。 请帮忙。
这是我们的广播课: -
PhonecallReceiver.java
public class PhonecallReceiver extends BroadcastReceiver
{
private static int lastState = TelephonyManager.CALL_STATE_IDLE;
private static Date callStartTime;
private static boolean isIncoming;
private static String savedNumber;
View view;
@Override
public void onReceive(Context context, Intent intent)
{
try
{
if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL"))
{
savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
}
else
{
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
int state = 0;
if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE))
{
state = TelephonyManager.CALL_STATE_IDLE;
}
else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
{
state = TelephonyManager.CALL_STATE_OFFHOOK;
}
else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
state = TelephonyManager.CALL_STATE_RINGING;
}
onCallStateChanged(context, state, number);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
//Derived classes should override these to respond to specific events of interest
protected void onIncomingCallStarted(Context context, String number, Date start){
// onCallStateChanged(context, state, number);
}
protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end){
}
public void onCallStateChanged(Context context, int state, String number)
{
if(lastState == state)
{
//No change, debounce extras
return;
}
switch (state)
{
case TelephonyManager.CALL_STATE_RINGING:
isIncoming = true;
callStartTime = new Date();
savedNumber = number;
onIncomingCallStarted(context, number, callStartTime);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if (isIncoming)
{
onIncomingCallEnded(context,savedNumber,callStartTime,new Date());
}
case TelephonyManager.CALL_STATE_IDLE:
if(isIncoming)
{
onIncomingCallEnded(context, savedNumber, callStartTime, new Date());
}
}
lastState = state;
}
}
CallReciever.java
public class CallReceiver extends PhonecallReceiver
{
Context context;
View view ;
LinearLayout ly;
WindowManager wm;
@Override
protected void onIncomingCallStarted( Context ctx, final String number, Date start)
{
Toast.makeText(ctx,"Kushal Incoming Call"+ number,Toast.LENGTH_LONG).show();
context = ctx;
ly = new LinearLayout(context);
LayoutInflater mInflater = LayoutInflater.from(context);
view = mInflater.inflate(R.layout.dialog,ly ,true);
wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT |
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSPARENT);
params.height =500;
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.format = PixelFormat.TRANSLUCENT;
params.gravity = Gravity.TOP;
// view = ly.inflate(R.layout.dialog_layout,null);
TextView textview = (TextView) view.findViewById(R.id.tv_client);
textview.setText("Someone is calling: " + number);
textview.setBackgroundColor(Color.red(4));
// ly.setBackgroundColor(Color.RED);
//ly.setOrientation(LinearLayout.VERTICAL);
wm.addView(view, params);
view.findViewById(R.id.dialog_ok).setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// do stuff
wm.removeView(view);
}
});
}
public void releaseLandScape(){
// ((WindowManager) context.getApplicationContext().getSystemService(Service.WINDOW_SERVICE)).removeView(view);
wm.removeView(view); //error on this line
// This doesn't work as well
//wm.removeViewImmediate(orientationChanger);
}
@Override
protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end)
{
try {
Toast.makeText(ctx, "Bye Bye" + number, Toast.LENGTH_LONG).show();
WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
releaseLandScape();
}catch (Exception e){
e.printStackTrace();
}
}
}
错误: -
java.lang.NullPointerException:尝试调用接口方法' void android.view.WindowManager.removeView(android.view.View)'在空对象引用上
如果你仔细查看,那么你可以看到" CallReciever.java",在来电时,我启动视图,但是当调用结束时,视图为空。这就是问题所在。