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)


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