Service是Android里面后台运行的一种方法.
因为涉及到后台进程, 这里面的事情, 肯定就不止这些了.
我在这里只简单的了解了一些Service 调用的过程.
其中后台服务的退出和释放, 里面水很深.. 以后有机会再慢慢研究吧..
上图:
代码:
在Service 在 XML中的注册方法:
<service android:name=".MyService">
<intent-filter>
<action android:name="MY_SERVICE" />
</intent-filter>
</service>
或者只接写成:
<service android:name=".MyService">
</service>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.youtwo.testservice">
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".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=".MyService">
<intent-filter>
<action android:name="MY_SERVICE" />
</intent-filter>
</service>
</application>
</manifest>
layout/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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Service"
android:id="@+id/button1"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stop Service"
android:id="@+id/button2"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="BIND SERVICE"
android:id="@+id/button3"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="UNBIND SERVICE"
android:id="@+id/button4"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="EXIT"
android:id="@+id/button5"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</RelativeLayout>
com/example/youtwo/testservice/MainActivity.java
package com.example.youtwo.testservice;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button btnStartMyService, btnStopMyService, btnBindMyService, btnUnbindMyService, btnExit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化按钮
btnStartMyService = (Button) this.findViewById(R.id.button1);
btnStartMyService.setOnClickListener(new ClickEvent());
btnStopMyService = (Button) this.findViewById(R.id.button2);
btnStopMyService.setOnClickListener(new ClickEvent());
btnBindMyService = (Button) this.findViewById(R.id.button3);
btnBindMyService.setOnClickListener(new ClickEvent());
btnUnbindMyService = (Button) this.findViewById(R.id.button4);
btnUnbindMyService.setOnClickListener(new ClickEvent());
btnExit = (Button) this.findViewById(R.id.button5);
btnExit.setOnClickListener(new ClickEvent());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("Activity", "onDestroy");
}
private ServiceConnection _connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
// TODO Auto-generated method stub
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
};
class ClickEvent implements View.OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent();
// 两种初始化方法
// 1. setAction() 对应XML中的 <action android:name="MY_SERVICE" />
//intent.setAction("MY_SERVICE");
// 2. setClass 对应XML中的 <service android:name=".MyService">
intent.setClass(MainActivity.this, MyService.class);
if (v == btnStartMyService) {
// 启动service
// 会调用MyService 的 onCreate() 和 onStartCommand() 方法
startService(intent);
} else if (v == btnStopMyService) {
// 停止service
stopService(intent);
} else if (v == btnBindMyService) {
// 绑定service
bindService(intent, _connection, Service.BIND_AUTO_CREATE);
} else if (v == btnUnbindMyService) {
// Service绑定了之后才能解绑
if (MyService.ServiceState == "onBind")
unbindService(_connection);
} else if (v == btnExit) {
// 退出时, 停止Service
if (MyService.ServiceState == "onBind")
unbindService(_connection);
stopService(intent);
finish();
}
}
}
}
com/example/youtwo/testservice/MyService.java:
package com.example.youtwo.testservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
/**
* Created by YouTwo on 2015/1/19.
*/
public class MyService extends Service {
static public String ServiceState="";
@Override
public IBinder onBind(Intent arg0) {
Log.e("MyService", "onBind");
ServiceState="onBind";
return null;
}
@Override
public boolean onUnbind(Intent intent){
super.onUnbind(intent);
Log.e("MyService", "onUnbind");
ServiceState="onUnbind";
return false;
}
@Override
public void onCreate() {
super.onCreate();
Log.e("MyService", "onCreate");
ServiceState="onCreate";
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.e("MyService", "onStartCommand");
ServiceState="onStartCommand";
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("MyService", "onDestroy");
ServiceState="onStart";
System.out.println("service stop");
}
}