|
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解析
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中調(diào)用
<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布局文件中按鈕的調(diào)用
Button btn=(Button)findViewById(R.id.btnExit);
按鈕的監(jiān)聽事件,當用戶點擊按鈕的時候詢問用戶是否要退出改程序,如果選擇是,退出程序,選擇否不執(zhí)行任何操作
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)設(shè)置了Activity要顯示的布局文件(\layout\main.xml)。程序中的String imei =tm.getSimSerialNumber();是讀取SIM卡序列號,需要有權(quán)限,所以 在Androidmani.xml 中加入了權(quán)限
<uses-permission android:name="android.permission.READ_PHONE_STATE">
</uses-permission>
|
|
|