如何检查我的Live-Wallpaper是否正在运行

时间:2014-02-21 16:07:29

标签: java android live-wallpaper

我有动态壁纸和活动。如果用户激活了我的壁纸,我怎样才能检查活动?如果没有,我如何将他发送到动态壁纸设置?

1 个答案:

答案 0 :(得分:1)

找到解决方案:

  1. 检查服务是否正在运行的方法

    private boolean isMyServiceRunning(String className) {
    ActivityManager manager = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (className.equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;}
    
  2. 启动意图ACTION_SET_WALLPAPER

        //Check if Wallpaper Service is active
    if(isMyServiceRunning(MyWallpaperService.class.getName())){
        Log.d(TAG, "active" );
    } else {
        Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER);
        startActivity(Intent.createChooser(intent, getActivity().getString(R.string.title_for_wallpaper_chooser)));
    }