After the call, the dynamic wallpaper is actually displayed behind the Activity, while the Activity is transparent, so that you can see the following dynamic wallpaper. if the Activity is not transparent, nothing can be seen.
Two interfaces are used in the code.
IWallpaperService mService;
IWallpaperEngine mEngine;
We can see that the directory contains three aidl interfaces, which are
Copy codeThe Code is as follows: interface IWallpaperConnection {
Void attachEngine (IWallpaperEngine engine );
ParcelFileDescriptor setWallpaper (String name );
}
Oneway interface IWallpaperService {
Void attach (IWallpaperConnection connection,
IBinder windowToken, int windowType, boolean isPreview,
Int reqWidth, int reqHeight );
}
Oneway interface IWallpaperEngine {
Void setDesiredSize (int width, int height );
Void setVisibility (boolean visible );
Void dispatchPointer (in MotionEvent event );
Void dispatchWallpaperCommand (String action, int x, int y, int z, in Bundle extras );
Void destroy ();
}
Define wallpaper management and wallpaper information Variables
Copy codeThe Code is as follows: private WallpaperManager mWallpaperManager = null;
Private WallpaperInfo mWallpaperInfo = null;
Private WallpaperConnection mWallpaperConnection = null;
Private Intent mWallpaperIntent;
Initialize these variables
Copy codeThe Code is as follows: mWallpaperManager = WallpaperManager. getInstance (this );
MWallpaperInfo = mWallpaperManager. getWallpaperInfo (); // If null is returned, the current wallpaper is not a dynamic one.
MWallpaperIntent = new Intent (WallpaperService. SERVICE_INTERFACE );
MWallpaperIntent. setClassName (mWallpaperInfo. getPackageName (), mWallpaperInfo. getServiceName ());
Bind dynamic wallpaper Service
Copy codeThe Code is as follows: bindService (mIntent, this, Context. BIND_AUTO_CREATE );
IWallpaperService mService; // here is an adil Interface
Test attach in connection listening
Copy codeThe Code is as follows: public void onServiceConnected (ComponentName name, IBinder service ){
MService = IWallpaperService. Stub. asInterface (service );
Try {
MService. attach (this, view. getWindowToken (),
// WindowManager. LayoutParams. TYPE_APPLICATION_MEDIA_OVERLAY,
WindowManager. LayoutParams. TYPE_APPLICATION_MEDIA,
True, root. getWidth (), root. getHeight ());
} Catch (RemoteException e ){
Log. w ("", "Failed attaching wallpaper; clearing", e );
}
}
The bindService always fails. Later, the permission problem was discovered. Only the apk with system permissions can use the WallpaperService. SERVICE_INTERFACE service. Therefore, the problem becomes how to obtain system permissions.