码迷,mamicode.com
首页 > 其他好文 > 详细

Service简单实例

时间:2015-04-09 23:18:05      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

1. 继承service类,重写方法service的 onBinder() 方法

2.在 Manifest中注册 service,设置过滤的url ( public static final String ACTION = "com.lql.service.ServiceDemo"; //自定义)

3. 启动service   startservice(new Intent(ServiceDemo.ACITION));

  <service android:name="com.example.androidtest.ServiceDemo" >
            <intent-filter>
                <action android:name="com.lql.service.ServiceDemo" />
            </intent-filter>
 </service>

4. 总结:service的生命周期

第一次启动:

04-09 21:46:09.637: I/ServiceDemo(9280): onCreate
04-09 21:46:09.637: I/ServiceDemo(9280): onStartCommand
04-09 21:46:09.637: I/ServiceDemo(9280): onStart

 

第二次启动:

04-09 21:46:49.369: I/ServiceDemo(9413): onStartCommand
04-09 21:46:49.369: I/ServiceDemo(9413): onStart

 

 

 

 

 

ServiceDemo.java

public class ServiceDemo extends Service {
	 private static final String TAG = "ServiceDemo" ;  
	 public static final String ACTION = "com.lql.service.ServiceDemo";  

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		Log.i(TAG, "onCreate");
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		Log.i(TAG, "onDestroy");
		super.onDestroy();
	}

	@Override
	@Deprecated
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		Log.i(TAG, "onStart");
		super.onStart(intent, startId);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		Log.i(TAG, "onStartCommand");
		return super.onStartCommand(intent, flags, startId);
	}
	
	
     
}

 

StartService.java

public class StartService extends Activity {
  
	Button start_service;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_start_service);
		start_service = (Button) findViewById(R.id.start_service);
		start_service.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				startService(new Intent(ServiceDemo.ACTION));
			}
		});
	}

	
}

 

 

start_service_activity.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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".StartService" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/start_service"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="110dp"
        android:text="开启服务" />

</RelativeLayout>

 

Service简单实例

标签:

原文地址:http://www.cnblogs.com/childhooding/p/4412328.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!