界面如图:依次点击按钮,在debug模式下启动,通过Android studio的console后台,可以看到system打印的 信息
先点start new activity-->startService-->sendBroadCast-->sendBroadCast2-->stopService
运行日志为:
I/System.out: NewActivity已经启动
I/System.out: 接收到数据:Hello from main activity.
I/System.out: onStartClicked
I/System.out: MyService:onCreate,服务启动
I/System.out: MyService:onStartCommand,service每次都会调用的服务开始命令
V/AudioManager: playSoundEffect effectType: 0
I/System.out: onSendClicked,系统调用sendBroadcast()方法,显式发消息
I/System.out: onReceive,接收到广播发来的消息
I/System.out: onSendClicked2,系统调用sendBroadcast()方法,隐式发消息
I/zygote64: Compiler allocated 6MB to compile void android.view.ViewRootImpl.performTraversals()
I/zygote64: Do full code cache collection, code=125KB, data=108KB
I/zygote64: After code cache collection, code=113KB, data=75KB
V/AudioManager: playSoundEffect effectType: 0
I/System.out: onStartClicked
I/System.out: MyService:onDestroy,服务销毁
mainActivity:
package com.lmj.bluetoothchat;
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 onBtnClicked(View v){
System.out.println("onBtnClicked");
// 启动一个新的画面Activity
// Intent是一个消息对象,封装有动作和数据
// 显式调用NewActivity
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("msg", "Hello from main activity.");
startActivity(intent);
}
public void onStartClicked(View v){
System.out.println("onStartClicked");
// 启动后台服务
// 服务长期后台运行,除非,用户显式调用stopService来停止服务
startService(new Intent(this, MyService.class));
}
public void onStopClicked(View v){
System.out.println("onStartClicked");
stopService(new Intent(this, MyService.class));
}
public void onSendClicked(View v){
System.out.println("onSendClicked,系统调用sendBroadcast()方法,显式发消息");
// 通过sendBroadCast发送一个广播
// 显式发送
sendBroadcast(new Intent(this, MyReceiver.class));
}
public void onSendClicked2(View v){
System.out.println("onSendClicked2,系统调用sendBroadcast()方法,隐式发消息");
// 通过sendBroadCast发送一个广播
// 隐式广播需要指定一个匹配Action字符串
// 暗号
sendBroadcast(new Intent("com.example.activitytransferdemo.ACTION"));
}
}
MyService服务类:
package com.lmj.bluetoothchat;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
// 服务一定要注册到Manifest中去
public class MyService extends Service {
private InnerReceiver receiver;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("MyService:onCreate,服务启动");
// 动态注册广播接收器
// 可用于在不同组件之间进行异步通信
receiver = new InnerReceiver();
// 意图过滤(消息过滤器),匹配隐式消息
IntentFilter filter = new IntentFilter("com.lmj.bluetoothchat.ACTION");
registerReceiver(receiver, filter);
}
class InnerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
System.out.println("InnerReceiver:onReceive,接收到广播发来的消息");
}
}
// 服务只要启动一次,不管用户调用多少次startService,都只要调用一次onCreate创建一个服务对象
// 但是,每次调用 startService时,都会调用其onStartCommand方法
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
System.out.println("MyService:onStartCommand,service每次都会调用的服务开始命令");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
System.out.println("MyService:onDestroy,服务销毁");
super.onDestroy();
unregisterReceiver(receiver);
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
MyReceiver 广播类(处理广播的接收):
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
System.out.println("onReceive,接收到广播发来的消息");
}
}
第二页面测试类NewActivity:普及一下知识:
android有
四大组件:Activity、BroadcastReceive、Service、Content Provider ;
五大存储: SharedPreferences、SDCard 、SQLite数据库存储数据、使用ContentProvider存储数据、IO存储 ;
六大布局 :LineartLayout 、FrameLayout 、TableLayout 、 RelativeLayout 、 AbsoluteLayout 、 GridLayout ;
七大生命周期 :onCrate 不可见不可交互、 onStart 可见不可交互 、 onResume 可见可交互 、 onPause 可见不可交互 、onStop 不可见不可交互 、 onDestory 销毁了 、 onRestart 从不可见到可见 ;
八大基本数据类型 : short 短整型 、 int 整型 、 long 长整型 、 float 浮点型 、 char 字符型 、 boolean 布尔型 、 byte 字节型
package com.lmj.bluetoothchat;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
// Android中的四大组件都需要注册到Manifest文件中
public class NewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// 设置屏幕上显示的内容
setContentView(R.layout.activity_seconde);
System.out.println("NewActivity已经启动");
// 获得当前Actiivty启动的Intent对象
Intent intent = getIntent();
String msg = intent.getStringExtra("msg");
System.out.println("接收到数据:" + msg);
}
}
布局文件activity_main.xml:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:onClick="onBtnClicked"
android:text="Start New Activity" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:onClick="onStartClicked"
android:text="StartService" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true"
android:onClick="onStopClicked"
android:text="StopService" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button3"
android:layout_centerHorizontal="true"
android:layout_marginTop="75dp"
android:onClick="onSendClicked"
android:text="Send BroadCast" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="35dp"
android:onClick="onSendClicked2"
android:text="SendBroadcast2" />
</RelativeLayout>