gpt4 book ai didi

java - Android 未接来电广播接收器在屏幕上弹出

转载 作者:太空宇宙 更新时间:2023-11-04 13:42:19 25 4
gpt4 key购买 nike

在我的应用程序中,当用户未接来电时,我需要在屏幕上显示一个弹出窗口,因为我编写了一个接收器,它工作正常,但当我尝试显示弹出窗口时,它显示一个异常

06-30 15:24:11.320: E/AndroidRuntime(21759): java.lang.ClassCastException: android.app.ReceiverRestrictedContext cannot be cast to android.app.Activity
06-30 15:53:20.883: E/AndroidRuntime(22873): at com.ieltsdialer.receiver.MissedCallReceiver.initMissedCallPopup(MissedCallReceiver.java:96)
06-30 15:53:20.883: E/AndroidRuntime(22873): at com.ieltsdialer.receiver.MissedCallReceiver$CustomPhoneStateListener.onCallStateChanged(MissedCallReceiver.java:83)

如何从接收器中调用弹出窗口?或者还有其他选择来显示弹出窗口吗?

MissedCallReceiver.java

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.ieltsdialer.R;
import com.ieltsdialer.common.SlidingPanel;

public class MissedCallReceiver extends BroadcastReceiver{


private static final String TAG = "BroadcastReceiver";
Context mContext;
String incoming_nr;
private int prev_state;
private Animation animShow, animHide;

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub

TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); //TelephonyManager object
CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); //Register our listener with TelephonyManager
Bundle bundle = intent.getExtras();
String phoneNr= bundle.getString("incoming_number");
Log.v(TAG, "phoneNr: "+phoneNr);
mContext=context;
}

/* Custom PhoneStateListener */
public class CustomPhoneStateListener extends PhoneStateListener{

private static final String TAG = "CustomPhoneStateListener";

@Override
public void onCallStateChanged(int state, String incomingNumber){

if(incomingNumber!=null&&incomingNumber.length()>0) incoming_nr=incomingNumber;
switch(state){
case TelephonyManager.CALL_STATE_RINGING:

Log.d(TAG, "CALL_STATE_RINGING");
prev_state=state;

break;

case TelephonyManager.CALL_STATE_OFFHOOK:

Log.d(TAG, "CALL_STATE_OFFHOOK");
prev_state=state;

break;

case TelephonyManager.CALL_STATE_IDLE:

Log.d(TAG, "CALL_STATE_IDLE==>"+incoming_nr);
if((prev_state==TelephonyManager.CALL_STATE_OFFHOOK)){

prev_state=state;
//Answered Call which is ended
Toast.makeText(mContext, "answered call end", 5).show();

}

if((prev_state==TelephonyManager.CALL_STATE_RINGING)){

prev_state=state;
//Rejected or Missed call
Toast.makeText(mContext, " missed call end"+incomingNumber, 5).show();
initMissedCallPopup();

}

break;
}
}
}

public void initMissedCallPopup() {
// TODO Auto-generated method stub
final SlidingPanel popup = (SlidingPanel) ((Activity) mContext).findViewById(R.id.popup_window);


animShow = AnimationUtils.loadAnimation( mContext, R.anim.popup_show);
animHide = AnimationUtils.loadAnimation( mContext, R.anim.popup_hide);

final ImageButton hideButton = (ImageButton) ((Activity) mContext).findViewById(R.id.hide_popup_button);

popup.setVisibility(View.VISIBLE);
popup.startAnimation( animShow );
hideButton.setEnabled(true);

hideButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
popup.startAnimation( animHide );
hideButton.setEnabled(false);
popup.setVisibility(View.GONE);
}});

final TextView appName = (TextView) ((Activity) mContext).findViewById(R.id.app_name);
final TextView description = (TextView) ((Activity) mContext).findViewById(R.id.missed_call_description);

appName.setText("App Title");
description.setText("Missed call Description");

}

}

SlidingPanel.java

public class SlidingPanel extends LinearLayout 
{
private Paint innerPaint, borderPaint ;

public SlidingPanel(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public SlidingPanel(Context context) {
super(context);
init();
}

private void init() {
innerPaint = new Paint();
innerPaint.setARGB(100, 25, 25, 75); //gray
innerPaint.setAntiAlias(true);

borderPaint = new Paint();
borderPaint.setARGB(255, 255, 255, 255);
borderPaint.setAntiAlias(true);
borderPaint.setStyle(Style.STROKE);
borderPaint.setStrokeWidth(5);
}

public void setInnerPaint(Paint innerPaint) {
this.innerPaint = innerPaint;
}

public void setBorderPaint(Paint borderPaint) {
this.borderPaint = borderPaint;
}

@Override
protected void dispatchDraw(Canvas canvas) {

RectF drawRect = new RectF();
drawRect.set(0,0, getMeasuredWidth(), getMeasuredHeight());

canvas.drawRoundRect(drawRect, 5, 5, innerPaint);
canvas.drawRoundRect(drawRect, 5, 5, borderPaint);

super.dispatchDraw(canvas);
}
}

最佳答案

您的 initMissedCallPopup 方法不正确。

第一:

final SlidingPanel popup = (SlidingPanel) ((Activity) mContext).findViewById(R.id.popup_window);

给你一个异常,因为 mContext 不是 Activity 而是 android.app.ReceiverRestrictedContext (异常消息非常清楚地表明了这一点)。您正在 onReceive 中分配此变量。您必须找到某种方法来获取对包含 SlidingPanel 的 Activity 的引用。

关于java - Android 未接来电广播接收器在屏幕上弹出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31135588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com