基本信息
源码名称:android 动态切换壁纸实例 利用service机制实现 附完整源码 带动态截图
源码大小:0.59M
文件格式:.zip
开发语言:Java
更新时间:2013-04-12
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

通过点击 startService实现 android手机动态壁纸功能


main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="@string/hello" />
	<Button android:id="@ id/startService" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="startService"
		android:onClick="myStartService" />
	<Button android:id="@ id/stopService" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="stopService"
		android:onClick="myStopService" />
	<Button android:id="@ id/bindService" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="bindService"
		android:onClick="myBindService" />
	<Button android:id="@ id/unBindService" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="unBindService"
		android:onClick="myUnBindService" />

</LinearLayout>


MainActivity.java

 

package com.service;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;

public class MainActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}

	public void myStartService(View view) {
		Intent intent = new Intent(this, MyService.class);
		this.startService(intent);
	}

	public void myStopService(View view) {
		Intent intent = new Intent(this, MyService.class);
		this.stopService(intent);
	}

	ServiceConnection connection = new ServiceConnection() {

		public void onServiceConnected(ComponentName name, IBinder service) {
			System.out.println("Connected");
			System.out.println(name.getClassName());
			System.out.println(name.getPackageName());
			MyServiceOther.MyBinder binder = (MyServiceOther.MyBinder) service;
			MyServiceOther myS = binder.getService();
			System.out.println(myS.i);

		}

		public void onServiceDisconnected(ComponentName name) {
			System.out.println("Disconnected");
		}

	};

	public void myBindService(View view) {
		Intent intent = new Intent(this, MyServiceOther.class);
		this.bindService(intent, connection, Context.BIND_AUTO_CREATE);
	}

	public void myUnBindService(View view) {
		this.unbindService(connection);
	}
}

MyService.java

 

package com.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class MyService extends Service {

	boolean flag = true;
	MyThread myThread;
	int[] ins = new int[] { R.drawable.item1, R.drawable.item2,
			R.drawable.item3 };
	int i = 0;

	class MyThread extends Thread {
		public void run() {
			while (flag) {
				i  ;
				try {
					Thread.sleep(3000);
					MyService.this.setWallpaper(MyService.this.getResources()
							.openRawResource(ins[i % 3]));
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		}
	}

	public IBinder onBind(Intent intent) {
		return null;
	}

	public void onCreate() {
		super.onCreate();
		System.out.println("onCreate");
		myThread = new MyThread();
		myThread.start();
	}

	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		System.out.println("onStart");
	}

	public void onDestroy() {
		super.onDestroy();
		System.out.println("onDestroy");
		flag = false;
	}

}

MyServiceOther.java

package com.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class MyServiceOther extends Service {

	int i = 5;

	class MyBinder extends Binder {
		MyServiceOther getService() {
			return MyServiceOther.this;
		}
	}

	public IBinder onBind(Intent intent) {
		System.out.println("onBind");
		return new MyBinder();
	}

	public boolean onUnbind(Intent intent) {
		System.out.println("onUnbind");
		return super.onUnbind(intent);
	}

	public void onCreate() {
		super.onCreate();
		System.out.println("onCreate");
	}

	public void onDestroy() {
		super.onDestroy();
		System.out.println("onDestroy");
	}

}