分享

Android简单开发教程(二)

 小飞苑 2011-04-16
3.Android 应用解析
资源文件解析:
AndroidManifest.xml 样例,代码如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas./apk/res/android"
package="com.test.AndroidTMS.views"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/tmslogo" android:label="@string/app_name">
<activity android:name=".AndroidTMS"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-sdk android:minSdkVersion="5" />
</manifest>
AndroidManifest.xml解析
manifest
根节点,描述了package中所有的内容
xmlns:android
包含命名空间的声明。xmlns:android=http://schemas.
/apk/res/android,使得Android中各种标准属性能在文件中使用,提供了大部分元素中的数据
Package
声明应用程序包
application
包含packageapplication级别组件声
明的根节点。此元素也可包application的一些全局和默认的属性,如标签、icon、主题、必要的权限,等等。一个manifest能包含零个或一个此元素(不能大于一个)
android:icon
应用程序图标
android:label
应用程序名字
Activity
用来与用户交互的主要工具。Activity是用户打开一个应用程序的初始页面,大部分
被使用到的其他页面也由不同activity所实现,并声明在另外的activity标记中。
注意,每一个activity必须有一个<activity>标记对应,无论它给外部使用或是只用于自己的package中。如果一个activity没有对应的标记,你将不能运行它。另外,为了支持运行时查找Activity,可包含一个或多个<intent-filter>元素来描述activity所支持的操作
android:name
这个activity对应的类别
android:label
这个activity的代号
intent-filter
声明了指定的一组组件支持的Intent值,从而形成了IntentFilter。除了能在此元素下指定不同类型的值,属性也能放在这里来描
述一个操作所需的唯一的标签、icon和其他信息
action
组件支持的Intent action
category
组件支持的Intent Category。这里指定
了应用程序默认启动的activity
uses-permission
应用需要的权限应当在此处申请,所申请的权限应当被系统或某个应用所定义,否则视为无效申请。同时,使用权限的申请需要遵循权限授予条件,非platform认证的应用无法申请高级权限。
uses-sdk
该应用程序所使用的sdk版本相关
Strings.xml解析
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, AndroidTMS!</string>
<string name="app_name">AndroidTMS</string>
</resources>
这个文件很简单,就定义了两个字符串资源,与R.java中对应的索引,
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
String资源的使用
Resources r = this.getContext().getResources();
String appname= ((String) r.getString(R.string.app_name));
String hello= ((String) r.getString(R.string.hello))
;
AndroidManifest.xml中调用
<activity android:name=".AndroidTMS"
android:label="@string/app_name">
项目的布局文件layout中资源文件的解析
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas./apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical" android:gravity="center_vertical">
<TextView android:id="@+id/username_view"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_marginLeft="60dip" android:layout_marginRight="30dip"
android:text="用户信息" android:gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView android:id="@+id/dataInfo" android:layout_marginRight="10dip"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dip"></TextView>
<Button android:text="退出程序" android:id="@+id/btnExit"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
R.java中对应的索引
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int btnExit=0x7f050002;
public static final int dataInfo=0x7f050001;
public static final int username_view=0x7f050000;
}
public static final class layout {
public static final int main=0x7f030000;
}
layout布局文件的加载
setContentView(R.layout.user_info);
layout布局文件中按钮的调用
Button btn=(Button)findViewById(R.id.btnExit);
按钮的监听事件,当用户点击按钮的时候询问用户是否要退出改程序,如果选择是,退出程序,选择否不执行任何操作
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog dlg = new AlertDialog.Builder(ArtistsActivity.this)
.setTitle("Login Exit").setMessage("您要退出程序??")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
}
}) .setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}).create();
dlg.show();
}
});
程序解析:
public class AndroidTMS extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=(TextView) findViewById(R.id.dataInfo);
TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
String imei =tm.getSimSerialNumber();
tv.setText(imei+”sim卡号”);
Button btn=(Button) findViewById(R.id.btnExit);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog dlg = new AlertDialog.Builder(ArtistsActivity.this)
.setTitle("Login Exit").setMessage("您要退出程序??")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
}
}) .setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}).create();
dlg.show();
}
});
}
}
主程序AndroidTMS类继承自TabActivity类,重写了void onCreate(Bundle savedInstanceState)方法。在onCreate方法中通setContentView(R.layout.main)设置了Activity要显示的布局文件(\layout\main.xml)。程序中的String imei =tm.getSimSerialNumber();是读取SIM卡序列号,需要有权限,所以 在Androidmani.xml 中加入了权限
<uses-permission android:name="android.permission.READ_PHONE_STATE">
</uses-permission>

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多