屏幕上的Android弹出窗口来自未接来电广播接收器

时间:2015-06-30 10:16:49

标签: java android popup calllog

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

p

如何从接收器中拨打弹出窗口?或者是否有其他替代show popup?

MissedCallReceiver.java

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)

SlidingPanel.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");

    }

}

1 个答案:

答案 0 :(得分:0)

您的initMissedCallPopup方法不正确。

首先:

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

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