长按事件
在Android中提供了长按事件的处理操作,所谓的长按事件就好象读者经常使用某一个手机,长按某一个组件2秒之后才会触发这一操作,而不像普通的单击事件那样,每次单击都会执行一次,长按事件使用View.OnLongClickListener接口进行事件的处理操作,此接口定义如下:
public static interface View.OnLongClickListener{
public boolean onLongClick(View v) ;
}
授权
由于这种设置手机桌面背景的操作属于手机的支持服务,所以首先必须得到相关的授权后才可以执行,如果要授权则需要修改AndroidManifest.xml文件,并增加以下的授权操作:
<uses-permission android:name="android.permission.SET_WALLPAPER" />
Manifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.richard.onlongclicklistener"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.richard.onlongclicklistener.MainActivity"
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>
</manifest>
布局文件:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<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="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
</RelativeLayout>
Activity文件:
package com.richard.onlongclicklistener;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
private ImageView img = null;
private TextView info = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.img = (ImageView) super.findViewById(R.id.img);
this.info = (TextView) super.findViewById(R.id.info);
this.img.setOnLongClickListener(new OnLongClickListenerImpl());
}
private class OnLongClickListenerImpl implements OnLongClickListener{
@Override
public boolean onLongClick(View arg0) {
try{
System.out.println("aaaa");
MainActivity.this.clearWallpaper(); //清楚已有的桌面
MainActivity.this.setWallpaper(MainActivity.this.getResources().openRawResource(R.drawable.ic_launcher));
MainActivity.this.info.setText("手机桌面背景已修改");
}catch(Exception e){
System.out.println(e);
MainActivity.this.info.setText("手机桌面背景设置失败");
}
return true;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
测试效果: