下面列出了怎么用android.app.WallpaperInfo的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public WallpaperInfo getWallpaperInfo(int userId) {
userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Binder.getCallingUid(), userId, false, true, "getWallpaperInfo", null);
synchronized (mLock) {
WallpaperData wallpaper = mWallpaperMap.get(userId);
if (wallpaper != null && wallpaper.connection != null) {
return wallpaper.connection.mInfo;
}
return null;
}
}
@Override
public void onApplyButtonClicked(@NonNull final WallpaperCard wallpaperCard) {
final WallpaperInfo info = WallpaperManager.getInstance(this).getWallpaperInfo();
if (info == null || !Objects.equals(info.getPackageName(), getPackageName())) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.choose_wallpaper_title);
builder.setMessage(R.string.choose_wallpaper);
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Only after user click OK, we change currentWallpaperCard.
LWApplication.setCurrentWallpaperCard(getApplicationContext(), wallpaperCard);
cardAdapter.notifyDataSetChanged();
LWApplication.setPreviewWallpaperCard(wallpaperCard);
Intent intent = new Intent(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER);
startActivity(intent);
}
});
addDialog = builder.create();
addDialog.show();
} else {
LWApplication.setCurrentWallpaperCard(this, wallpaperCard);
cardAdapter.notifyDataSetChanged();
// Display a notice for user.
Snackbar.make(
coordinatorLayout,
String.format(
getResources().getString(R.string.applied_wallpaper),
wallpaperCard.getName()
),
Snackbar.LENGTH_LONG
).show();
}
}
public void setTransparent() {
if (backgroundImage == null) {
Drawable backgroundDrawable;
WallpaperInfo wallpaperInfo = wallpaperManager.getWallpaperInfo();
if (wallpaperInfo != null)
backgroundDrawable = wallpaperInfo.loadThumbnail(getContext().getPackageManager());
else {
try {
backgroundDrawable = wallpaperManager.getDrawable();
} catch (SecurityException e) {
setColor(getDefaultColor());
return;
}
}
backgroundImage = ImageUtils.cropBitmapToBar(getContext(), me.jfenn.androidutils.ImageUtils.drawableToBitmap(backgroundDrawable));
}
if (backgroundImage != null) {
int color = ColorUtils.getAverageColor(backgroundImage);
setColor(color);
if (isTransparentHome)
needsBackgroundImageDraw = true;
else backgroundImage = null;
} else setColor(backgroundColor.getDefault());
postInvalidate();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == IMAGE_PICK && resultCode == RESULT_OK) {
if (data != null && data.getData() != null) {
Uri uri = data.getData();
addTemporaryWallpaperTile(uri, false);
}
} else if (requestCode == PICK_WALLPAPER_THIRD_PARTY_ACTIVITY) {
setResult(RESULT_OK);
finish();
} else if (requestCode == PICK_LIVE_WALLPAPER) {
WallpaperManager wm = WallpaperManager.getInstance(this);
final WallpaperInfo oldLiveWallpaper = mLiveWallpaperInfoOnPickerLaunch;
final WallpaperInfo clickedWallpaper = mLastClickedLiveWallpaperInfo;
WallpaperInfo newLiveWallpaper = wm.getWallpaperInfo();
// Try to figure out if a live wallpaper was set;
if (newLiveWallpaper != null &&
(oldLiveWallpaper == null
|| !oldLiveWallpaper.getComponent()
.equals(newLiveWallpaper.getComponent())
|| clickedWallpaper.getComponent()
.equals(oldLiveWallpaper.getComponent()))) {
// Return if a live wallpaper was set
setResult(RESULT_OK);
finish();
}
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == IMAGE_PICK && resultCode == RESULT_OK) {
if (data != null && data.getData() != null) {
Uri uri = data.getData();
addTemporaryWallpaperTile(uri, false);
}
} else if (requestCode == PICK_WALLPAPER_THIRD_PARTY_ACTIVITY) {
setResult(RESULT_OK);
finish();
} else if (requestCode == PICK_LIVE_WALLPAPER) {
WallpaperManager wm = WallpaperManager.getInstance(this);
final WallpaperInfo oldLiveWallpaper = mLiveWallpaperInfoOnPickerLaunch;
final WallpaperInfo clickedWallpaper = mLastClickedLiveWallpaperInfo;
WallpaperInfo newLiveWallpaper = wm.getWallpaperInfo();
// Try to figure out if a live wallpaper was set;
if (newLiveWallpaper != null &&
(oldLiveWallpaper == null
|| !oldLiveWallpaper.getComponent()
.equals(newLiveWallpaper.getComponent())
|| clickedWallpaper.getComponent()
.equals(oldLiveWallpaper.getComponent()))) {
// Return if a live wallpaper was set
setResult(RESULT_OK);
finish();
}
}
}
public WallpaperConnection(WallpaperInfo info, WallpaperData wallpaper) {
mInfo = info;
mWallpaper = wallpaper;
}
public LiveWallpaperTile(Drawable thumbnail, WallpaperInfo info, Intent intent) {
mThumbnail = thumbnail;
mInfo = info;
}
public void onLiveWallpaperPickerLaunch(WallpaperInfo info) {
mLastClickedLiveWallpaperInfo = info;
mLiveWallpaperInfoOnPickerLaunch = WallpaperManager.getInstance(this).getWallpaperInfo();
}
public LiveWallpaperTile(Drawable thumbnail, WallpaperInfo info, Intent intent) {
mThumbnail = thumbnail;
mInfo = info;
}
static boolean isCurrent(Context context) {
WallpaperManager wm = WallpaperManager.getInstance(context);
WallpaperInfo wi = wm.getWallpaperInfo();
return wi != null && new ComponentName(context, EarthWallpaperService.class)
.equals(wi.getComponent());
}
public void onLiveWallpaperPickerLaunch(WallpaperInfo info) {
mLastClickedLiveWallpaperInfo = info;
mLiveWallpaperInfoOnPickerLaunch = WallpaperManager.getInstance(this).getWallpaperInfo();
}
public LiveWallpaperTile(Drawable thumbnail, WallpaperInfo info, Intent intent) {
mThumbnail = thumbnail;
mInfo = info;
}
/**
* 判断是否是使用我们的壁纸
*
* @param paramContext
* @return
*/
public static boolean wallpaperIsUsed(Context paramContext) {
WallpaperInfo localWallpaperInfo = WallpaperManager.getInstance(paramContext).getWallpaperInfo();
return ((localWallpaperInfo != null) && (localWallpaperInfo.getPackageName().equals(paramContext.getPackageName())) &&
(localWallpaperInfo.getServiceName().equals(LiveWallpaperService.class.getCanonicalName())));
}