1.MainActivity.java

package com.example.administrator.hello4;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView; public class MainActivity extends AppCompatActivity {
//声明
private TextView textView;
private ImageView imageView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //初始化:通过组件id获取组件实例
textView = (TextView) findViewById(R.id.tv);
imageView = (ImageView) findViewById(R.id.iv); imageView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
imageView.setBackgroundResource(R.drawable.a);
textView.setText("图片已经设置为手机桌面");
return false;
}
}); }
}

2.activity_mian.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.hello4.MainActivity"> <TextView
android:id="@+id/tv"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="长按图片将其设置为背景" />
<ImageView
android:id="@+id/iv"
android:src="@drawable/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

3

在长按事件中的onLongClick中返回false:表示事件没有完全处理完毕。这样就可以继续执行setOnclicklistener事件 弹出“显示信息。。。”

在长按事件中的onLongClick中返回ture:表示事件完全处理完毕。不执行setOnclicklistener事件 所以不弹出“显示信息。。。”

3.按照前面的例子只能在软件上面显示已经更改设置为壁纸,但是在手机壁纸还没有更改,因为没有管理者权限

(1)MianActivity.java此时增加了设置壁纸的方法setWallPaper.

 package com.example.administrator.hello4;

 import android.app.WallpaperManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView; import java.io.IOException; public class MainActivity extends AppCompatActivity {
//声明
private TextView textView;
private ImageView imageView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //初始化:通过组件id获取组件实例
textView = (TextView) findViewById(R.id.tv);
imageView = (ImageView) findViewById(R.id.iv); imageView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
imageView.setBackgroundResource(R.drawable.a);
textView.setText("图片已经设置为手机桌面");
return false;
}
}); setWallPaper(); }
public void setWallPaper(){
WallpaperManager wallpaperManager = WallpaperManager.getInstance(MainActivity.this);
try {
wallpaperManager.setResource(R.drawable.a);
} catch (IOException e) {
e.printStackTrace();
}
} }

(2)在AndroidManiifest.xml配置管理者权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.hello4"> <uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission> //配置的管理者权限
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>

这样就可以运行软件之后真正的将图片设置为手机壁纸

长按事件OnLongClickListener的更多相关文章

  1. 第24章、OnLongClickListener长按事件(从零开始学Android)

    在Android App应用中,OnLongClick事件表示长按2秒以上触发的事件,本章我们通过长按图像设置为墙纸来理解其具体用法. 知识点:OnLongClickListener OnLongCl ...

  2. ListView item 中TextView 如何获取长按事件

    昨天晚上小伙伴突然来信, ListView item中嵌套的TextView 无法获取长按事件 从前从来没有仔细留意过, coding后发现...果然没什么动静 而且没有合适的API让我调用获取Tex ...

  3. Android Button四种点击事件和长按事件

    项目XML代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andr ...

  4. RecyclerView的单击和长按事件(转)

    转自:http://www.jianshu.com/p/f2e0463e5aef 前言 上一篇文章揭开RecyclerView的神秘面纱(一):RecyclerView的基本使用中,主要讲述了Recy ...

  5. Anroid关于fragment控件设置长按事件无法弹出Popupwindows控件问题解决记录

    一.问题描述     记录一下最近在安卓的gragment控件中设置长按事件遇见的一个坑!!!     在正常的activity中整个活动中设置长按事件我通常实例化根部局,例如LinearLayout ...

  6. Android学习笔记长按事件的处理

    常见的长按事件 代码示例: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedIns ...

  7. Android系统中自定义按键的短按、双击、长按事件

    在项目中碰到这样的问题: 由于系统中的按键在底层做了重新定义或者新增了按键,此时需要在APP层对按键事件(keyevent)做分解处理,模拟Android系统做法,把keyevent分解成: 1.单击 ...

  8. 长按事件jquery mobile

    chat_enlarge.addEventListener('touchend', function(event) { if(fingers == 1){ event.preventDefault() ...

  9. Android RecyclerView单击、长按事件:基于OnItemTouchListener +GestureDetector标准实现(二),封装抽取成通用工具类

     Android RecyclerView单击.长按事件:基于OnItemTouchListener +GestureDetector标准实现(二),封装抽取成通用工具类 我写的附录文章2,介绍了 ...

随机推荐

  1. LVM to increase and reduce 10G size for /data

    =======================increase10G for/data=============================(system env /dev/MongoData00 ...

  2. The NPF driver isn't running

    转自:http://blog.csdn.net/zhangkaihang/article/details/7470239 今天安装Wireshark软件时出现了如下图所示的错误,就搜索了一下解决方法, ...

  3. 【洛谷 P1667】 数列 (贪心)

    题目链接 对于一个区间\([x,y]\),设这个区间的总和为\(S\) 那么我们在前缀和(设为\(sum[i]\))的意义上考虑到原操作其实就是\(sum[x−1]+=S\) , \(sum[x]+S ...

  4. 填坑webpack

    1.Concepts: webpack is a module bundler for modern JS applications. Since there are lots of complex ...

  5. passwd讲解

    root:$dffjioowwf/:16274:0:999999:7::: 1用户名:密码:最近修改密码的日期:密码不能更改的天数:密码过期时间:密码需要更改期限到拉前7发出警告:宽限天数:帐号过期时 ...

  6. Ubuntu Touch环境搭建

    最近搞了一下Nexus 5的MultiRom Manger,体验了一把Ubuntu Touch和Android L,总体感觉还不错,不过Android L的NFC驱动还有问题,Ubuntu Touch ...

  7. 面试===Linux试题及答案

    一. 单选题: 1.添加一条静态路由,使到网络196.199.3通过eth2接口出去,用: A. route add -net 196.199.3.0 B. route add -net 196.19 ...

  8. 如何生成pyc/pyo/pyd文件

    # 一.如何生成pyc/pyo文件 # 1.通过编写代码生成 import py_compile # 参数如下 ''' def compile(file, cfile=None, dfile=None ...

  9. [ Python - 15 ] win7安装paramiko问题总汇

    安装环境: win7 sp1 python3.5 安装paramiko 新装的win7 sp1 x64位系统,安装好python3.5和pycharm后,需要用到paramiko模块于是开始安装: & ...

  10. Storm中log4j日志打印不出来的解决办法

    使用storm命令启动JAVA进程的时候,发现log4j日志打印不出来,咋办呢? 解决办法如下(亲测): 删除strom/lib目录下的log4j-over-slf4j-1.6.6.jar strom ...