1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > java 制作动态手机壁纸_android 动态切换壁纸实例 利用service机制实现 附完整源

java 制作动态手机壁纸_android 动态切换壁纸实例 利用service机制实现 附完整源

时间:2021-11-29 12:41:28

相关推荐

java 制作动态手机壁纸_android 动态切换壁纸实例 利用service机制实现 附完整源

【实例简介】通过点击 startService实现 android手机动态壁纸功能

【实例截图】

【核心代码】

main.xml

android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:layout_height="wrap_content" android:text="@string/hello" />

android:layout_height="wrap_content" android:text="startService"

android:onClick="myStartService" />

android:layout_height="wrap_content" android:text="stopService"

android:onClick="myStopService" />

android:layout_height="wrap_content" android:text="bindService"

android:onClick="myBindService" />

android:layout_height="wrap_content" android:text="unBindService"

android:onClick="myUnBindService" />

MainActivity.java

package com.service;

import android.app.Activity;

import ponentName;

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

}

}

java 制作动态手机壁纸_android 动态切换壁纸实例 利用service机制实现 附完整源码 带动态截图...

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。