我希望在屏幕上创建图标,只需听取触摸(不强制创建无用的通知) 经过3天的网上搜索并在IRC频道中询问并实施以下示例:
Android app that runs on top of ALL other apps?
Creating a system overlay window (always on top)
我发现有一种hacky方式可以做到这一点,但没有人知道它(智能任务栏开发人员除外) 我安装智能任务栏与大多数源代码示例,发现这个示例杀死没有通知栏,但智能任务栏永远不会被杀死,(我发现你甚至无法用常规任务杀手杀死它,如Android任务管理器),只有一种方法杀死此图标,转到设置>应用和GT;选择智能任务栏>强制停止。
我发现的另一件事是它永远不会去后台和android任务管理器(在实时进程列表中)它被标记为可见而你无法杀死它 有人可以帮助我如何创建一个始终可见并且永远不会在屏幕上被杀死的图标
答案 0 :(得分:1)
在您服务的setupNotification()
方法中使用onCreate
,并在服务的clearNotification()
方法中使用onDestroy
。
private int SERVICE_NOTIFICATION = 1; // this should be unique for your app
private void setupNotification()
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
Notification serviceNotification = new NotificationCompat.Builder(this)
.setContentTitle("Your Service")
.setContentText("Your Service is running in the background")
.setSmallIcon(R.drawable.ic_launcher)
.setPriority(NotificationCompat.PRIORITY_MIN)
.setOngoing(true)
.build();
startForeground(SERVICE_NOTIFICATION, serviceNotification);
}
else{
Notification serviceNotification = new Notification();
serviceNotification.flags = Notification.FLAG_ONGOING_EVENT;
startForeground(SERVICE_NOTIFICATION, serviceNotification);
}
}
private void clearNotification()
{
stopForeground(true);
}
答案 1 :(得分:0)
这绝对可行。你应该保持一个始终运行的服务。 并且在创建该服务时,
@Override
public void onCreate() {
super.onCreate();
customView= new MYCustomView(MyService.this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.RIGHT | Gravity.TOP;
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(customView, params);
}
这会将您的视图放在所有视觉之上。