Android简单实现更换桌面背景的方法

http://blog.163.com/lixiaodaoaaa@126/blog/static/349948682012419112330446/

package mldn.lxd.SetWallPaperTest;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.Toast;

public class SetWallPaperActivity extends Activity {
   
   
   private ImageView imageView = null;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imageView = (ImageView)super.findViewById(R.id.myImg);
        imageView.setOnLongClickListener(new myImageOnLongClickListener());
    }
    
    private final class myImageOnLongClickListener implements OnLongClickListener
    {
      @Override
      public boolean onLongClick(View v)
      {
         try
         {
            SetWallPaperActivity.this.clearWallpaper();
         } catch (IOException e)
         {
            Toast.makeText(getApplicationContext(),"清空桌面背景失败,请稍后再试",Toast.LENGTH_LONG).show();
            e.printStackTrace();
         }
         InputStream  myImagInputStream = imageView.getResources().openRawResource(R.drawable.mydesttop);
         try
         {
            SetWallPaperActivity.this.setWallpaper(myImagInputStream);
            Toast.makeText(getApplicationContext(),"恭喜你设置成功!!",Toast.LENGTH_LONG).show();
         } catch (IOException e)
         {
            // TODO Auto-generated catch block
            Toast.makeText(getApplicationContext(),"设置桌面图标失败,请稍后再试",Toast.LENGTH_LONG).show();
            e.printStackTrace();
         }
         return false;
      }
    }
    
}
桌面成功设置了。嘎嘎看如下

你可能感兴趣的:(android,开发学习)