2015-06-30 108 views
1

在我的應用程序需要在屏幕上顯示一個彈出窗口,當用戶得到的未接來電,爲我寫了一個接收器其工作正常,但當我嘗試顯示彈出它顯示了一個異常來自未接來電廣播接收器屏幕的Android彈出

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); 
    } 
} 
+0

你能否也請提一下這個行,引發異常? –

+0

是的,我更新了我的問題@Prera​​k Sola它在animShow = AnimationUtils.loadAnimation(mContext,R.anim.popup_show); –

+0

你也可以顯示你的進口報表嗎?我想,你可能從錯誤的包中導入了類。 –

回答

0

你initMissedCallPopup方法是不正確的。

第一:

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

給你一個例外,因爲mContext不活動,但android.app.ReceiverRestrictedContext(異常信息非常清楚地表明這一點)。您正在onReceive中分配此變量。 您必須找到一些方法來獲取對包含SlidingPanel的活動的引用。

+0

準確@Okas請你幫我找到方法來獲取參考包含SlidingPanel的活動。 –

+0

這將超出此問題範圍。試着找到一個解決方案,如果你不能,然後制定一個新的問題。 – Okas