我正在使用 EasyTouchService.class 向 WindowManger 添加一些视图。从 Launcher图标
重新启动应用后每当我从启动器重新启动ViewManager时,WindowManager都会再次添加View。
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
每当我从启动器图标再次启动应用程序时,它将重新启动服务并将视图再次添加到windowManager。
public class HomeActivity extends AppCompatActivity {
private Context mContext;
private BannerAdapter bannerAdapter;
private List<String> imageurls;
private ViewPager mViewPager;
public static int OVERLAY_PERMISSION_REQ_CODE = 1234;
public EasyTouchService mService;
protected boolean mBound;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
if(!isMyServiceRunning(EasyTouchService.class))
bindService(new Intent(this, EasyTouchService.class),
mConnection, Context.BIND_AUTO_CREATE);
final SwitchCompat hideShowSC = findViewById(R.id.sc_hide_show);
hideShowSC.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mService != null)
mService.toggleShowHide();
}
});
SwitchCompat startStopSC = findViewById(R.id.sc_start_stop);
startStopSC.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mService != null && buttonView.isChecked()){
mService.stopService();
unbindService(mConnection);
mService = null;
hideShowSC.setVisibility(View.GONE);
}
else{
bindService(new Intent(HomeActivity.this, EasyTouchService.class), mConnection, Context.BIND_AUTO_CREATE);
hideShowSC.setVisibility(View.VISIBLE);
}
}
});
mContext = HomeActivity.this;
imageurls = new ArrayList<>();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
} else {
if(!isMyServiceRunning(EasyTouchService.class))
startService(new Intent(this, EasyTouchService.class));
}
} else {
if(!isMyServiceRunning(EasyTouchService.class))
startService(new Intent(this, EasyTouchService.class));
}
imageurls.add("https://lh3.googleusercontent.com/9muBGwdXfXDCP_j-Ie-Q8v2koJlWOfWptWXcVDLEMLG4sPSZ12yMA3MwXou9qdRTDyMw28vxq1VRz6OWbrV-qhouPkIHvu8bx74cPhMEM95I6kHmIMqB2URc5QnnjgSkghkmImbaU97XYswGf08TA4kb3WNQH70Z-NsFPRbs69ANqfEvGXn4AthR_B9w_NU3Fnm5cBKf-4o-EoLUnNXLn1J7gJ6LcGnbyKE42UyfJ5c5ttbsyMdiPogk7SCl3ilE9ORoSKFzT9xbQTtErZsXKDbRu9xlb9E7b0pDBUa_PyEKRnrN29nFhzNo6fSbJqAbqmgsyfcLi5e0gm4sx863tYc48WamuqWsSn84S2jmdeFb1TwLKUfKrLF39Rgguo63vTKAtursmrm9ibz5gBBE4oNcptq9GFhtDnQKJVCjoFX5hkDtM13rO_uClYVlukb8TcXIRjsp7hPnxg449HGi5u6ztCWO2iTviS6QpHhug8BBZpHV82gkK1WxBaKMzfyjm4xz8lV7zVZCPi6Y9-182msBha1ejY0ah85-JEuPwWuUBw7lubVx_YpN16qZ_LUYaPQP4GR_KKeM7weTPmFA_OwnmIEVaWY=w640-h360-no");
imageurls.add("https://lh3.googleusercontent.com/NPbSvn901rfFpTysxx8tyhoqxxtwwcphdb6nkGMo0S_8-7-S10My2vgbQGMqE8qWsn9eSqioL1SQeLaSUaQhjrwVahKvh7CQLsexDxNkgJrFX5yPeeEitX75KUSmFuYvGWf1YMhf_Hr6KMfRAFl1PVw7nRxVqQ80y4GDBKUzaFAheVx-HBiUeBuK_CDfYdjT2peWJy_j3LevxDG0UmimCCURzeZGdMCnZqqkJbCR1vse8Rrf2eWUqXgwcpBppqxz-sD8xiOeyz8idS6x0kihLCwDZZKqtC9ENJGr-caQu9TMgs5-x8Vac1bKDHviuTQHwTKtymrMv2-pkC0acOyZIY0tt5SUdMAa_a2bEn14ezPbzWUBIvPqACA_JwulM9E8BAP4m6IOnYn1hdsZqJWo_k8qYJpGuoDll63HlJV-4wtZo1acXlRca5U-pFnPaA0YEPZWWtK_hM720VLF7nRj1y-AKH_GQbcwM14icMqoIUWk6Gvb1RK64QPYWw8r733SLrlRY7E0EG3633GqgX9Z3r3bz1gCaZc_l0SrJGYQsxT1BxdIxGOT-2p7b6A7Hw_51AFbZN0X1DN4cWigUiMtORdx6YLfdr4=w640-h360-no");//working...
imageurls.add("https://lh3.googleusercontent.com/b9PmGyj9lKfa3sKd-e4dHKy5FBT5EIrl31iAu7qmeGUgdp2ZF6XKde9JrFJ-2BSRXZ-Y8s10KMI8fOSEOyUS1ZLux41Itt6-jJFfkSC14-y9qNg4cFcwiDa7NjHWmT8L7gKU0rSPDdUQ-L0PDLiHBVVy7npZm_OUpZuJqnFVqE_alATCUSLl9106z-D72sf0MeZncqzs8j0Ct-tg7NJ-LVXtX0DYS_dt4A7HRAUDlEKW85OQPACG_yKKzO4JD47ri-dv5E9o0JPV8FofIDvUYsbCqCi-INiYyhPv88RTyh0G9OwGQSmL_1SSu9xclHd0gaPls7O_AJKOEFSJOKNX2Qipa3R_YIXbirQqfscNKeCJkpnuyex2YxDrC8FnKPlEsOIh9X_YWaE5To5TIPvzXbrikZJQ8aly5DVKra-s42gibYdQPc4WO_zGMloc6S2hcd9fu_JdUVjgE6EYPcf7Xc6fanE4aikhHBgG1JeITHeqjlwDirBo7qwSfbsDwPOG01ZR0I-kbKvl1nrSDhF8cTSEjKy2vN8SVWlDWPfd8JTCvcOUn2-izM7TjcBddexXNHAG6ADAkD3uQGogHjYkarVmIG4r4aY=s250-k-rw-no");
bannerAdapter = new BannerAdapter(mContext, imageurls);
mViewPager = findViewById(R.id.view_pager);
mViewPager.setAdapter(bannerAdapter);
AppCompatButton settingBtn = findViewById(R.id.btn_settings);
settingBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(mContext, SettingActivity.class));
}
});
}
@TargetApi(23)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
if (Settings.canDrawOverlays(this)) {
// SYSTEM_ALERT_WINDOW permission not granted...
startService(new Intent(this, EasyTouchService.class));
} else {
Toast.makeText(this, "Please allow this to draw over other apps to use.", Toast.LENGTH_LONG).show();
}
}
}
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
// We've bound to LocalService, cast the IBinder and get
// LocalService instance
EasyTouchService.LocalBinder binder = (EasyTouchService.LocalBinder) service;
mService = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
}
或者有一种方法可以在服务启动时从windowManager中删除所有视图。