#关系
ViewGroup 是从view继承的
Layout 是ViewGroup 继承的,布局可以添加元素
片段里可以存放布局
Activity 可以移除 Fragment,增加片段,通过 getFragmentManager 来管理 Fragment 通过findFragmentById来获得
Fragment的元素修改通过 getView() 取得layout 之后修改里面的元素
如果 Layout 还没有实例化,通过 LayoutInflater 来操作通过inflate方法可以找到layout文件夹下的布局并实例化
Context 上下文 常用之类为Service Activity Application 等
Activity显示组件都在widget包下
#视图注意
android 的视图都非线程安全的,所以在子线程操作视图,会触发异常
处理方法,在子线程中调用 Handler 把数据发送给主线程,在主线程上做更新操作
包括 Timer 更新视图都不可以的,必须回到主线程才能更新视图
#线程间数据发送
android 中的线程间消息可以通过 Handler 来传送
Handler handler = new Handler(){
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
#往制定线程发送消息
#发送消息
handler.sendMessage(message);
##广播
#(全局监听)
<receiver android:name="com.example.a.br_s">
<intent-filter>
<action android:name="hi" />
</intent-filter>
</receiver>
-----------------------------------------------------------
public class br_s extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("hi")){
//
}
}
}
#(程序监听)
IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction("hi");
registerReceiver(mBroadcastReceiver, myIntentFilter);
-----------------------------------------------------------
BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//
}
};
#派发广播
Intent mIntent = new Intent("hi");
sendBroadcast(mIntent);
#服务
public class LocalService extends Service {
private static final String TAG = "LocalService";
private IBinder binder=new LocalService.LocalBinder();
@Override
public IBinder onBind(Intent intent) {
return binder;
}
MediaPlayer mediaPlayer=null;
@Override
public void onCreate() {
Log.i(TAG, "onCreate");
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
Log.i(TAG, "onStart");
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand");
return START_STICKY;
}
@Override
public void onDestroy() {
Log.i(TAG, "onDestroy");
super.onDestroy();
}
//定义内容类继承Binder
public class LocalBinder extends Binder{
LocalService getService(){
return LocalService.this;
}
}
}
#启动服务
private void startCustomService(){
Intent intent=new Intent(this,LocalService.class);
startService(intent);
}
#绑定服务
LocalService localService=null;
private void BinderService(){
Intent intent=new Intent(this,LocalService.class);
bindService(intent, new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName componentName, IBinder binder) {
//调用bindService方法启动服务时候,如果服务需要与activity交互,
//则通过onBind方法返回IBinder并返回当前本地服务
localService=((LocalService.LocalBinder)binder).getService();
//这里可以提示用户,或者调用服务的某些方法
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
localService=null;
//这里可以提示用户
}
}, Context.BIND_AUTO_CREATE);
}
android 笔记