android > Service
**什么是 Service,
Service 是 android 四大组件之一, 即android 系统的服务(不是一个线程而是 主程序的一部分) ,于 Activity 不同,它是不能与用户交互的,不能自己启动的, 需要调用 Context.startServece() 来启动 , (Context 类似 ThiActive.this) ,运行后台,如果我们推出应用 service 进程并没有结束, 它任然 在 后台 运行, 比如 我们播放音乐的时候, 可能想边听歌边干点其他的 就 需要这个东西 ,
** 启动和停止 Service
启动: startService(Intent intent) 这个时候 Service 会调用 自身的 onCreate 方法
停止: staopService(Intent intent) 来停止 servece, 这个时候会调用 onDestory() 方法
其中 intent = (ThisActivity.this, ServiceActive.class);
下面是一个例子, 先 是 Servers的 class ,这个的功能新建一个线程 然后 是每隔1秒钟执行一次 加 1 的操作
package udpreceive.com;import android.app.Activity;import android.app.Service;import android.content.Intent;import android.os.Bundle;import android.os.IBinder;import android.util.Log;public class UdpReceive extends Service{private boolean threadDisable;private int count;@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();System.out.println( "dd");new Thread(new Runnable() {public void run() {// TODO Auto-generated method stubwhile (!threadDisable) {try {Thread.sleep(1000);} catch (Exception e) {// TODO: handle exception}count++;System.out.println( "Count is " + count);}}}).start();} @Override public void onDestroy() { super.onDestroy(); this.threadDisable = true; Log.v("CountService", "on destroy"); } public int getCount() { return count; }}
然后是 主 activity 启动后 调用 servers
package remote.com;import java.io.BufferedInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectOutputStream;import java.io.Serializable;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.net.SocketException;import java.net.UnknownHostException;import java.nio.Buffer;import java.util.ArrayList;import java.util.List;import udpreceive.com.UdpReceive;import android.app.Activity;import android.content.Intent;import android.hardware.Camera.Size;import android.os.Bundle;import android.os.Handler;import android.os.HandlerThread;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class RemoteActivity extends Activity { /** Called when the activity is first created. */private Button start_button;private Button end_bu; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); start_button = (Button)findViewById(R.id.start_bu); end_bu = (Button)findViewById(R.id.end_bu); start_button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {// TODO Auto-generated method stubIntent intent = new Intent();intent.setClass(RemoteActivity.this, UdpReceive.class);startService(intent);System.out.println("a"); }}); end_bu.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {// TODO Auto-generated method stubRemoteActivity.this.stopService(new Intent(RemoteActivity.this,UdpReceive.class));System.out.println("b");}}); // } ////// }
最后是 manifest
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="remote.com" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".RemoteActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="udpreceive.com.UdpReceive" android:label="@string/app_name" > </activity> <service android:name="udpreceive.com.UdpReceive"></service> </application></manifest>
其中<service android:name="udpreceive.com.UdpReceive"></service> 这样定义因为 我的UdpReceive 在udpreceive.com 包里面 所以定义全包名,如果 和 主 activity 在一个包里 就写
<service android:name=".UdpReceive"></service> 就ok
更多相关文章
- Android(安卓)对话框(Dialog)大全 建立你自己的对话框
- Android调用WIFI设置
- Android(安卓)任务共用性Affinity
- Android(安卓)onActivityResult()不执行的几个原因
- Android小趣
- 关于android WebViewClient和WebChromeClient
- android:configChanges属性
- Android(安卓)UEventObserver
- Android摄像头--通过Intent启动