当前位置: 代码迷 >> Android >> android 远程上载桌面背景图案——URLConnection 与 setWallpaper() 搭配
  详细解决方案

android 远程上载桌面背景图案——URLConnection 与 setWallpaper() 搭配

热度:363   发布时间:2016-05-01 15:39:26.0
android 远程下载桌面背景图案——URLConnection 与 setWallpaper() 搭配

主类代码:

package com.EX08;import java.net.URL;import java.net.URLConnection;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;import android.widget.TextView;public class EX08_10 extends Activity {	private Button button1, button2;	private EditText edittext;	private ImageView imageview;	private TextView textview;	private Bitmap bm;	@Override	protected void onCreate(Bundle savedInstanceState) {		// TODO Auto-generated method stub		super.onCreate(savedInstanceState);		setContentView(R.layout.ex08_10);		/** 通过 findViewById()构造器创建 Button 和TextView、EditText、ImageView 对象 */		button1 = (Button) findViewById(R.id.myButton1);		button2 = (Button) findViewById(R.id.myButton2);		textview = (TextView) findViewById(R.id.myText);		edittext = (EditText) findViewById(R.id.myEdit);		imageview = (ImageView) findViewById(R.id.myImage);		button1.setOnClickListener(new Button.OnClickListener() {			@Override			public void onClick(View v) {				// TODO Auto-generated method stub				String path = edittext.getText().toString();				if (path.equals("")) {					showDialog("图片网址不能为空!");				} else {					setImage(path, 1);				}			}		});		button2.setOnClickListener(new Button.OnClickListener() {			@Override			public void onClick(View v) {				try {					String path = edittext.getText().toString();					if (path.equals("")) {						showDialog("网址不可以为空!");					} else {						/**传入 type=2 设置成桌面*/						setImage(path, 2);					}				} catch (Exception e) {					showDialog("读取错误,网址可能不是图片或网址错误!");					bm = null;					imageview.setImageBitmap(bm);					button2.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());				imageview.setImageBitmap(bm);				button2.setEnabled(true);			} else if (type == 2) {				/**设置成桌面*/				EX08_10.this.setWallpaper(BitmapFactory.decodeStream(conn.getInputStream()));				bm = null;				imageview.setImageBitmap(bm);				button2.setEnabled(false);				showDialog("桌面背景设置成功!");			}		} catch (Exception e) {			showDialog("读取错误,网址可能不是图片,或网址错误!");			bm = null;			imageview.setImageBitmap(bm);			button2.setEnabled(false);			e.printStackTrace();		}	}	/**弹出Dialog对话框*/	private void showDialog(String mess) {		new AlertDialog.Builder(EX08_10.this).setTitle("Message").setMessage(				mess).setNegativeButton("确定",				new DialogInterface.OnClickListener() {					public void onClick(DialogInterface dialog, int which) {					}				}).show();	}}

?

?

主配置文件代码(AndroidManifest.xml):

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"	package="com.EX08" android:versionCode="1" android:versionName="1.0">	<application android:icon="@drawable/icon"		android:label="@string/app_name">		<activity android:name=".EX08"			android:label="@string/app_name">			<intent-filter>				<action android:name="android.intent.action.MAIN" />				<category					android:name="android.intent.category.LAUNCHER" />			</intent-filter>		</activity>		<activity android:name="EX08_01" />		<activity android:name="EX08_02" />		<activity android:name="EX08_03" />		<activity android:name="EX08_04" />		<activity android:name="EX08_05" />		<activity android:name="EX08_09" />		<activity android:name="EX08_10" />	</application>	<!-- 添加网络连接权限 -->	<uses-permission android:name="android.permission.INTERNET">	</uses-permission>	<uses-permission		android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />	<!-- 设置桌面背景的权限 -->	<uses-permission		android:name="android.permission.SET_WALLPAPER" />	<uses-sdk android:minSdkVersion="7" /></manifest>

?

string.xml配置如下:

<?xml version="1.0" encoding="utf-8"?><resources>	<string name="hello">Hello World, EX08_02</string>	<string name="app_name">EX08</string>	<string name="load">正在载入</string>	<string name="str_msg">下載中....請稍候...</string>	<string name="str_list_url1">Google</string>	<string name="str_list_url2">Dubblogs</string>	<string name="str_list_url3">Yahoo</string>	<string name="str_list_url4">MSN</string>	<string name="str_url1">http://www.google.com</string>	<string name="str_url2">http://www.dubblogs.cc</string>	<string name="str_url3">http://www.yahoo.com</string>	<string name="str_url4">http://www.msn.com</string>	<string name="url">		http://www.dubblogs.cc:8751/Android/Test/Media/mp3/test.mp3	</string>	<string name="str_title">輸入圖片網址:</string>	<string name="str_button1">預覽圖片</string>	<string name="str_button2">設為桌面</string>	<string name="imageurl">	<!--(gif格式) http://www.baidu.com/img/baidu_logo_jr_1003_315.gif -->	<!--(png格式) http://share.ooopic.com/icon/img_icon/505/Dark_Part_1_031.png -->	http://www.54caizi.org/plugin/windsphoto/photofile/20086/200862023250132.jpg	</string></resources>

?

同时支持:JPG、PNG、GIF、BMP格式的图片

?

?效果如下:

1 楼 董瑞龙 2010-09-25  
学习了 谢谢
  相关解决方案