来电广播接收器在 Android 4.4.2 中不工作
Incomming call broadcast receivernot working in Android 4.4.2
我有一个来电广播接收器,它正在 android 7.1 nut 上工作,而不是 android 4.2.
广播接收器
public class CallReceiver extends BroadcastReceiver {
private MainActivity activity;
public static final String PREFS_PHONE = "Phone";
public static final String PREFS_EMAIL = "Email";
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
SharedPreferences settings = context.getSharedPreferences(context.getResources().getString(R.string.app_name),0);
if(settings.contains(PREFS_EMAIL)) {
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("MY_DEBUG_TAG", state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
DatabaseHandler db = new DatabaseHandler(context);
User user;
String phone = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
try {
user = db.getUserByPhone(phone);
Log.d("USER", user.getEmail());
context.startActivity(new Intent(context, FloatingActivity.class)
.putExtra("PHONE", TelephonyManager.EXTRA_INCOMING_NUMBER));
String phoneNumber = extras
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
settings.edit().putString(PREFS_PHONE, phone).apply();
Log.d("MY_DEBUG_TAG", phoneNumber);
} catch (IndexOutOfBoundsException e) {
Log.i(phone, "Not Found!");
}
}
}
}
}
}
浮动activity
public class FloatingActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chathead_layout);
startService(new Intent(this, FloatingViewService.class)
.putExtra("PHONE", this.getIntent().getStringExtra("PHONE")));
finish();
}
}
浮动服务
public class FloatingViewService extends Service {
private View chatHead;
private View closeChatHead;
private WindowManager windowManager;
private DatabaseHandler db = new DatabaseHandler(this);
private String phone;
private SharedPreferences settings;
private boolean mayRemove;
private Rect chatHeadRect;
private Rect closeHeadRect;
private boolean isHover;
public static final String PREFS_PHONE = "Phone";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
//Inflate the floating view layout we created
settings = getSharedPreferences(getResources().getString(R.string.app_name),0);
phone = settings.getString(PREFS_PHONE, null);
User user = db.getUserByPhone(phone);
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
chatHead = LayoutInflater.from(this).inflate(R.layout.chathead_layout, null);
closeChatHead = LayoutInflater.from(this).inflate(R.layout.close_chathead_layout, null);
closeChatHead.setVisibility(View.INVISIBLE);
//Add the view to the window.
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
//Specify the view position
params.gravity = Gravity.TOP | Gravity.LEFT; //Initially view will be added to top-left corner
params.x = 0;
params.y = 500;
final WindowManager.LayoutParams params2 = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params2.gravity = Gravity.BOTTOM | Gravity.CENTER;
//Add the view to the window
windowManager.addView(chatHead, params);
windowManager.addView(closeChatHead, params2);
((CircleImageView) chatHead.findViewById(R.id.profile_image)).setImageDrawable(new ColorDrawable(Color.parseColor("#"+user.getColour())));
((TextView) chatHead.findViewById(R.id.pts)).setText(user.getPts()+"");
switch (user.getDirection()) {
case 1:
chatHead.findViewById(R.id.up).setVisibility(View.VISIBLE);
break;
case 0:
chatHead.findViewById(R.id.equal).setVisibility(View.VISIBLE);
break;
case -1:
chatHead.findViewById(R.id.down).setVisibility(View.VISIBLE);
break;
}
final CircleImageView remover = ((CircleImageView) chatHead.findViewById(R.id.remover));
//Set the close button
/*ImageView closeButton = (ImageView) chatHead.findViewById(R.id.close_btn);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//close the service and remove the from from the window
stopSelf();
}
});*/
//Drag and move floating view using user's touch action.
chatHead.findViewById(R.id.chat_head).setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//remember the initial position.
initialX = params.x;
initialY = params.y;
//get the touch location
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
closeChatHead.setVisibility(View.VISIBLE);
return true;
case MotionEvent.ACTION_MOVE:
//show remover
//Calculate the X and Y coordinates of the view.
params.x = initialX + (int) (event.getRawX() - initialTouchX);
params.y = initialY + (int) (event.getRawY() - initialTouchY);
//Update the layout with new X & Y coordinate
windowManager.updateViewLayout(chatHead, params);
int[] a = new int[2];
int[] b = new int[2];
chatHead.getLocationOnScreen(a);
View remover = closeChatHead.findViewById(R.id.remover);
remover.getLocationOnScreen(b);
chatHeadRect = new Rect(a[0], a[1], a[0] + chatHead.getWidth(), a[1] + chatHead.getHeight());
closeHeadRect = new Rect(b[0], b[1], b[0] + remover.getWidth(), b[1] + remover.getHeight());
if (checkToRemove(Math.round((chatHeadRect.left)) + Math.round((chatHeadRect.width()) / 2),
Math.round((chatHeadRect.top)) + Math.round((chatHeadRect.height()) / 2),
closeHeadRect.left, closeHeadRect.top, closeHeadRect.left + closeHeadRect.width())) {
//Log.d("motion move","intersect = yes");
mayRemove = true;
if (!isHover) {
isHover = !isHover;
windowManager.removeView(closeChatHead);
closeChatHead = LayoutInflater.from(FloatingViewService.this).inflate(R.layout.close_chathead_layout, null);
windowManager.addView(closeChatHead, params2);
}
} else {
//Log.d("motion move","intersect = no");
mayRemove = false;
if (isHover) {
//Log.d("MOTION MOVE2","PASSOU");
isHover = !isHover;
windowManager.removeView(closeChatHead);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
closeChatHead = inflater.inflate(R.layout.close_chathead_layout, null);
windowManager.addView(closeChatHead, params2);
}
}
closeChatHead.setVisibility(View.VISIBLE);
return true;
case MotionEvent.ACTION_UP:
int Xdiff = (int) (event.getRawX() - initialTouchX);
int Ydiff = (int) (event.getRawY() - initialTouchY);
//The check for Xdiff <10 && YDiff< 10 because sometime elements moves a little while clicking.
//So that is click event.
if (Xdiff < 10 && Ydiff < 10) {
Intent intent = new Intent(FloatingViewService.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
//close the service and remove view from the view hierarchy
stopSelf();
}
if (mayRemove) {
stopSelf();
}
closeChatHead.setVisibility(View.INVISIBLE);
return true;
}
return false;
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
if (chatHead != null) windowManager.removeView(chatHead);
}
private boolean checkToRemove(int x,int y,int left,int top,int left_width){
return (y>(top-50) && x>(left-50) && x<(left_width-50));
}
}
为什么不工作:
在 7.1 中,一切正常,我接到了电话,聊天头也出现了。
在 4.4 [LG] 中,我接到电话但没有任何反应。
在 4.4 [BQ] 应用程序崩溃。
有什么帮助吗?
找到问题了。
我从 activity 外部调用 startActivity 的地方,例如:
context.startActivity(new Intent(context, FloatingActivity.class)
.putExtra("PHONE", TelephonyManager.EXTRA_INCOMING_NUMBER));
在android 4.4需要设置flag Intent.FLAG_ACTIVITY_NEW_TASK
o 在所有情况下我都需要这样称呼它:
context.startActivity(new Intent(context, FloatingActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra("PHONE", TelephonyManager.EXTRA_INCOMING_NUMBER));
我有一个来电广播接收器,它正在 android 7.1 nut 上工作,而不是 android 4.2.
广播接收器
public class CallReceiver extends BroadcastReceiver {
private MainActivity activity;
public static final String PREFS_PHONE = "Phone";
public static final String PREFS_EMAIL = "Email";
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
SharedPreferences settings = context.getSharedPreferences(context.getResources().getString(R.string.app_name),0);
if(settings.contains(PREFS_EMAIL)) {
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("MY_DEBUG_TAG", state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
DatabaseHandler db = new DatabaseHandler(context);
User user;
String phone = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
try {
user = db.getUserByPhone(phone);
Log.d("USER", user.getEmail());
context.startActivity(new Intent(context, FloatingActivity.class)
.putExtra("PHONE", TelephonyManager.EXTRA_INCOMING_NUMBER));
String phoneNumber = extras
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
settings.edit().putString(PREFS_PHONE, phone).apply();
Log.d("MY_DEBUG_TAG", phoneNumber);
} catch (IndexOutOfBoundsException e) {
Log.i(phone, "Not Found!");
}
}
}
}
}
}
浮动activity
public class FloatingActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chathead_layout);
startService(new Intent(this, FloatingViewService.class)
.putExtra("PHONE", this.getIntent().getStringExtra("PHONE")));
finish();
}
}
浮动服务
public class FloatingViewService extends Service {
private View chatHead;
private View closeChatHead;
private WindowManager windowManager;
private DatabaseHandler db = new DatabaseHandler(this);
private String phone;
private SharedPreferences settings;
private boolean mayRemove;
private Rect chatHeadRect;
private Rect closeHeadRect;
private boolean isHover;
public static final String PREFS_PHONE = "Phone";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
//Inflate the floating view layout we created
settings = getSharedPreferences(getResources().getString(R.string.app_name),0);
phone = settings.getString(PREFS_PHONE, null);
User user = db.getUserByPhone(phone);
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
chatHead = LayoutInflater.from(this).inflate(R.layout.chathead_layout, null);
closeChatHead = LayoutInflater.from(this).inflate(R.layout.close_chathead_layout, null);
closeChatHead.setVisibility(View.INVISIBLE);
//Add the view to the window.
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
//Specify the view position
params.gravity = Gravity.TOP | Gravity.LEFT; //Initially view will be added to top-left corner
params.x = 0;
params.y = 500;
final WindowManager.LayoutParams params2 = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params2.gravity = Gravity.BOTTOM | Gravity.CENTER;
//Add the view to the window
windowManager.addView(chatHead, params);
windowManager.addView(closeChatHead, params2);
((CircleImageView) chatHead.findViewById(R.id.profile_image)).setImageDrawable(new ColorDrawable(Color.parseColor("#"+user.getColour())));
((TextView) chatHead.findViewById(R.id.pts)).setText(user.getPts()+"");
switch (user.getDirection()) {
case 1:
chatHead.findViewById(R.id.up).setVisibility(View.VISIBLE);
break;
case 0:
chatHead.findViewById(R.id.equal).setVisibility(View.VISIBLE);
break;
case -1:
chatHead.findViewById(R.id.down).setVisibility(View.VISIBLE);
break;
}
final CircleImageView remover = ((CircleImageView) chatHead.findViewById(R.id.remover));
//Set the close button
/*ImageView closeButton = (ImageView) chatHead.findViewById(R.id.close_btn);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//close the service and remove the from from the window
stopSelf();
}
});*/
//Drag and move floating view using user's touch action.
chatHead.findViewById(R.id.chat_head).setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//remember the initial position.
initialX = params.x;
initialY = params.y;
//get the touch location
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
closeChatHead.setVisibility(View.VISIBLE);
return true;
case MotionEvent.ACTION_MOVE:
//show remover
//Calculate the X and Y coordinates of the view.
params.x = initialX + (int) (event.getRawX() - initialTouchX);
params.y = initialY + (int) (event.getRawY() - initialTouchY);
//Update the layout with new X & Y coordinate
windowManager.updateViewLayout(chatHead, params);
int[] a = new int[2];
int[] b = new int[2];
chatHead.getLocationOnScreen(a);
View remover = closeChatHead.findViewById(R.id.remover);
remover.getLocationOnScreen(b);
chatHeadRect = new Rect(a[0], a[1], a[0] + chatHead.getWidth(), a[1] + chatHead.getHeight());
closeHeadRect = new Rect(b[0], b[1], b[0] + remover.getWidth(), b[1] + remover.getHeight());
if (checkToRemove(Math.round((chatHeadRect.left)) + Math.round((chatHeadRect.width()) / 2),
Math.round((chatHeadRect.top)) + Math.round((chatHeadRect.height()) / 2),
closeHeadRect.left, closeHeadRect.top, closeHeadRect.left + closeHeadRect.width())) {
//Log.d("motion move","intersect = yes");
mayRemove = true;
if (!isHover) {
isHover = !isHover;
windowManager.removeView(closeChatHead);
closeChatHead = LayoutInflater.from(FloatingViewService.this).inflate(R.layout.close_chathead_layout, null);
windowManager.addView(closeChatHead, params2);
}
} else {
//Log.d("motion move","intersect = no");
mayRemove = false;
if (isHover) {
//Log.d("MOTION MOVE2","PASSOU");
isHover = !isHover;
windowManager.removeView(closeChatHead);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
closeChatHead = inflater.inflate(R.layout.close_chathead_layout, null);
windowManager.addView(closeChatHead, params2);
}
}
closeChatHead.setVisibility(View.VISIBLE);
return true;
case MotionEvent.ACTION_UP:
int Xdiff = (int) (event.getRawX() - initialTouchX);
int Ydiff = (int) (event.getRawY() - initialTouchY);
//The check for Xdiff <10 && YDiff< 10 because sometime elements moves a little while clicking.
//So that is click event.
if (Xdiff < 10 && Ydiff < 10) {
Intent intent = new Intent(FloatingViewService.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
//close the service and remove view from the view hierarchy
stopSelf();
}
if (mayRemove) {
stopSelf();
}
closeChatHead.setVisibility(View.INVISIBLE);
return true;
}
return false;
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
if (chatHead != null) windowManager.removeView(chatHead);
}
private boolean checkToRemove(int x,int y,int left,int top,int left_width){
return (y>(top-50) && x>(left-50) && x<(left_width-50));
}
}
为什么不工作:
在 7.1 中,一切正常,我接到了电话,聊天头也出现了。
在 4.4 [LG] 中,我接到电话但没有任何反应。
在 4.4 [BQ] 应用程序崩溃。
有什么帮助吗?
找到问题了。
我从 activity 外部调用 startActivity 的地方,例如:
context.startActivity(new Intent(context, FloatingActivity.class)
.putExtra("PHONE", TelephonyManager.EXTRA_INCOMING_NUMBER));
在android 4.4需要设置flag Intent.FLAG_ACTIVITY_NEW_TASK
o 在所有情况下我都需要这样称呼它:
context.startActivity(new Intent(context, FloatingActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra("PHONE", TelephonyManager.EXTRA_INCOMING_NUMBER));