在Android系统中经常需要设置手机桌面功能,此文就通过程序来讲解如何在android系统中设置手机桌面。

Android前端xml文件代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    tools:context=".MainActivity" >    <TextView android:id="@+id/img"        android:text="设置手机桌面"        android:layout_width="fill_parent"        android:layout_height="wrap_content"/>        <ImageView         android:layout_height="fill_parent"        android:layout_width="fill_parent"        android:id="@+id/imgId"        android:src="@drawable/zht_bg"/></LinearLayout>

Android后台Activity程序代码:

package com.example.fiveandroid;public class MainActivity extends Activity {private ImageView imageView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.activity_main); //默认显示视图;//获取图片组件;this.imageView = (ImageView) super.findViewById(R.id.imgId);//设置长按按钮监听事件;this.imageView.setOnLongClickListener(new MyLongClickListener());}/** * 监听长按按钮事件; * @author Administrator * */private class MyLongClickListener implements OnLongClickListener{@Overridepublic boolean onLongClick(View v) {InputStream inputStream = null;try {//清空桌面背景;MainActivity.this.clearWallpaper(); //根据背景图片获取输入流;inputStream = MainActivity.this.imageView.getResources().openRawResource(R.drawable.zht_bg);//设置桌面背景;MainActivity.this.setWallpaper(inputStream);Toast.makeText(MainActivity.this, "设置桌面背景成功.",  Toast.LENGTH_SHORT).show();} catch (NotFoundException e) {Toast.makeText(MainActivity.this, "设置桌面背景失败.", Toast.LENGTH_SHORT).show();e.printStackTrace();} catch (IOException e) {Toast.makeText(MainActivity.this, "设置桌面背景失败.", Toast.LENGTH_SHORT).show();e.printStackTrace();}finally{//关闭输入流;if(inputStream != null){try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}}return false;}}}

效果如下:

1


2



更多相关文章

  1. LinearLayout按下(pressed)或获取焦点(focused)时背景设置不同颜
  2. Android(安卓)TextView在XML中的属性
  3. 为Activity屏幕的标题添加图标
  4. Android中禁止多点触控的设置
  5. 2010.11.16———android Camera 拍照的两个问题
  6. android Notification 的使用
  7. Android去掉状态栏和标题栏的两种方式
  8. 设置环境变量
  9. 设置环境变量

随机推荐

  1. 对比Excel学习SQL(5):分组和子查询
  2. 使用 React 实现页面过渡动画仅需四个步
  3. LeetCode 题解 | 1312. 让字符串成为回文
  4. 剑指Offer 图解 | 寻找旋转排序数组中的
  5. 我就想问一句,约么?
  6. 从 O(N) 优化到 O(logN),你的第一想法是什
  7. 神器 VS Code,超详细Python配置使用指南
  8. 世界上有哪些代码量很少,但很牛逼很经典的
  9. LeetCode 题解 | 406 号问题:根据身高重建
  10. 【最新】iPhone X 交互设计官方指南