1 开通两个权限
<!-- 必须设定SET_WALLPAPER权限与INTERNET权限 -->
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-permission android:name="android.permission.INTERNET"/>
2
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<EditText
android:id="@+id/myEdit"
android:layout_width="280px"
android:layout_height="wrap_content"
android:text="http://"
android:textSize="12sp"
android:layout_x="20px"
android:layout_y="42px"
>
</EditText>
<TextView
android:id="@+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_title"
android:textSize="16sp"
android:textColor="@drawable/black"
android:layout_x="20px"
android:layout_y="12px"
>
</TextView>
<Button
android:id="@+id/myButton1"
android:layout_width="80px"
android:layout_height="45px"
android:text="@string/str_button1"
android:layout_x="70px"
android:layout_y="102px"
>
</Button>
<Button
android:id="@+id/myButton2"
android:layout_width="80px"
android:layout_height="45px"
android:text="@string/str_button2"
android:layout_x="150px"
android:layout_y="102px"
>
</Button>
<ImageView
android:id="@+id/myImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20px"
android:layout_y="152px"
>
</ImageView>
</AbsoluteLayout>
3
package dfzy.EX094;
/* import相关class */
import java.net.URL;
import java.net.URLConnection;
import java.net.URLConnection;
import dfzy.EX094.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class EX094 extends Activity
{
/* 变量声明 */
private Button mButton1;
private Button mButton2;
private EditText mEditText;
private ImageView mImageView;
private Bitmap bm;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* 初始化对象 */
mButton1 =(Button) findViewById(R.id.myButton1);
mButton2 =(Button) findViewById(R.id.myButton2);
mEditText = (EditText) findViewById(R.id.myEdit);
mImageView = (ImageView) findViewById(R.id.myImage);
mButton2.setEnabled(false);
/* 预览图片的Button */
mButton1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
String path=mEditText.getText().toString();
if(path.equals(""))
{
showDialog("网址不可为空白!");
}
else
{
/* 传入type=1为预览图片 */
setImage(path,1);
}
}
});
/* 将图片设为桌面的Button */
mButton2.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
try
{
String path=mEditText.getText().toString();
if(path.equals(""))
{
showDialog("网址不可为空白!");
}
else
{
/* 传入type=2为设置桌面 */
setImage(path,2);
}
}
catch (Exception e)
{
showDialog("读取错误!!");
bm = null;
mImageView.setImageBitmap(bm);
mButton2.setEnabled(false);
e.printStackTrace();
}
}
});
}
/* 将图片抓下来预览或并设置为桌面的方法 */
private void setImage(String path,int type)
{
try
{
URL url = new URL(path);
URLConnection conn = url.openConnection();
conn.connect();
if(type==1)
{
/* 预览图片 */
bm = BitmapFactory.decodeStream(conn.getInputStream());
mImageView.setImageBitmap(bm);
mButton2.setEnabled(true);
}
else if(type==2)
{
/* 设置为桌面 */
EX094.this.setWallpaper(conn.getInputStream());
bm = null;
mImageView.setImageBitmap(bm);
mButton2.setEnabled(false);
showDialog("桌面背景设置完成!");
}
}
catch (Exception e)
{
showDialog("读取错误!网址可能不是图片或网址错误!");
bm = null;
mImageView.setImageBitmap(bm);
mButton2.setEnabled(false);
e.printStackTrace();
}
}
/* 跳出Dialog的方法 */
private void showDialog(String mess){
new AlertDialog.Builder(EX094.this).setTitle("Message")
.setMessage(mess)
.setNegativeButton("确定", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
}
})
.show();
}
}
4 效果是对的,不展示了