我正在创建一个聊天头服务,该服务从来电开始。我想在来电时打开聊天头。只有打开我的屏幕,我才能这样做。即使我的应用程序被锁定,我怎样才能实现这一目标?请帮我。
我的聊天主管服务课程:
package com.tarun.notifyme2;
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;
public class ChatHeadService extends Service {
private WindowManager windowManager;
private ImageView chatHead;
WindowManager.LayoutParams params;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int res = super.onStartCommand(intent, flags, startId);
return res;
}
@Override
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
chatHead = new ImageView(this);
chatHead.setImageResource(R.drawable.bell2);
chatHead.setClickable(true);
params= new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 200;
chatHead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(ChatHeadService.this,"You clicked the imageview",Toast.LENGTH_LONG).show();
Log.i("tag","You clicked the imageview");
startActivity(new Intent(ChatHeadService.this, SendNoti.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
stopSelf();
}
});
//this code is for dragging the chat head
chatHead.setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
int flag=0;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = params.x;
initialY = params.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
if(flag==3){
flag=1;
return true;
}else{
flag=1;
return false;
}
case MotionEvent.ACTION_UP:
if(flag==3){
flag=2;
return true;
}else{
flag=2;
return false;
}
case MotionEvent.ACTION_MOVE:
flag=3;
params.x = initialX
+ (int) (event.getRawX() - initialTouchX);
params.y = initialY
+ (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(chatHead, params);
return true;
default:
Toast.makeText(getApplicationContext(),"You ckiced the imageview",Toast.LENGTH_LONG).show();
Log.i("tag","You clicked the imageview");
/*
Intent i = new Intent(view.getContext(),SendNoti.class);
startActivity(i);
stopSelf();*/
return true;
}
}
});
/*
Snackbar.make(chatHead, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();*/
windowManager.addView(chatHead, params);
}
@Override
public void onDestroy() {
super.onDestroy();
if (chatHead != null)
windowManager.removeView(chatHead);
stopSelf();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
我的类检测传入和传出类
package com.tarun.notifyme2;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
/**
* Created by Tarun Talreja on 13-08-2016.
*/
public class OutgoingCallReceiver extends BroadcastReceiver {
static String outgoingNo;
static String incomingNo;
private Context mContext;
private Intent mIntent;
public OutgoingCallReceiver() {
super();
}
@Override
public void onReceive(final Context context, Intent intent) {
String state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);
// TODO Auto-generated method stub
mContext = context;
mIntent = intent;
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int events = PhoneStateListener.LISTEN_CALL_STATE;
tm.listen(phoneStateListener, events);
/*
if (state.equals(TelephonyManager.CALL_STATE_RINGING)) {
// This code will execute when the phone has an incoming call
// get the phone number
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, "Call from:" +incomingNumber, Toast.LENGTH_LONG).show();
incomingNo = incomingNumber;
context.startService(new Intent(context, ChatHeadService.class));
}else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE)
|| intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_OFFHOOK)) {
// This code will execute when the call is disconnected
Toast.makeText(context, "Detected call hangup event", Toast.LENGTH_LONG).show();
context.stopService(new Intent(context, ChatHeadService.class));
}
*/
}
private final PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
String callState = "UNKNOWN";
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
callState = "IDLE";
mContext.stopService(new Intent(mContext, ChatHeadService.class));
break;
case TelephonyManager.CALL_STATE_RINGING:
// -- check international call or not.
if (incomingNumber.startsWith("00")) {
Toast.makeText(mContext,"International Call- " + incomingNumber,Toast.LENGTH_LONG).show();
callState = "International - Ringing (" + incomingNumber+ ")";
incomingNo = incomingNumber;
} else {
Toast.makeText(mContext, "Local Call - " + incomingNumber, Toast.LENGTH_LONG).show();
callState = "Local - Ringing (" + incomingNumber + ")";
incomingNo = incomingNumber;
}
mContext.startService(new Intent(mContext, ChatHeadService.class));
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
String dialingNumber = mIntent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
/*
if (dialingNumber.startsWith("00")) {
Toast.makeText(mContext,"International - " + dialingNumber,Toast.LENGTH_LONG).show();
callState = "International - Dialing (" + dialingNumber+ ")";
outgoingNo=dialingNumber;
} else {
*/
Toast.makeText(mContext, "Local Call - " + dialingNumber,Toast.LENGTH_LONG).show();
callState = "Local - Dialing (" + dialingNumber + ")";
outgoingNo=dialingNumber;
//}
break;
}
Log.i(">>>Broadcast", "onCallStateChanged " + callState);
super.onCallStateChanged(state, incomingNumber);
}
};
}