创建一个帽子头或弹出图标/按钮,它将出现在Android来电和拨出电话的前面。我通过服务(广播接收者)而不是通过活动来做到这一点。
最后,我正在运行应用程序并打电话,没有任何事情发生。 虽然我正在调用该服务但是没有启动,但是日志显示该方法已被调用。 我做错了什么?为什么它不起作用?
接收者:
public void onReceive(Context context, Intent intent)
{
Log.i("LOG", "CallReciever onRecieve()");
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
Log.v("LOG", "state is:"+state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
String num = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.v("LOG", "phone ringing, num:"+num);
Log.v("LOG", "adding floating button");
addAppButtonToCallScreen(context);//add out application to the main call screen
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE))//call ended or hanged up
{
//remove our button from screen
removeAppButtonFromCallScreen();
}
}
/**
* add our application to the main call screen
* @param context
*/
private void addAppButtonToCallScreen(Context context)
{
Log.i("LOG","addAppButtonToCallScreen()");
Intent intent = new Intent(context, FloatingButtonService.class);
context.startService(intent);
}
服务:
public class FloatingButtonService extends Service
{
private WindowManager _windowManager;
private ImageView _floatingButton;
private boolean _isMove;
@Override
public void onCreate()
{
Log.i("LOG", "FloatingButtonService.OnCreate()");
super.onCreate();
_floatingButton = new ImageView(this); //the logo bubble as imageView
_floatingButton.setImageResource(R.drawable.ic_call);
_windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
//set the layout params
final LayoutParams myParams = new WindowManager.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,
LayoutParams.TYPE_PHONE,
LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
myParams.gravity = Gravity.TOP | Gravity.LEFT;
myParams.x=0;
myParams.y=100;
// add a floatingButton icon in window
Log.v("LOG", "adding the view");
_windowManager.addView(_floatingButton, myParams);
try{
//for moving the picture on touch and slide
_floatingButton.setOnTouchListener(new View.OnTouchListener() {
WindowManager.LayoutParams paramsT = myParams;
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
private long touchStartTime = 0;
@Override
public boolean onTouch(View v, MotionEvent event)
{
Log.i("LOG", "floating button touch event");
//remove button bubble on long press
if(System.currentTimeMillis()-touchStartTime>ViewConfiguration.getLongPressTimeout() && initialTouchX== event.getX())
{
_windowManager.removeView(_floatingButton);
stopSelf();
return false;
}
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
touchStartTime = System.currentTimeMillis();
initialX = myParams.x;
initialY = myParams.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
break;
case MotionEvent.ACTION_UP:
if (_isMove==false)
{
startApp();//start our application
}
break;
case MotionEvent.ACTION_MOVE:
myParams.x = initialX + (int) (event.getRawX() - initialTouchX);
myParams.y = initialY + (int) (event.getRawY() - initialTouchY);
_windowManager.updateViewLayout(v, myParams);
_isMove=true;
break;
}
return false;
}
});
} catch (Exception e){
e.printStackTrace();
}
}
/**
* show toast */
private void startApp()
{
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "pressed button weeeeeeeeee", Toast.LENGTH_LONG).show();
}
日志:
01-08 18:03:56.745: I/LOG(10149): CallReciever onRecieve()
01-08 18:03:56.745: V/LOG(10149): state is:RINGING
01-08 18:03:56.745: V/LOG(10149): phone ringing, num:0549968089
01-08 18:03:56.745: V/LOG(10149): adding floating button
01-08 18:03:56.745: I/LOG(10149): addAppButtonToCallScreen()
01-08 18:04:04.645: I/LOG(10149): CallReciever onRecieve()
01-08 18:04:04.645: V/LOG(10149): state is:IDLE
01-08 18:11:45.765: I/LOG(10543): CallReciever onRecieve()
01-08 18:11:45.765: V/LOG(10543): state is:RINGING
01-08 18:11:45.765: V/LOG(10543): phone ringing, num:0549968089
01-08 18:11:45.765: V/LOG(10543): adding floating button
01-08 18:11:45.765: I/LOG(10543): addAppButtonToCallScreen()
01-08 18:11:57.950: I/LOG(10543): CallReciever onRecieve()
01-08 18:11:57.950: V/LOG(10543): state is:OFFHOOK
01-08 18:12:11.275: I/LOG(10543): CallReciever onRecieve()
01-08 18:12:11.275: V/LOG(10543): state is:IDLE