Android开发——四大组件Service初体验
2016-12-20 22:48
405 查看
我们上一篇博客把Service的概念理解了一遍,这一篇博客我们就来试试它的简易用法吧。
同时通过这个案例我们来理解一下Service的生命周期和Service的两种启动方式。
activity_main.xml
<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="com.example.studyservice.MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="btn"
android:text="First" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="btn1"
android:text="绑定Secand" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="btn2"
android:text="解除Secand" />
</LinearLayout>
MainActivity.java
package com.example.studyservice;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends Activity {
private ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btn(View view) {
// TODO Auto-generated method stub
System.out.println(111+"11111111111111s");
Intent intent=new Intent(this,FirstService.class);
startService(intent);
}
public void btn1(View view) {
// TODO Auto-generated method stub
System.out.println(111+"11111111111111s");
Intent intent=new Intent(this,SecandSercive.class);
bindService(intent, connection, BIND_AUTO_CREATE);
}
public void btn2(View view) {
// TODO Auto-generated method stub
Intent intent=new Intent(this,SecandSercive.class);
unbindService(connection);
}
}
FirstService.java
package com.example.studyservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class FirstService extends Service{
private boolean flag = true;
private int count=1;
@Override
public void onCreate() {
// TODO Auto-generated method stub
System.out.println(222+"22222222222222222");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
new Thread(){
public void run() {
while (flag) {
System.out.println("count"+count);
count++;
if (count==100) {
flag=false;
onDestroy();
}
try {
sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}.start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
System.out.println("FirstService OnDestroy");
flag=false;
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
SecandService.java
package com.example.studyservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class SecandSercive extends Service{
private boolean flag=true;
private int count=0;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("SecandService create");
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
System.out.println("11111111111111111111111SecandService onUnbind");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
flag=false;
System.out.println("SecandService onDestroy");
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
System.out.println("SecandService onBind");
new Thread(){
public void run() {
while (flag) {
System.out.println("count"+count);
count++;
if (count==100) {
flag=false;
onDestroy();
}
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}.start();
return null;
}
}
同时通过这个案例我们来理解一下Service的生命周期和Service的两种启动方式。
activity_main.xml
<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="com.example.studyservice.MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="btn"
android:text="First" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="btn1"
android:text="绑定Secand" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="btn2"
android:text="解除Secand" />
</LinearLayout>
MainActivity.java
package com.example.studyservice;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends Activity {
private ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btn(View view) {
// TODO Auto-generated method stub
System.out.println(111+"11111111111111s");
Intent intent=new Intent(this,FirstService.class);
startService(intent);
}
public void btn1(View view) {
// TODO Auto-generated method stub
System.out.println(111+"11111111111111s");
Intent intent=new Intent(this,SecandSercive.class);
bindService(intent, connection, BIND_AUTO_CREATE);
}
public void btn2(View view) {
// TODO Auto-generated method stub
Intent intent=new Intent(this,SecandSercive.class);
unbindService(connection);
}
}
FirstService.java
package com.example.studyservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class FirstService extends Service{
private boolean flag = true;
private int count=1;
@Override
public void onCreate() {
// TODO Auto-generated method stub
System.out.println(222+"22222222222222222");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
new Thread(){
public void run() {
while (flag) {
System.out.println("count"+count);
count++;
if (count==100) {
flag=false;
onDestroy();
}
try {
sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}.start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
System.out.println("FirstService OnDestroy");
flag=false;
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
SecandService.java
package com.example.studyservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class SecandSercive extends Service{
private boolean flag=true;
private int count=0;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("SecandService create");
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
System.out.println("11111111111111111111111SecandService onUnbind");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
flag=false;
System.out.println("SecandService onDestroy");
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
System.out.println("SecandService onBind");
new Thread(){
public void run() {
while (flag) {
System.out.println("count"+count);
count++;
if (count==100) {
flag=false;
onDestroy();
}
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}.start();
return null;
}
}
相关文章推荐
- android开发步步为营之37:四大组件之Service(上)通过startService(intent)启动
- Android开发之四大组件——Service
- Android开发——四大组件之Service的探索
- Android开发四大组件之Service总结
- Android开发四大组件---Service详解
- Android开发四大组件---Service详解
- Android开发总结笔记 四大组件之Service(上) 1-2-4
- Android开发四大组件之Service(详解篇)
- Android开发四大组件之Service(具体解释篇)
- Android 开发之四大组件 --- Service
- Android开发四大组件---Service详解
- Android开发四大组件之Service(详解篇)
- Android开发四大组件---Service详解
- Android App开发基础篇—四大组件之Service
- Android开发四大组件之Service(实例篇)
- Android开发总结笔记 四大组件之Service(下) 1-2-6
- Android四大组件之服务-Service 原理和应用开发详解
- Android开发总结笔记 四大组件之Service(中) 1-2-5
- android开发步步为营之36:四大组件之Service(中)通过bindService启动
- 【Android开发日记】之入门篇(五)——Android四大组件之Service