在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. android:View的setTag和getTag使用
  2. react-native 启动页(react-native link react-native-splash-scr
  3. Android之音频录音
  4. android Preference之android:dependency(列表依赖关系)
  5. android:configChanges="orientation|keyboardHidden"的使用
  6. Android全屏--两种activity的实现方式
  7. 清理android桌面
  8. android闹钟
  9. Android(安卓)Theme的使用

随机推荐

  1. Android程序退出彻底关闭进程的方法
  2. Android(安卓)Handler 泄漏
  3. 手机rom的那些坑
  4. Android使用外部字体
  5. android 手机屏幕密度等级和屏幕逻辑尺寸
  6. TranslateAnimation动画
  7. Android中振动器(Vibrator)的使用
  8. Android(安卓)SDK Manager:failed to ins
  9. Android如何实现引导页
  10. Android源码分析之Framework的MediaPlaye