activity生命周期
完整生命周期 oncreate--》onstart--》onresume--》onpause--》onstop--》ondestory
可视生命周期 onstart--》onresume--》onpause--》onstop
前台生命周期 onresume--》onpause 界面用户仍然可见,但是失去焦点
使用场景:
1.应用程序退出自动保存数据 ondestory oncreate
2.应用程序最小化 暂停的操作 onstop onstart 视频播放器
3.游戏的暂停和开始 前台生命周期
package com.itheima.lifecycle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
//被创建的时候调用的方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("oncreate");
}
//被销毁的时候调用的方法
@Override
protected void onDestroy() {
System.out.println("ondestory");
super.onDestroy();
}
//当activity界面用户可见的时候调用的方法
@Override
protected void onStart() {
System.out.println("onstart");
super.onStart();
}
@Override
protected void onRestart() {
System.out.println("onrestart");
super.onRestart();
}
//当activity界面用户不可见的时候调用的方法
@Override
protected void onStop() {
System.out.println("onstop");
super.onStop();
}
//界面开始获取到焦点对应的方法。 (界面按钮可以被点击,文本框可以输入内容)
@Override
protected void onResume() {
System.out.println("onresume");
super.onResume();
}
//界面失去焦点对应的方法(暂停)(按钮不可被点击,文本框不可输入内容,但是界面用户仍然能看见)
@Override
protected void onPause() {
System.out.println("onpause");
super.onPause();
}
public void click(View view){
Intent intent = new Intent(this,SecondActivity.class);
startActivity(intent);
}
}
横竖屏切换的生命周期
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="重拳" />
<TextView
android:id="@+id/tv_blood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="100" />
</RelativeLayout>
默认情况下横竖屏切换activity会被销毁然后重新创建。
package com.itheima.kof97;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
/**
* 默认情况下横竖屏切换activity会被销毁然后重新创建。
* @author Administrator
*
*/
public class MainActivity extends Activity {
private TextView tv_blood;
private int blood = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("oncreate");
setContentView(R.layout.activity_main);
tv_blood = (TextView) findViewById(R.id.tv_blood);
}
public void click(View view){
blood --;
tv_blood.setText("对方的生命值:"+blood);
if(blood<0){
Toast.makeText(this, "K.O.!", 1).show();
}
}
//被销毁的时候调用的方法
@Override
protected void onDestroy() {
System.out.println("ondestory");
super.onDestroy();
}
//当activity界面用户可见的时候调用的方法
@Override
protected void onStart() {
System.out.println("onstart");
super.onStart();
}
@Override
protected void onRestart() {
System.out.println("onrestart");
super.onRestart();
}
//当activity界面用户不可见的时候调用的方法
@Override
protected void onStop() {
System.out.println("onstop");
super.onStop();
}
//界面开始获取到焦点对应的方法。 (界面按钮可以被点击,文本框可以输入内容)
@Override
protected void onResume() {
System.out.println("onresume");
super.onResume();
}
//界面失去焦点对应的方法(暂停)(按钮不可被点击,文本框不可输入内容,但是界面用户仍然能看见)
@Override
protected void onPause() {
System.out.println("onpause");
super.onPause();
}
}
android:configChanges="orientation|keyboardHidden|screenSize"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.kof97"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:configChanges="orientation|keyboardHidden|screenSize"
android:name="com.itheima.kof97.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
任务栈的概念
1.一个应用程序一般都是由多个activity组成的。
2.任务栈(task stack)(别名back stack后退栈) 记录存放用户开启的activity的。
3.一个应用程序一被开启系统就给他分配一个任务栈,当所有的activity都退出的时候,任务栈就清空了。
4.任务栈的id是一个integer的数据类型 自增长的。
5.在android操作系统里面会存在多个任务栈,一个应用程序一个任务栈。
6.桌面应用和一般的应用程序是一样的,任务栈的行为也是一样。
7.默认情况下, 关闭掉一个应用程序,清空了这个应用程序的任务栈。应用程序的进程还会保留。
为什么要引入任务栈的概念:
windows下 可以通过点击任务栏 切换任务
android下 长按小房子 切换任务
为了记录用户开启了那些activity,记录这些activity开启的先后顺序,google引入任务栈(task stack)概念,帮助维护好的用户体验。
activity的启动模式
activity的启动模式:
1. standard 默认标准的启动模式, 每次startActivity都是创建一个新的activity的实例。
适用于绝大大数情况
2. singleTop 单一顶部,如果要开启的activity在任务栈的顶部已经存在,就不会创建新的实例,
而是调用 onNewIntent() 方法。
应用场景: 浏览器书签。 避免栈顶的activity被重复的创建,解决用户体验问题。
3. singletask 单一任务栈 , activity只会在任务栈里面存在一个实例。如果要激活的activity,在
任务栈里面已经存在,就不会创建新的activity,而是复用这个已经存在的activity,
调用 onNewIntent() 方法,并且清空当前activity任务栈上面所有的activity
应用场景:浏览器activity, 整个任务栈只有一个实例,节约内存和cpu的目的
注意: activity还是运行在当前应用程序的任务栈里面的。不会创建新的任务栈。
4. singleInstance 单态 单例模式
单一实例,整个手机操作系统里面只有一个实例存在。不同的应用去打开这个activity
共享 公用的同一个activity。
他会运行在自己单独,独立的任务栈里面,并且任务栈里面只有他一个实例存在。
应用场景:呼叫来电界面 InCallScreen
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.taskstack"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.taskstack.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.itheima.taskstack.SecondActivity"
android:launchMode="singleInstance"
>
<intent-filter>
<action android:name="com.itheima.task.single"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="我是界面01"
android:textSize="30sp"
/>
<Button
android:onClick="open01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="开启界面01" />
<Button
android:onClick="open02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="开启界面02" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="我是界面02"
android:textSize="30sp" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="open01"
android:text="开启界面01" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="open02"
android:text="开启界面02" />
</LinearLayout>
package com.itheima.taskstack;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
System.out.println("02activity被创建了。任务栈id:"+getTaskId());
}
public void open01(View view){
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
}
public void open02(View view){
Intent intent = new Intent(this,SecondActivity.class);
startActivity(intent);
}
@Override
protected void onNewIntent(Intent intent) {
System.out.println("02activityonnew intnet。任务栈id:"+getTaskId());
super.onNewIntent(intent);
}
}
package com.itheima.taskstack;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("01activity被创建了。任务栈id:"+getTaskId());
}
public void open01(View view){
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
}
public void open02(View view){
Intent intent = new Intent(this,SecondActivity.class);
startActivity(intent);
}
}
广播接收者ip拨号器
四大组件:
Activity
Content provider 内容提供者
Broadcast receiver 广播接受者
Service 服务
电台: 发送广播
收音机: 接受广播
android系统下的广播:
电池电量低。
电池充电完毕
短信到来了
程序安装卸载
sd卡卸载 安装
1.写一个类继承广播接受者
2.在清单文件配置关心的动作
3.一旦广播事件发生了,就会执行广播接受者的onreceive方法
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.ipdail"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.ipdail.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 配置广播接受者 -->
<receiver android:name="com.itheima.ipdail.OutCallReceiver">
<intent-filter>
<!-- 配置广播接收者关心的事件是外拨电话 -->
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
</application>
</manifest>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请设置ip号码" />
<EditText
android:id="@+id/et_number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="phone"
/>
<Button
android:onClick="save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="保存"
/>
</LinearLayout>
package com.itheima.ipdail;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
/**
* 1.创建一个收音机 继承广播接受者
*
*/
public class OutCallReceiver extends BroadcastReceiver {
//当接收到消息对应的方法
@Override
public void onReceive(Context context, Intent intent) {
String number = getResultData();
System.out.println("哈哈,有电话打出去了"+number);
SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
String ipnumber = sp.getString("ipnumber", "");
//判断是否是长途。是否有前缀
setResultData(ipnumber+number);
}
}
package com.itheima.ipdail;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et_number;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_number = (EditText) findViewById(R.id.et_number);
sp = getSharedPreferences("config", MODE_PRIVATE);
}
public void save(View view){
String ipnumber = et_number.getText().toString().trim();
if(TextUtils.isEmpty(ipnumber)){
Toast.makeText(this, "清除ip号码成功", 0).show();
}else{
Toast.makeText(this, "设置ip号码成功", 0).show();
}
Editor editor = sp.edit();
editor.putString("ipnumber", ipnumber);
editor.commit();
}
}
sd卡的广播事件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.sdcardmointor"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.sdcardmointor.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.itheima.sdcardmointor.SDStatusReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
<action android:name="android.intent.action.MEDIA_REMOVED"/>
<data android:scheme="file"></data>
</intent-filter>
</receiver>
</application>
</manifest>
package com.itheima.sdcardmointor;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class SDStatusReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "sd卡别移除,微信头像或者图片暂时不可用", 1).show();
}
}
短信广播接受者
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.smsreceiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.smsreceiver.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.itheima.smsreceiver.SmsReceiver">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
<!-- 配置广播接受者 -->
<receiver android:name="com.itheima.smsreceiver.OutCallReceiver">
<intent-filter android:priority="1000">
<!-- 配置广播接收者关心的事件是外拨电话 -->
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
</application>
</manifest>
package com.itheima.smsreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
/**
* 1.创建一个收音机 继承广播接受者
*
*/
public class OutCallReceiver extends BroadcastReceiver {
//当接收到消息对应的方法
@Override
public void onReceive(Context context, Intent intent) {
String number = getResultData();
if("5556".equals(number)){
setResultData(null);
}
}
}
package com.itheima.smsreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("短信到来了。 。。。");
Object[] objs = (Object[]) intent.getExtras().get("pdus");
for (Object obj : objs) {
// 得到短信对象
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) obj);
String body = smsMessage.getMessageBody();
String sender = smsMessage.getOriginatingAddress();
System.out.println("body:" + body);
System.out.println("sender:" + sender);
// 终止掉当前的广播。
if ("5556".equals(sender)) {
abortBroadcast();
}
}
}
}
自定义广播的发送和接受
发送广播的电台
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.sender"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.sender.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.itheima.sender;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 发送广播事件 (消息)
* @param view
*/
public void click(View view){
Intent intent = new Intent();
//自定义一个广播动作。
intent.setAction("com.itheima.sender.jiuminga");
//大吼一声 把消息发出去了。 无序广播
sendBroadcast(intent);
//发送有序广播
//sendOrderedBroadcast(intent, receiverPermission);
}
}
警察
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.police"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:theme="@android:style/Theme.Translucent"
android:name="com.itheima.police.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.itheima.police.PoliceReceiver">
<intent-filter >
<action android:name="com.itheima.sender.jiuminga"/>
</intent-filter>
</receiver>
</application>
</manifest>
package com.itheima.police;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class PoliceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "我是警察,我收到了消息", 1).show();
}
}
广播接受者的优先级
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:onClick="send1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="发送无序广播" />
<Button
android:onClick="send2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="发送有序广播" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.broadcasttest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.broadcasttest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.itheima.broadcasttest.Level1Receiver" >
<intent-filter android:priority="1000" >
<action android:name="com.itheima.broadcasttest.songwennuan" />
</intent-filter>
</receiver>
<receiver android:name="com.itheima.broadcasttest.Level2Receiver" >
<intent-filter android:priority="500" >
<action android:name="com.itheima.broadcasttest.songwennuan" />
</intent-filter>
</receiver>
<receiver android:name="com.itheima.broadcasttest.Level3Receiver" >
<intent-filter android:priority="100" >
<action android:name="com.itheima.broadcasttest.songwennuan" />
</intent-filter>
</receiver>
<receiver android:name="com.itheima.broadcasttest.FinalReceiver" >
<intent-filter android:priority="0" >
<action android:name="com.itheima.broadcasttest.songwennuan" />
</intent-filter>
</receiver>
</application>
</manifest>
package com.itheima.broadcasttest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 发送无序广播
* @param view
*/
public void send1(View view){
Intent intent = new Intent();
intent.setAction("com.itheima.broadcasttest.songwennuan");
intent.putExtra("msg", "发1万块");
//无序广播,不可被拦截,不可终止。
sendBroadcast(intent);
}
/**
* 发送有序广播
* @param view
*/
public void send2(View view){
Intent intent = new Intent();
intent.setAction("com.itheima.broadcasttest.songwennuan");
//有序广播,可被拦截,可终止,可以修改数据。
sendOrderedBroadcast(intent, null, new FinalReceiver(), null, 0, "给农民兄弟发10000块钱", null);
}
}
package com.itheima.broadcasttest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class Level1Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String message = getResultData();
System.out.println("省级部门得到中央的消息:"+message);
abortBroadcast();
setResultData("给农民兄弟发5000块钱");
}
}
package com.itheima.broadcasttest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class Level2Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String message = getResultData();
System.out.println("市级部门得到省级的消息:"+message);
setResultData("给农民兄弟发2000块钱");
}
}
package com.itheima.broadcasttest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class Level3Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String message = getResultData();
System.out.println("乡级部门得到市的消息:"+message);
setResultData("给农民兄弟发两大大米");
}
}
package com.itheima.broadcasttest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class FinalReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String message = getResultData();
System.out.println("农民兄弟得到乡的消息:"+message);
}
}
进程的优先级&为什么使用服务
服务: 长期后台运行的没有界面的组件
android应用:什么地方需要用到服务?
天气预报:后台的连接服务器的逻辑,每隔一段时间 获取最新的天气信息
股票显示:后台的连接服务器的逻辑,每隔一段时间 获取最新的股票信息
mp3播放器: 后台长期的播放音乐。
new Thread(){}.start(); 子线程没有界面,也是长期后台运行的。
android系统进程管理是按照一定的规则的:
1.应用程序一旦被打开 通常情况下关闭(清空任务栈)后进程不会停止。方面下一次快速启动。
带来内存不足的问题。
2.Android系统有一套 内存清理机制。 按照优先级去回收系统的内存。
进程分为5个等级的优先级:(从高到低)
1.Foreground process 前台进程 用户正在玩的应用程序对应的进程
2.Visible process 可视进程 用户仍然可以看到这个进程的界面。
3.Service process服务进程 应用程序有一个服务组件在后台运行。
4.Background process 后台进程 应用程序没有服务在运行 并且最小化 (activity onstop)
5.Empty process 空进程 没有任何运行的activity, 任务栈空了
长期后台运行的组件, 不要在activity开启子线程。
应该是创建服务,在服务里面开启子线程。
服务的目的:
1.长期后台运行。
2.提高进程的优先级,系统不容易回收掉进程,即便回收了,内存充足的时候,把进程重新创建。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.testservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.testservice.MainActivity"
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:name="com.itheima.testservice.MyService"></service>
</application>
</manifest>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:onClick="click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="开启服务" />
</RelativeLayout>
package com.itheima.testservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
//oncreate ondestory onstart onstop onresume onpause
//服务没有界面
@Override
public void onCreate() {
System.out.println("服务创建了");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("服务接收到了开启指令");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
System.out.println("服务销毁了");
super.onDestroy();
}
}
package com.itheima.testservice;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view){
Intent intent = new Intent(this,MyService.class);
startService(intent);
}
}