目录
四大组件:
Activity、Service、BroadcastReceiver、Content Provider。
一、Activity
1. 生命周期(4种状态)
Active/Running状态
Paused状态
Stopped状态
Killed Activity状态
3种生命周期:
完整生命周期 onCreate() ~ onDestory()
可见生命周期 onStart() ~ onStop()
前景生命周期 onResume() ~ onPause()
2. intent的使用
(省略)
3. 使用Bundle在Activity之间传递数据
Bundle相当于Map类,即通过(Key,Value)方式描述数据,用Bundle绑定数据,便于数据处理。Bundle提供了putXXX()和getXXX()方法来保存和获取数据。
栗子:模拟评论功能
第一个界面:
第二个界面:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="300dp"
android:hint="请输入"
/>
<Button
android:id="@+id/but1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交" />
</LinearLayout>
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SecondActivity">
<TextView
android:id="@+id/showMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FF000000"
/>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private EditText myText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//绑定
myText = (EditText) this.findViewById(R.id.content);
Button button = this.findViewById(R.id.but1);
//点击事件
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String str = myText.getText().toString();
Intent intent = new Intent();
Bundle bundle = new Bundle();
intent.setClass(MainActivity.this,SecondActivity.class);
bundle.putString("message",str);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
}
SecondActivity.java
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Bundle bundle = this.getIntent().getExtras();
String message = bundle.getString("message");
TextView myText = (TextView) this.findViewById(R.id.showMessage);
myText.setText(message);
}
}
二、Service
service没有用户界面,运行在后台,负责一些用户看不到、并且会有持续事件的事情。
(1)自己创建的Service需要继承 android.app.Service 类,并且要在AndroidManifest.xml文件中通过标签注册。
(2)可以通过startService() 或 bindService() 方法启动service,通过stopService() 方法或者unBindService() 方法停止Service。
(3)Service 是跑在程序主线程中的,处理耗时事情需要启动一个线程,以防止阻塞主线程。
(4)当第一次启动Service时,先后调用了onCreate() 、 onStart() 。
当停止Service时,执行 onDestroy()。
当Service为启动状态时,再启动Service,不会执行 onCreate() 方法,而直接执行 onStart() 方法。
三、Content Provider
作用:为每个应用准备一个对外提供数据的方式,使得不同应用间的可以数据共享。
访问Content Provider 中的数据主要通过 ContentResolver 对象。
例如:
查询Content Provider的方法:
(1)ContentResolver类中的 query()方法
(2)Activity 对象的 managedQuery(),返回的都是 Cursor对象。
四、 BroadcastReceiver
在应用程序之间进行传输信息的机制。
栗子:监听电池电量信息
(监听电量是不能静态注册的。)
.
不能静态注册的广播:
android.intent.action.SCREEN_ON
android.intent.action.SCREEN_OFF
android.intent.action.BATTERY_CHANGED
android.intent.action.CONFIGURATION_CHANGED
android.intent.action.TIME_TICK
另外,不能像组件那样在manifests里声明一个receive ,只能通过Context.registerReceiver()注册。
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/batteryView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
/>
</LinearLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private TextView batteryView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
batteryView = (TextView) this.findViewById(R.id.batteryView);
}
protected void onPause() {
super.onPause();
unregisterReceiver(batteryInfoReceiver);//注销广播监听事件
}
protected void onResume() {
super.onResume();
registerReceiver(batteryInfoReceiver, new IntentFilter(
Intent.ACTION_BATTERY_CHANGED
));
}
private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())){
int level = intent.getIntExtra("level",0);//当前电量
int scale = intent.getIntExtra("scale",100);//总电量
batteryView.setText("Battery level:"
+ String.valueOf(level * 100 / scale) + "%");
}
}
};
}
五、补充
1. AndroidMainfest.xml代码解释
关于 android:exported = true
在Activity中该属性用来标示:当前Activity是否可以被另一个Application的组件启动:true允许被启动;false不允许被启动。
android:exported 是Android中的四大组件 Activity,Service,Provider,Receiver 四大组件中都会有的一个属性。
总体来说它的主要作用是:是否支持其它应用调用当前组件。
如果Service等的AndroidManifest中声明为android:exported=”false”
则该服务不能够跨进程使用。
Permission Denied!