Android開發筆記--基礎篇(二)

       學習的過程總是很無聊的,不過還好,有手機相伴。隨便寫了兩個app,放在自己手機上,雖然都是練習的程序,但是,還是寫了點東西的,也是爲了學習嘛。

       其實我不打算寫什麼原理的東西,只是簡單的說下Activity的生命週期和Service的生命週期吧。說到Activity的生命週期,肯定要按照這張圖來說,但是其實如果你用程序學習的話,可以打log來看。

      其實從圖就可以看出有這幾個狀態:onCreate();onStart();onResume();onPause();onRestart();onDestroy()。這幾個的轉換關係從圖也可以看得出來,就是開始啓動一個Activity時,就依次轉換了開始三個狀態,然後就啓動了,也就是你看得見那個界面,說明在前臺運行了。然後有可能要暫停,這就是onPause()這個狀態,但是這個狀態其實還是在前臺的,就是你還是可以看到的那個。但是很快就不可以看到了,因爲你改去做別的Activity了,你這個Activity就步可見了,也就是我們說的後臺運行了。這時有兩種可能性,一種就是你過了一會兒,又回到這個Activity了,所以就是onRestart()這個狀態,然後就重頭來一遍,但是步用創建這個狀態,因爲他本來就存在。但是還有另外一種可能性,就是內存步足了,然後要想辦法,所以就把你這個Activity給kill了,所以就沒有了,如果你還要回來,沒辦法,只能再創建一次了,這次是真正的重頭開始一次。當然,這裏其實還有第三種情況,就是你後臺運行了,用戶自己步想要這個Activity了,主動kill了,就是onDestroy()這個狀態,徹底摧毀了這個Activity。

       那個Service的生命週期其實就比這個要簡單點,因爲就兩個順序流程。

       這裏分爲兩種Service,一種是unbounded service,一種是bounded service。第一種只有三個狀態:onCreate();onStartCommand();onDestroy()。這其實就和Activity類似,創建,然後開始,然後就讓service運行了,但是最後要關閉service就會經歷摧毀的過程。同理,第二種的service其實只是比第一種多了一個在摧毀前的onUnbind()這個的意思就是解除綁定。這就要從第一種和第二種的區別說起了,第一種就是簡單的一對一,一個服務對一個用戶,所以用戶選擇關閉服務時,就直接摧毀服務就可以了。但是第二種是針對多用戶,(當然也可以是一個用戶),每一個用戶都要在創建服務的時候綁定一次,也就是onBind();然後只有所有用戶都解除綁定了,這時纔會摧毀這個服務,這裏加入onUnbind()也就是爲了每個用戶不要這個服務的時候解除綁定,當發現沒有用戶的時候,就不要這個服務了,也就這點差別。

       這個Activity和Intent的使用,我做了一個app,但是就是簡單的用Intent在兩個Activity之間傳數據。這裏要說下Intent,其實他就可以看着是一個對象,你用它在Activity或者Service之間以及後面要學習的組件之間傳遞東西,這個東西可以是數據,圖片等等。代碼和演示我就步展示了,這個比較簡單。

       

        第二個主要是針對UI設計的,我做了一些主要的設計,當然還有很多沒用到。主要的都有了,這裏就不多說了,直接把代碼貼出來,不懂的直接看官網資料。

MainActivity.java的代碼如下:

package com.example.view;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.*;
import android.widget.*;



public class MainActivity extends Activity {

	private ProgressBar pb;
	String[] friends = {"黃平","傻明","傻博","海牛寶寶愛學習","小規小烏龜","王子明","校長","銳哥","章童鞋","婷婷姐"};
	ArrayAdapter<String> adapter;
	
