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>
    
  • 相关阅读:
    代理类和装饰类的区别
    spring mvc 处理映射的几种方式
    如何深入浅出的理解跳转方式:重定向和请求转发
    springMVC拦截配置
    ※版本管理※=>☆SVN工具=>※解决地域麻烦※№→搭建自己的网络SVN (SourceForge 免费) [转]
    权力社会? 金钱社会? 透过现象看本质-让权力和金钱的力量沿着制度的河道流淌,才是社会稳定的基石
    自己封装的CMusic类 【转】
    VC++中MCI播放音频文件 【转】
    DevExpress.XtraGrid 【转】
    C# Process.Start()方法详解 [转]
  • 原文地址:https://www.cnblogs.com/childhooding/p/4412328.html
Copyright © 2011-2022 走看看