按住2秒后才有所操作

在main.xml中的代码如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<TextView

android:id="@+id/info"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="长按图片,将图片为背景"/>

<ImageView

android:id="@+id/img"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:src="@drawable/mldn_bg"/>

</LinearLayout>

在MyLongClickDemo.java中的代码如下:

package com.tarena.longclick;

import java.io.IOException;

import android.app.Activity;

import android.content.res.Resources.NotFoundException;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnLongClickListener;

import android.widget.ImageView;

import android.widget.TextView;

public class MyLongClickDemo extends Activity {

private TextView info = null;

private ImageView img = null;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

super.setContentView(R.layout.main);

this.info = (TextView) super.findViewById(R.id.info);

this.img = (ImageView) super.findViewById(R.id.img);

this.img.setOnLongClickListener(new OnLongClickListenerImpl());

}

private class OnLongClickListenerImpl implements OnLongClickListener{

public boolean onLongClick(View v) {

try { //生成try catch,先选中要try catch的部分,再右键Surround With --> Try/catch Block

MyLongClickDemo.this.clearWallpaper(); //清除已有的桌面

MyLongClickDemo.this.setWallpaper(MyLongClickDemo.this.img

.getResources().openRawResource(R.drawable.mldn_bg)); // 设置桌面

MyLongClickDemo.this.info.setText("手机桌面背景已修改!");

} catch (Exception e) {

MyLongClickDemo.this.info.setText("手机桌面背景设置失败");

}

return false;

}

}

}

然后还要在AndroidManifest.xml中修改权限:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.tarena.longclick"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />

<application

android:icon="@drawable/ic_launcher"

android:label="@string/app_name" >

<activity

android:name=".MyLongClickDemo"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

<uses-permission android:name="android.permission.SET_WALLPAPER"/> //授权

</manifest>

更多相关文章

  1. android EditText设置不可写
  2. android Dialog大小修改
  3. android 创建桌面快捷方式 、插件
  4. Android(安卓)Activity的启动
  5. 从 Android(安卓)Sample ApiDemos 中学习 android.animation API
  6. android源码下载方式
  7. APP开发实战94-Vector静态图的使用
  8. 修改android系统和watchdog的延时
  9. android 开机直接运行app并当做手机桌面

随机推荐

  1. Android(安卓)3rd-party libraries
  2. android联系人过滤
  3. Build Variants Android变体构建配置
  4. android网络状态检查
  5. Cocos2d-x & NDK
  6. Android(安卓)- ScrollView 使用小计 里
  7. 4. android 滚动视图
  8. 关于android 布局中诡异的AttributeSet
  9. 【Android】定义HttpPost连接超时
  10. android 自定义组件随着手指自动画圆