	private void display(String text){
		Toast.makeText(this, text,Toast.LENGTH_SHORT).show();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		//TextView
		TextView tv =(TextView)findViewById(R.id.textview1);
		
		//Button and EditView
		Button button1 =(Button)findViewById(R.id.button1);
		button1.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				EditText ev =(EditText)findViewById(R.id.edittext1);
				String str=ev.getText().toString();
				display(str);
			}
		});
		
		//RadioGroup
		RadioGroup radiogroup1 = (RadioGroup)findViewById(R.id.radiogroup1);
		radiogroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(RadioGroup arg0, int arg1) {
				// TODO Auto-generated method stub
				if(arg1==R.id.radiobutton1)
					display("your choice is a boy!");
				else if(arg1==R.id.radiobutton2)
					display("your choice is a girl!");
			}
			
		});
		
		//CheckBox
		CheckBox checkbox1=(CheckBox)findViewById(R.id.checkbox1);
		CheckBox checkbox2=(CheckBox)findViewById(R.id.checkbox2);
		CheckBox checkbox3=(CheckBox)findViewById(R.id.checkbox3);
		
		checkbox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked)
					display("good good study!");
			}
		});
		checkbox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked)
					display("day day play!");
			}

			
			
		});
		checkbox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked)
					display("It's time for sleep!");
			}
		});
		
		//Spinner
		adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,friends);
		Spinner spinner1 = (Spinner)findViewById(R.id.spinner1);
		spinner1.setAdapter(adapter);
		spinner1.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				if(arg2==0)
					display("黃平是好朋友!");
				else if(arg2==1)
					display("傻明很傻!");
				else if(arg2==2)
					display("傻博是王博!");
				else if(arg2==3)
					display("我的博客暱稱是海牛寶寶愛學習");
				else if(arg2==4)
					display("王子規的博客暱稱是小規小烏龜");
				else if(arg2==5)
					display("王子明在非洲酷斃!!");
				else if(arg2==6)
					display("校長在華科do research!");
				else if(arg2==7)
					display("銳哥暗戀中哥很久了!");
				else if(arg2==8)
					display("章童靴是資環學院的妹紙!");
				else if(arg2==9)
					display("婷婷姐是華農的妹紙!");
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		//ProgressBar
		Button button2 = (Button)findViewById(R.id.button2);
		button2.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				pb = (ProgressBar)findViewById(R.id.progressbar1);
				pb.setVisibility(0);
				
			}});
		Button button3 = (Button)findViewById(R.id.button3);
		button3.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				pb.setVisibility(4);
				
			}});
		
		//Notification
		Button button4 = (Button)findViewById(R.id.button4);
		button4.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//得到一個NotificationManager對象
				NotificationManager notifymanager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
				Notification notify =new Notification(R.drawable.ic_launcher,"通知",System.currentTimeMillis());
				PendingIntent pi =PendingIntent.getActivity(MainActivity.this, 123123, new Intent(Intent.ACTION_DIAL,Uri.parse("15002750341")), 0);
				notify.setLatestEventInfo(MainActivity.this, "通知", "你又在亂看提醒!你是猴子請來的逗比麼?", pi);
				notifymanager.notify(1, notify);
			}});

	}
	
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if(item.getItemId()==R.id.item1)
			finish();
		else if(item.getItemId()==R.id.item2)
		{
			//彈出對話框
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			LayoutInflater li = this.getLayoutInflater();
			View view =li.inflate(R.layout.dialog, null);
			builder.setTitle("關於");
			builder.setIcon(R.drawable.ic_launcher);
			builder.setView(view);
			builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					dialog.cancel();
				}
			}).create().show();
			
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

layou裏面的兩個文件main.xml和dialog.xml代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
	android:id="@+id/widget0"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	xmlns:android="http://schemas.android.com/apk/res/android">
<RadioGroup
	android:id="@+id/radiogroup1"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:layout_x="63dp"
	android:layout_y="130dp">
<RadioButton
	android:id="@+id/radiobutton1"
	android:layout_width="146dp"
	android:layout_height="40dp"
	android:text="girl" />
<RadioButton
	android:id="@+id/radiobutton2"
	android:layout_width="144dp"
	android:layout_height="37dp"
	android:text="boy" />
</RadioGroup>
<TextView
	android:id="@+id/textview1"
	android:layout_width="wrap_content"
	android:layout_height="25dp"
	android:text="陳海中的測試"
	android:layout_x="112dp"
	android:layout_y="7dp" />
<EditText
	android:id="@+id/edittext1"
	android:layout_width="200dp"
	android:layout_height="wrap_content"
	android:text="請輸入內容"
	android:textSize="18sp"
	android:layout_x="63dp"
	android:layout_y="35dp" />
<Button
	android:id="@+id/button1"
	android:layout_width="50dp"
	android:layout_height="wrap_content"
	android:text="OK!"
	android:layout_x="203dp"
	android:layout_y="84dp" />
<CheckBox
	android:id="@+id/checkbox1"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="study"
	android:layout_x="20dp"
	android:layout_y="222dp" />
<CheckBox
	android:id="@+id/checkbox2"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="play"
	android:layout_x="118dp"
	android:layout_y="224dp" />
<CheckBox
	android:id="@+id/checkbox3"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="sleep"
	android:layout_x="213dp"
	android:layout_y="225dp" />
<Spinner
	android:id="@+id/spinner1"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:layout_x="25dp"
	android:layout_y="269dp" />

<ProgressBar
    android:id="@+id/progressbar1"
    android:visibility="gone"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="33dp"
    android:layout_y="339dp" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="150dp"
    android:layout_y="354dp"
    android:text="@string/button2" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="232dp"
    android:layout_y="354dp"
    android:text="@string/button3" />

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="112dp"
    android:layout_y="406dp"
    android:text="@string/button4" />

</AbsoluteLayout>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
	<TextView
	android:id="@+id/textview2"
	android:layout_width="wrap_content"
	android:layout_height="25dp"
	android:text="@string/textview2"
 />
</LinearLayout>


     當然還有string.xml和一些別的,我覺得很簡單就步貼了,比方說還有圖標的修改,直接在drawable裏面添加就可以了,不過,如果不行的話,記得重啓手機,我重啓之後就好了。

(開始界面)

(隨便按了幾個鍵的測試效果綜合)

(notification)

(menu)

(dialog)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章