zoukankan      html  css  js  c++  java
  • Service简单实例

    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>
    
  • 相关阅读:
    Delphi中SQL语句配置参数代码示例
    Delphi中treeview的使用部分
    Delphi listview使用部分总结代码
    如何用Delphi编写自己的可视化控件
    关于treeview节点图标的帖子
    Delphi调用存储过程
    DELPHI的开源控件集(转自http://xieyunc.blog.163.com/)
    雨巷(A Lane in the Rain)
    五一过去了,新的开始
    好好的学习,做个有本事的人:),好好的玩,做个快乐的人!
  • 原文地址:https://www.cnblogs.com/childhooding/p/4412328.html
Copyright © 2011-2022 走看看