展会信息港展会大全

android 背景设置 android开发教程
来源:互联网   发布日期:2015-10-02 16:02:54   浏览:2118次  

导读:public static final String TAG = WallpaperActivity;/*** 背景图片名称*/private static final String FILE_NAME = MM-320x480.png;/*** @see android.app.Activity#onCreate(android.os.B......

public static final String TAG = "WallpaperActivity";

/**

* 背景图片名称

*/

private static final String FILE_NAME = "MM-320x480.png";

/**

* @see android.app.Activity#onCreate(android.os.Bundle)

*/

public void onCreate(Bundle cycle) {

super.onCreate(cycle);

super.setContentView(R.layout.wallpaper);

// 取得背景图片

Bitmap wallpaper = this.getWallpager();

// 设置桌面背景

this.putWallpaper(wallpaper);

}

/**

* 取得SDCard中的背景图片

*/

private Bitmap getWallpager() {

// SDCard的路径,也就是 /sdcard/

File root = Environment.getExternalStorageDirectory();

// MM图片

File wall = new File(root, FILE_NAME);

// MM图片路径

String path = wall.getAbsolutePath();

Log.d(TAG, "MM文件路径为:" + path);

return BitmapFactory.decodeFile(path);

}

/**

* 设置桌面背景

*/

private void putWallpaper(Bitmap bitmap) {

try {

WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);

wallpaperManager.setBitmap(bitmap);

} catch (IOException e) {

String msg = "设置桌面背景发生异常:" + e.getLocalizedMessage();

Log.e(TAG, "设置桌面背景发生异常!", e);

Toast.makeText(this, msg, Toast.LENGTH_LONG).show();

}

}

增加权限

在AndroidManifest.xml文件中增加设置桌面背景权限:

<!-- 设置桌面背景的权限 --> < uses-permission android:name ="android.permission.SET_WALLPAPER" />

赞助本站

人工智能实验室

相关热词: 背景设置

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港