[英]Android popup on screen from missed call broadcast receiver
在我的應用程序中,當用戶未接來電時,我需要在屏幕上顯示一個彈出窗口,為此,我編寫了一個接收機,其工作正常,但是當我嘗試顯示彈出窗口時,它顯示了異常
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的活動的引用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.