android 之 service(上)
注意service不是一个单独的进程,也不是一个线程。与程序在同一个进程中。
service需要在Manifest文件中进行注册
停止Service:-Context.stopService();
service的简单生命周期
在启动一个service时,先调用onCreate()方法再调用onStartCommand()方法,若此时再次启动service不会再调用onCreate(),而是调用onStartCommand()。在停止时调用onDestroy()方法。
下面是一个例子,可以清楚的看到service的生命周期:
================MainActivity.java=================
package com.yx.testservice1;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button startServiceButton;
private Button stopServiceButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startServiceButton = (Button) findViewById(R.id.startServiceButton);
stopServiceButton = (Button) findViewById(R.id.stopServiceButton);
startServiceButton.setOnClickListener(new StartServiceButtonLisener());
stopServiceButton.setOnClickListener(new StopServiceButtonLisener());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class StartServiceButtonLisener implements OnClickListener{
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, FirstService.class);
startService(intent);
}
}
class StopServiceButtonLisener implements OnClickListener{
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, FirstService.class);
stopService(intent);
}
}
}
===================FirstService.java===============
package com.yx.testservice1;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class FirstService extends Service {
@Override
public IBinder onBind(Intent arg0) {
System.out.println("onBind");
return null;
}
//当创建一个service中会先调用的,在此点击start后不会执行onCreate方法,直接执行onStartCommand
@Override
public void onCreate() {
System.out.println("onCreate");
super.onCreate();
}
@Override
public void onDestroy() {
System.out.println("onDestroy");
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//intent是从activity中传过来的
System.out.println("onStartCommand");
System.out.println("flags-->"+flags);
System.out.println("startId-->"+startId);
return super.onStartCommand(intent, flags, startId);
}
}
=================AndroidManifest.xml==============
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yx.testservice1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.yx.testservice1.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=".FirstService"></service>
</application>
</manifest>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。