---
title: "safe07"
author: "xpchou"
date: "2012-08-20T13:28:08Z"
tags: []
categories: ["移动开发"]
slug: "safe07"
draft: false
lastmod: "2012-08-19T21:56:08Z"
id: 1637555
url: "https://xpchou.iteye.com/blog/1637555"
---

1.service 系统的一个组件. 可以理解为长期在后台运行的没有界面的activity.
  两种开启服务的方法:
	1. startService(intent); 服务一旦开启就会长期的在后台运行,调用者退出,服务不受影响.
	2. bindService();        好基友. 如果调用者挂了,服务也会跟着挂掉.

2.startService() 调用者不能 调用服务里面的方法.
  bindService()  调用者可以调用到服务里面的方法.

3.调用绑定服务
  1.绑定服务bindService(intent,conn,Context.BIND_AUTO_CREATE)
  2,当服务被绑定的时候，会调用里面的onBind()方法,返回一个IBinder()
  3.在调用activity里面conn的实现类里面有一个onServiceConnected()
  4.利用IBinder对象调用服务里面的方法。
  注意：一般通过接口的方式 保存服务里面方法的引用

4.
本地服务: 服务和调用者在同一个进程里面.
远程服务: 服务和调用者不在同一个进程里面.

调用远程服务里面的方法: aidl (android interface defination language)


本地服务: public class MyBinder extends Binder implements IService
远程服务：private class MyBinder extends IService.Stub


本地服务调用者 调用的时候:iService = (IService) service;
远程服务                 :iService = IService.Stub.asInterface(service);



服务只会开启一次, oncreate只会在服务创建的时候 执行 ,多次调用startService()方法,不会重复开启服务.
多次调用stopService的方法 ,服务只会被停止一次,多次调用不会报错.

服务只会被开启一次, 多次调用bindService的方法 如果服务已经开启了 就不会再去执行oncreate().
如果服务是采用bind的方式 绑定的只能解绑一次.

如果服务被绑定过,就不能通过stopService的方法 停止服务了,
要想停止服务 必须先手动的把服务解除绑定.然后才能停止服务

5.Looper.prepare();创建一个消息链，一个死循环，是一个阻塞式方法，在子线程中调用可以，不推荐在主线程中调用
  Looper.loop();

6.设置一个位移动画，并设置动画的时长，以及在一个view上添加这个设置的动画
	TranslateAnimation ta = new TranslateAnimation(
						Animation.RELATIVE_TO_SELF, 0,
						Animation.RELATIVE_TO_SELF, 1.0f,
						Animation.RELATIVE_TO_SELF, 0,
						Animation.RELATIVE_TO_SELF, 0);
				ta.setDuration(500);
				view.startAnimation(ta);

7.获取当前正在运行的程序的包名
	String currentpackname = ActivityManager.getRunningTasks(1).get(0).topActivity.getPackageName();


8.获取当前正在运行的task需要配置GET_TASK权限

9.让这个intent在这个新的任务栈中开启任务
	intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

10.在Activity里，把后退的按键给屏蔽了
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK){
			return true;//把后退的按键给屏蔽了
		}
		return super.onKeyDown(keyCode, event);
	}

11.android:excludeFromRecents="true" 
   把当前的activity从最近的任务栈中移除

12.<activity android:name=".UnistallDialogActivity" 
        android:theme="@android:style/Theme.Dialog">
   </activity>

13.把当前activity的标题隐藏掉
   requestWindowFeature(Window.FEATURE_NO_TITLE);

14.通过系统的内存信息,获取可用内存
   MemoryInfo outInfo = new MemoryInfo();
   am.getMemoryInfo(outInfo);
   
   long availMem = outInfo.availMem();

15.注册一个内容观察者
	第一个参数为给指定uri注册的观察者，第二个参数为包括这个uri根节点下的所有路径，
	context.getContentResolver().registerContentObserver(uri,true,new observer);

16.反注册掉内容观察者
	context.getContentResolver().unRegisterContentObserver(observer);

17.清单文件里，activity节点下的 android:excludeFromRecents="true" 表示把它从最近的任务栈中删除