当前位置:文档之家› Android四大核心组件

Android四大核心组件


求 真
务 实
敬 业 进 取
求 真
务 实
敬 业 进 取
android:id="@+id/button1"
android:id="@+id/button2"
求 真
务 实
敬 业 进 取
public class MainActivity extends Activity implements OnClickListener { private Button button1; private Button button2; private Intent serviceIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(yout.activity_main); serviceIntent = new Intent(this,EchoService.class); button1 = (Button) this.findViewById(R.id.button1); button2 = (Button) this.findViewById(R.id.button2); button1.setOnClickListener(this); button2.setOnClickListener(this);
求 真 务 实
敬 业 进 取
求 真
务 实
敬 业 进 取
(2)bind service
android:id="@+id/button3"
android:id="@+id/button4"lic class MainActivity extends Activity implements OnClickListener, ServiceConnection
求 真
务 实
敬 业 进 取
3、activity生命周期的七个方法
求 真
务 实
敬 业 进 取
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(yout.activity_main); System.out.println("我生成了"); } @Override protected void onStart() { super.onStart(); System.out.println("我开始了"); } @Override protected void onResume() { super.onResume(); System.out.println("我恢复 了"); } 求 真 务 实 敬
service的生命周期,从它被创建开始,到它被销毁为止,可以 有两条不同的路径: (1)A started service 被开启的service通过其他组件调用 startService()被创建。 这种service可以无限地运行下去,必须调用stopSelf()方法 或者其他组件调用stopService()方法来停止它。 当service被停止时,系统会销毁它。 (2)A bound service 被绑定的service是当其他组件(一个客户)调用 bindService()来创建的。
Android 四大核心组件
求 真 务 实 敬 业 进 取
一、Activity
1、activity概念 • Activity:代表了Android程序的展现层,比 如用户看到的界面。一个Android程序会有 一些个Activities,在程序运行过程中也会切 换。
求 真
务 实
敬 业 进 取
2、activity三种状态
求 真
务 实
敬 业 进 取
二、Service
1、Service概念 • Android中的Service,其意思是“服务”, 它是在后台运行,不可交互的。Service自 己不能运行,需要通过某一个Activity或者 其它Context对象来调用
求 真
务 实
敬 业 进 取
2、service生命周期
import android.app.Service; import android.content.Intent; @Override import android.os.IBinder; public void onCreate() { super.onCreate(); public class EchoService extends Service { System.out.println(" 启动"); } @Override public IBinder onBind(Intent arg0) { @Override return null; public void onDestroy() { } super.onDestroy(); } System.out.println("销毁"); }
private EditText editText; Intent i = new Intent(MainActivity.this,Aty1.cl ass); editText = (EditText) findViewById(R.id.editText1); String text = editText.getText().toString(); //i.putExtra("txt", text); Bundle data = new Bundle(); data.putString("txt", text); i.putExtras(data); startActivity(i);
求 真 务 实
敬 业 进 取
@Override public void onClick(View v) { switch(v.getId()){ case R.id.button1: startService(serviceIntent); break; case R.id.button2: stopService(serviceIntent); break; } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; }
private Button button3; private Button button4; button3 = (Button) this.findViewById(R.id.button3); button3.setOnClickListener(this); button4 = (Button) this.findViewById(R.id.button4); button4.setOnClickListener(this); 求 真
• 运行:当一个Activity在屏幕的最上层时(系统堆栈 中的最顶端),此Activity就是属于active或running 的状态。 • 停止:当一个Activity被其它的Activity完全遮蔽, 被遮蔽Activity就是处于Stop的状态。 • 暂停:当一个Activity失去焦点(Focus)但还看得到 它的画面,那失去焦点的这个Activity则处在 Paused的状态。
@Override public void onClick(View arg0) { Intent i = new Intent(MainActivity.this,Aty1.class); startActivity(i); } }); } 求 真 务 实
敬 业 进 取
5、activity数据传递
4、activity操作
• (1)生成一个Android Activity包括一个继承于Activity子类Aty1、 activity_aty1.xml、AndroidManifest.xml中进行配置
求 真
务 实
敬 业 进 取
public class MainActivity extends Activity { private Button button1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(yout.activity_main); button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() {
求 真
务 实
敬 业 进 取
重新进入程序user navigates to the activity
user returms to the activity
返回按钮the activity is finishing or being destroyed by the system
求 真
务 实
敬 业 进 取
求 真
务 实
敬 业 进 取
方式一
传送简单的数据
private TextView txtView; txtView = (TextView) findViewById(R.id.textView1); txtView.setText(getIntent().get StringExtra("txt"));
相关主题