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. SynchronizationContext.Post方法 代替

    http://www.codeproject.com/KB/threads/SynchronizationContext.aspx看吧,不好,就将就的看下我的吧,呵呵!(没有直接翻译,不过大概的思路相 ...

  2. (转)Django常用命令

    转自GoodSpeed,http://www.cnblogs.com/cacique/archive/2012/09/30/2709145.html . . . . .

  3. js 禁止鼠标和键盘行为

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. spring+jersey+c3p0构建restful webservice(数据源采用c3p0)

    项目下载地址:  http://files.cnblogs.com/files/walk-the-Line/payment.zip

  5. sql数据库的链接方式

    今天看见了一个数据库的链接方法,给转载了,记得我刚刚学DAO的时候老是要记载这些东西,所以就上博客园上面看了看,就转过来了... MySQL: String Driver="com.mysq ...

  6. [51nod] 1305 Pairwise Sum and Divide 数学

    有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整:   fun(A)     sum = 0     for i = 1 to A.length         for j = ...

  7. ACM-ICPC 2018 南京赛区网络预赛 Sum

    A square-free integer is an integer which is indivisible by any square number except 11. For example ...

  8. abstract 与interface区别

    1.abstract用于修饰类,interface用于修饰接口 2.抽象类中可以有抽象和非抽象方法,接口中只能定义抽象方法,不能有实现 3.抽象类必须被继承,interface被实现 4.抽象类有构造 ...

  9. [BZOJ2453]维护队列|分块

    Description 你小时候玩过弹珠吗? 小朋友A有一些弹珠,A喜欢把它们排成队列,从左到右编号为1到N.为了整个队列鲜艳美观,小朋友想知道某一段连续弹珠中,不同颜色的弹珠有多少.当然,A有时候会 ...

  10. 【转】mybatis循环map的一些技巧

    原文地址:http://blog.csdn.net/linminqin/article/details/39154133 循环key: <foreach collection="con ...