Android-设置手机桌面

在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;
 @Override
 protected 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{
  @Override
  public 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


 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值