试试下面的代码
public class ChatHeadService extends Service {
private WindowManager windowManager;
private ImageView chatHead;
private static final String TAG = "ChatheadService";
private Timer timer;
private static final int delay = 500; // delay for .5 sec before first start
private static final int period = 500; // repeat check every .5 sec.
Intent mIntent;
WindowManager.LayoutParams params;
ActivityManager activityManager;
@Override public IBinder onBind(Intent intent) {
Log.i("called", "onBind");
return null;
}
@Override public void onCreate() {
super.onCreate();
Log.i("called", "onCreate");
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
chatHead = new ImageView(this);
chatHead.setImageResource(R.drawable.app_icon);
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 = 100;
activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
//handleCommand(intent);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("called", "onStartCommand");
mIntent=intent;
handleCommand(intent);
return START_NOT_STICKY;
}
// handles a Start command
private void handleCommand(Intent intent) {
Log.d(TAG, "service is starting");
if (timer == null) {
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
checkActivityForeground();
}
}, delay, period);
}
}
protected void checkActivityForeground() {
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
final List<ActivityManager.RunningAppProcessInfo> processInfos = am
.getRunningAppProcesses();
ActivityManager.RunningAppProcessInfo processInfo = processInfos
.get(0);
// for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
// getting process at 0th index means our application is on top on all apps or currently open
appPackageName = (Arrays.asList(processInfo.pkgList).get(0));
}
// }
}
else {
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = null;
componentInfo = taskInfo.get(0).topActivity;
appPackageName = componentInfo.getPackageName();
}
Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
if(appPackageName.contains("com.whatsapp")){
if(chatHead.isShown()==false){
windowManager.addView(chatHead, params);
}
}else{
if ((chatHead != null) && chatHead.isShown()){
windowManager.removeView(chatHead);
}
}
}
}, 500);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
if ((chatHead != null) && chatHead.isShown()){
windowManager.removeView(chatHead);
}
}
}, 500);
timer.cancel();
}
}
在清单中添加
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<service android:name=".ChatHeadService"></service>
在您的活动课程中,将服务启动为
startService(new Intent(MainActivity.this, ChatHeadService.class));