Chinaunix首页 | 论坛 | 博客
  • 博客访问: 543137
  • 博文数量: 179
  • 博客积分: 3845
  • 博客等级: 中校
  • 技术积分: 2003
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-16 21:25
文章分类
文章存档

2012年(74)

2011年(105)

分类: 嵌入式

2011-07-21 23:01:38

代码清单:

SimpleActivity.java
  1. package foolstudio.demo;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.app.Application;
  6. import android.content.Context;
  7. import android.content.ContentResolver;
  8. import android.content.res.AssetManager;
  9. import android.content.res.Resources;
  10. import android.graphics.drawable.Drawable;
  11. import android.view.LayoutInflater;
  12. import android.view.MenuInflater;
  13. import android.view.Window;
  14. import android.view.WindowManager;
  15. import android.view.ViewGroup.LayoutParams;
  16. import android.view.Display;

  17. public class SimpleActivity extends Activity {    
  18.     private static final String TAG = "SIMPLE_ACT";
  19.     
  20.     /** Called when the activity is first created. */
  21.     @Override
  22.     public void onCreate(Bundle savedInstanceState) {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.main);
  25.         
  26.         Log.i(TAG, "Activity creating...");
  27.     }
  28.     
  29.     @Override
  30.     public void onStart() {
  31.         super.onStart();
  32.         
  33.         Log.i(TAG, "Activity starting...");
  34.         
  35.         showInfo();
  36.     }
  37.     
  38.     @Override
  39.     public void onStop() {
  40.         super.onStop();
  41.         
  42.         Log.i(TAG, "Activity stopping...");
  43.     }
  44.     
  45.     @Override
  46.     public void onDestroy() {
  47.         super.onDestroy();

  48.         Log.i(TAG, "Activity destroying...");
  49.     }
  50.     
  51.     //显示Activity组件关联组件信息
  52.     private void showInfo() {
  53.         Log.i(TAG, "Title: " + this.getTitle().toString() );
  54.         Log.i(TAG, "Calling activity: " + this.getCallingPackage() );
  55.         
  56.         //获取应用程序实例
  57.         Application app = this.getApplication();
  58.         Log.i(TAG, "Package:" + app.getPackageName() );
  59.         //获取应用程序上下文实例
  60.         Context appContext = this.getApplicationContext();
  61.         Log.i(TAG, "AppContext: " + appContext.toString() );
  62.         //获取资产管理器
  63.         AssetManager am = this.getAssets();
  64.         Log.i(TAG, "AssetManager: " + am.getLocales()[1]);
  65.         //获取基础上下文
  66.         Context baseContext = this.getBaseContext();
  67.         Log.i(TAG, "BaseContext: " + baseContext.toString() );
  68.         //内容解决者
  69.         ContentResolver resolver = this.getContentResolver();
  70.         Log.i(TAG, "Resolver: " + resolver.toString() );
  71.         //布局填充器
  72.         LayoutInflater inflater = this.getLayoutInflater();
  73.         Log.i(TAG, "LayoutInflater: " + inflater.toString() );
  74.         //菜单填充器
  75.         MenuInflater inflater2 = this.getMenuInflater();
  76.         Log.i(TAG, "MenuInflater: " + inflater2.toString() );
  77.         //资源管理器
  78.         Resources resources = this.getResources();
  79.         Log.i(TAG, "Resources: " + resources.toString() );
  80.         //墙纸
  81.         Drawable wallpaper = this.getWallpaper();
  82.         Log.i(TAG, "Wallpaper: " + wallpaper.toString() );
  83.         //获取窗体实例
  84.         Window window = this.getWindow();
  85.         LayoutParams layoutParams = window.getAttributes();
  86.         
  87.         Log.i(TAG, "Layout height: " + layoutParams.height +
  88.                     ", width: " + layoutParams.width);
  89.         //获取窗体管理器
  90.         WindowManager wm = this.getWindowManager();
  91.         Display display = wm.getDefaultDisplay();
  92.         Log.i(TAG, "Window height: " + display.getHeight() +
  93.                     ", width: " + display.getWidth() );
  94.     }
  95. };
main.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent">
  6.     <TextView
  7.      android:layout_width="fill_parent"
  8.      android:layout_height="wrap_content"
  9.      android:text="@string/hello"/>
  10. </LinearLayout>
AndroidManifest.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android=""
  3.       package="foolstudio.demo"
  4.       android:versionCode="1"
  5.       android:versionName="1.0">
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  7.         <activity android:name=".SimpleActivity" android:label="@string/app_name">
  8.             <intent-filter>
  9.                 <action android:name="android.intent.action.MAIN" />
  10.                 <category android:name="android.intent.category.LAUNCHER" />
  11.             </intent-filter>
  12.         </activity>
  13.     </application>
  14.     <uses-sdk android:minSdkSimpleActivity.javaVersion="3" />
  15. </manifest>
Activity关联组件

从表现形式上,Activity程序用来提供可视界面,但是在后台方面,Activity几乎关联了Android应用程序中的大
部分组件。从SimpleActivity.java中的“showInfo"方法(第41行)可以看出,Activity的关联组件有:应用程序
、应用程序上下文、资产管理器、基础上下文、内容解决者、布局填充器、菜单填充器、资源管理器、墙纸、窗
体布局和窗体管理器。






阅读(1374) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~