AS-demo09

1,mainifast:

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

2,

<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"

    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">



    <TextView
    android:id="@+id/title"
    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/logo"/>
</LinearLayout>
================================


package com.example.demo09longclickchangebg;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.IOException;

public class MainActivity extends Activity {

    private TextView textView =null;
    private ImageView imageView = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.textView=(TextView)super.findViewById(R.id.title);
        this.imageView=(ImageView)super.findViewById(R.id.img);

        this.imageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                try {
                    MainActivity.this.clearWallpaper();
                    MainActivity.this.setWallpaper(MainActivity.this.imageView.getResources().openRawResource(R.drawable.logo));
                    MainActivity.this.textView.setText("手机桌面背景已修改BG changed!");
                } catch (IOException e) {
                    MainActivity.this.textView.setText("手机桌面背景修改Error");
                }
                return false;
            }
        });

    }


    @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;
    }
    
}

 

posted @ 2016-03-06 21:05  Alamps  阅读(242)  评论(0编辑  收藏  举报