SuperbookKing

11.后台执行之Service

Service is an application component that can perform long-running operations in the background and does not provide a user interface.

 

Service 后台长时间执行的控件

service 声明:

<service android:name=".service.ExampleSercice" />

Started 调用者与服务者没关系

oncreate->onstartCommand            ->ondestroy

Bound 调用者与服务者绑定 调用者退出 绑定的服务者也退出(继承binder方式)

1. myservice extends Sevice

private MyBinder myBinder = new MyBinder();
public class MyBinder extends Binder{
public BinderSercice getBoundSercice(){
return BinderSercice.this;
}
}
@Override
public IBinder onBind(Intent intent)
{
Log.i(TAG, "BinderSercice-->onBind");
return myBinder;
}

public void myMethod(){
Log.i(TAG, "BinderSercice-->myMethod");
super.onDestroy();
}

2.myActivity

private Boolean isConnected = false;
private ServiceConnection conn = new ServiceConnection() {

public void onServiceDisconnected(ComponentName name)
{
Log.i(TAG, "MainActivity-->onServiceDisconnected");
isConnected = false;
}

public void onServiceConnected(ComponentName name, IBinder binder)
{
Log.i(TAG, "MainActivity-->onServiceConnected");
isConnected = true;
MyBinder myBinder = (MyBinder)binder;
myBinder.getBoundSercice().myMethod();
}
};

/////////////////////

case R.id.onBindService:
bindService();
break;
case R.id.unbindService:
unbindService();
break;

//////////////////////

protected void unbindService()
{
Log.i(TAG, "MainActivity-->unbindService");
if (isConnected)
{
unbindService(conn);

}
}

protected void bindService()
{
Log.i(TAG, "MainActivity-->bindService");
Intent intent =new Intent(DemoAndroidActivity.this,BinderSercice.class);
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}

3 IntentService 多线程Service(执行完线程自动停止----一个请求)

myactivity

case R.id.onStartIntentService:
intent.setClass(DemoAndroidActivity.this, MyIntentService.class);
Log.i(TAG, "zhu线程-->onStartIntentService"+Thread.currentThread().getId());
startService(intent);

 

public class MyIntentService extends IntentService
{
	private static final String TAG = "MainActivity";
	public MyIntentService()
	{
		super("MyIntentService11");
		// TODO Auto-generated constructor stub
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId)
	{
		// TODO Auto-generated method stub
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	protected void onHandleIntent(Intent intent)
	{
		// TODO Auto-generated method stub
try
{
	Log.i(TAG, "service 线程文件下载:"+Thread.currentThread().getId());
	showNotification(false);
	Thread.sleep(10000);
} catch (InterruptedException e)
{
	// TODO Auto-generated catch block
	e.printStackTrace();
}
showNotification(true);
	}

	private void showNotification(boolean isFinish)
	{Notification notification
		;
		Intent intent = new Intent(this, DemoAndroidActivity.class);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent , PendingIntent.FLAG_UPDATE_CURRENT);
		if (isFinish)
		{
			notification= new Notification(R.drawable.title, "下载完成", System.currentTimeMillis());
				notification.setLatestEventInfo(this, "下载完成title", "下载完成。。。", contentIntent );
		}else {
			notification= new Notification(R.drawable.title, "正在下载", System.currentTimeMillis());
				notification.setLatestEventInfo(this, "正在下载title", "正在下载。。。", contentIntent );
		}
		notification.defaults|=Notification.DEFAULT_VIBRATE;
		notification.defaults|=Notification.DEFAULT_LIGHTS;
		notification.defaults|=Notification.DEFAULT_SOUND;
		//notification.l
		NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		
		notificationManager.notify(R.layout.main, notification);
	}

}

  4.消息处理(----多个请求)

intent.setClass(DemoAndroidActivity.this, ExampleSercice.class);
startService(intent);

public class ExampleSercice extends Service
{
private static final String TAG = "MainActivity";
private ServiceHandler handler;
private class ServiceHandler extends Handler{
public ServiceHandler(Looper looper)
{
super(looper);
}
@Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case 1:
try
{
Thread.sleep(5000);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i(TAG, "ServiceHandler-->msg111"+Thread.currentThread().getId());

break;

default:
break;
}stopSelf(msg.arg1);
}
};
@Override
public IBinder onBind(Intent intent)
{
Log.i(TAG, "ExampleSercice-->onBind");
return null;
}

@Override
public boolean onUnbind(Intent intent)
{
Log.i(TAG, "ExampleSercice-->onUnbind");
return super.onUnbind(intent);
}

@Override
public void onRebind(Intent intent)
{
Log.i(TAG, "ExampleSercice-->onRebind");
super.onRebind(intent);
}

@Override
public void onCreate()
{

Log.i(TAG, "ExampleSercice-->onCreate"+Thread.currentThread().getId());
Looper looper =Looper.getMainLooper();
handler = new ServiceHandler(looper);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.i(TAG, "ExampleSercice-->onStartCommand");
Message msg = handler.obtainMessage();
msg.arg1=startId;
msg.what=1;
msg.obj="111111";
handler.sendMessage(msg);
return START_NOT_STICKY;
}

@Override
public void onDestroy()
{
Log.i(TAG, "ExampleSercice-->onDestroy");
super.onDestroy();
}

}

posted on 2012-07-30 17:32  SuperbookKing  阅读(222)  评论(0)    收藏  举报