您的位置:首页 > 移动开发 > Android开发

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: