当前位置: 代码迷 >> Android >> .Net软件工程师玩转Android开发-(18)Android服务
  详细解决方案

.Net软件工程师玩转Android开发-(18)Android服务

热度:68   发布时间:2016-04-28 02:51:16.0
.Net程序员玩转Android开发---(18)Android服务

      在做.NET开发的时候,我们经常会写windows service服务,在后台运行。在android系统中,同样可以写android服务. Service是安卓四大组件一个非常重要的组件,

四大组件包括Activity, Service ,BroadCastReceive,Content Provicer, 前几节课中,我们一直讲解activity,这节我们看下怎样使用Service, 我们在一些后台操作的时候,会使用service,比如在后台不断获取当前地理位置信息。 并且service服务还可以与activity进行通信。

         Android中所有的服务都继承自Service,并且重载三个重要的方法

         onCreate:服务在第一次调用的时候执行,第二次如果服务没有终止,再次调用的时候不会执行该方法

         onStart:服务启动后,在该方法里面可以监听外部传入的一些信息

         onDestroy:服务终止时调用。

        我们看下面的例子

               

        1.创建服务

                    首先创建serviceone服务,继承Service,

                   

package com.example.helloword;import android.app.Service;import android.content.Intent;import android.os.Bundle;import android.os.IBinder;import android.util.Log;import android.widget.Toast;public class serviceone extends Service {	@Override	public IBinder onBind(Intent arg0) {		// TODO Auto-generated method stub		return null;	}	public serviceone()	{			}		@Override	public void onCreate() {		// TODO Auto-generated method stub		 Toast.makeText(this, "onCreate创建服务", Toast.LENGTH_SHORT).show();  		 Log.i("service", "onDestroy"); 	     	super.onCreate();	}	@Override	public void onDestroy() {		// TODO Auto-generated method stub		Toast.makeText(this, "服务停止", Toast.LENGTH_SHORT);  		super.onDestroy();	}	@Override	@Deprecated	public void onStart(Intent intent, int startId) {		// TODO Auto-generated method stub		Log.v("onstart", "接受消息");		Toast.makeText(this, "onStart接受消息", Toast.LENGTH_SHORT).show();		if (intent != null)		{			Bundle bundle = intent.getExtras();  						String para = bundle.getString("para");  						Toast.makeText(this, "接受参数"+para, Toast.LENGTH_SHORT).show();					}		super.onStart(intent, startId);	}    		}


            服务创建完成后,要在AndroidManifest.xml添加服务

              

 <service   android:name=".serviceone">     <intent-filter>          <action android:name="com.example.helloword.serviceone" />          <category android:name="android.intent.category.default" />    </intent-filter>  </service>  


         2.服务和Activity通信

                 上面的服务创建完成后,我们现在创建一个activity向服务发送信息,服务在onstart进行接收处理,首先创建布局文件

                  serviceactivity.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Button        android:id="@+id/btnstartservice"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="启动服务" />    <Button        android:id="@+id/btnstopservice"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="停止服务" /></LinearLayout>

                创建ServiceActivity,在启动服务的时候发送para参数
               
package com.example.helloword;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 ServiceActivity extends Activity {		private Button btnstart;//启动服务		private Button btnstop;//停止服务		Intent intent;		@Override	protected void onCreate(Bundle savedInstanceState) 	{		// TODO Auto-generated method stub		super.onCreate(savedInstanceState);		setContentView(R.layout.serviceactivity);		btnstart=(Button)findViewById(R.id.btnstartservice);		btnstop=(Button)findViewById(R.id.btnstopservice);				btnstart.setOnClickListener(new OnClickListener(){			@Override			public void onClick(View arg0) {				// TODO Auto-generated method stub				 intent = new Intent("com.example.helloword.serviceone");  				Bundle bundle = new Bundle();  				 bundle.putString("para","Hello World");  				  intent.putExtras(bundle);      			     startService(intent);      			}								});				btnstop.setOnClickListener(new OnClickListener(){			@Override			public void onClick(View arg0) {				// TODO Auto-generated method stub				stopService(intent);			}								});							}}



  相关解决方案