Service是什么?
Service是在一段不定的时间运行在后台,不会和用户产生交互,每个Service必须在 Manifest中 通过<Service>来声明,Service 不能自动运行 需要通过某一个Activity或者其他Context对象来调用, 我们可以通过contect.startsercice和contect.bindservierice来启动它。
Service 和其他的应用组件一样,运行在进程中的主线程中,这就是说如果Service需要很多耗时或者阻塞的操作,需要在子类中实现。
Service 需要在oncreate()调用startService方法中被调用,当执行stopService()停止服务,(无论执行多少次startService,一旦执行stopService方法则所有Service终止执行)。
Service不是什么?
1.Service 不是一个单独的进程,(打开Windos 任务管理器 的进程选项卡下,就是我们运行程序的进程)
2.Service 不是一个线程,(进程中包含多个线程吗,也就是说一个进程中或许有多个线程)
Service的特点:
1.没有图形化界面
2.通常处理耗时比较长的操作
3.不能自动运行
4.可以多次被调用
注意:Service 不是线程 也不是进程~
Service 的使用:
启动Service context.startService()
关闭Service context.stopService();
再次调用Service StartCommand();
我们编写一个例子来加以理解:
首先创建一个FirstService.java
package cn.bailu.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class FirstService extends Service {
public IBinder onBind(Intent arg0) {
return null;
}
public void onCreate() {//
System.out.println("Create 被启动了,让Service创建时");
super.onCreate();
}
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("StartCommand 启动Service 时启动的方法");
System.out.println(flags);
System.out.println(startId);
return super.onStartCommand(intent, flags, startId);
}
public void onDestroy() {
System.out.println("Destroy 被销毁了");
super.onDestroy();
}
}
创建一个Activity
TestActivity.java
package cn.bailu.service;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class TestActivity extends Activity {
private Button start_btn=null;
private Button stop_btn=null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showViews();
}
private void showViews() {
start_btn = (Button) findViewById(R.id.start_btn);
start_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(TestActivity.this, FirstService.class);
startService(intent);
System.out.println("Create success!!");
}
});
stop_btn = (Button) findViewById(R.id.stop_btn);
stop_btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(TestActivity.this, FirstService.class);
stopService(intent);
}
});
}
}
布局文件:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/start_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="StartService" />
<Button
android:id="@+id/stop_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="StopService" />
</LinearLayout>
manifest文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.bailu.service"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".TestActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".FirstService"></service>
</application>
</manifest>
需要注意的是:一定要将服务加入到 Manifest文件里:
<service android:name=".FirstService"></service>
如果提示程序异常终止的话,大部分原因是因为 我们的
<activity
android:label="@string/app_name"
android:name=".TestActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
没有配置好~
如有共同爱好Android的朋友们一起研究 共同探讨~ 留个QQ吧 909506260