android(记录)设为壁纸,使用toast提示信息

此应用展示如何使用Android创建一个简单的应用来获取并显示NASA的每日图片,并将其设置为壁纸。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

package com.duoduo.nasadailyimage;

import java.io.IOException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.app.WallpaperManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class NasaDailyImageActivity extends Activity {

	private ProgressDialog dialog;
	private Handler handler;
	private Bitmap bitmap;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		handler = new Handler();
	}

	public void onRefresh(View view) {
		dialog = ProgressDialog.show(this, "Loading", "Loading");
		Thread thread = new Thread() {
			public void run() {
				handler.post(new Runnable() {
					public void run() {
						TextView imageDescriptionView = (TextView) findViewById(R.id.imageDescription);
						imageDescriptionView
								.setText(getString(R.string.image_description2));
						dialog.dismiss();
					}
				});

			}
		};
		thread.start();
	}

	public void setWallPaper(View view) {
		Thread thread = new Thread() {
			public void run() {
				handler.post(new Runnable() {
					public void run() {
						WallpaperManager wallpaperManager = WallpaperManager
								.getInstance(NasaDailyImageActivity.this);
						// Resources res = getResources();
						// bitmap = BitmapFactory.decodeResource(res,
						// R.drawable.test);
						try {
							wallpaperManager.setResource(R.drawable.test);
							// wallpaperManager.setBitmap(bitmap);
							handler.post(new Runnable() {
								public void run() {
									Toast.makeText(NasaDailyImageActivity.this,
											"壁纸设置成功", Toast.LENGTH_SHORT)
											.show();
								}
							});
						} catch (IOException e) {
							handler.post(new Runnable() {
								public void run() {
									Toast.makeText(NasaDailyImageActivity.this,
											"壁纸设置失败", Toast.LENGTH_SHORT)
											.show();
								}
							});
							e.printStackTrace();
						}
					}
				});

			}
		};
		thread.start();

	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值