zoukankan      html  css  js  c++  java
  • android 中service的简单事例


    源码


    public class ServiceDemoActivity extends Activity {
    	private static final String TAG = "ServiceDemoActivity";
    	
    	Button bindBtn;
    	Button startBtn;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            bindBtn = (Button)findViewById(R.id.bindBtn);
            startBtn = (Button)findViewById(R.id.startBtn);
            
            bindBtn.setOnClickListener(new OnClickListener() {
    			public void onClick(View v) {
    			Log.e(TAG, "bindBtn");
    				bindService(new Intent(ServiceDemo.ACTION), conn, BIND_AUTO_CREATE);
    			}
    		});
            
            startBtn.setOnClickListener(new OnClickListener() {
    			public void onClick(View v) {
    			Log.e(TAG, "startBtn");
    				startService(new Intent(ServiceDemo.ACTION));
    			}
    		});
        }
        
        ServiceConnection conn = new ServiceConnection() {
        	public void onServiceConnected(ComponentName name, IBinder service) {
    			Log.e(TAG, "onServiceConnected");
    		}
    		public void onServiceDisconnected(ComponentName name) {
    			Log.e(TAG, "onServiceDisconnected");
    		}
    	};
    	@Override
    	protected void onDestroy() {
    		Log.e(TAG, "onDestroy unbindService");
    		unbindService(conn);
    		super.onDestroy();
    	};
    }

    <?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/bindBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="绑定服务" />
    
        <Button
            android:id="@+id/startBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="启动服务" />
    
    </LinearLayout>
    


    public class ServiceDemo extends Service {
    	private static final String TAG = "ServiceDemo";
    	public static final String ACTION = "com.example.servicedemoactivity.ServiceDemo";
    	
    
    	@Override
    	public IBinder onBind(Intent arg0) {
    		// TODO Auto-generated method stub
    		Log.e(TAG, " onBind ");
    		return null;
    	}
    	
    	@Override
    	public void onCreate() {
    		// TODO Auto-generated method stub
    		Log.e(TAG, " onCreate ");
    		super.onCreate();
    	}
    	
    	@Override
    	public void onStart(Intent intent, int startId) {
    		// TODO Auto-generated method stub
    		Log.e(TAG, " 1onStart ");
    		super.onStart(intent, startId);
    	}
    	
    	@Override
    	public int onStartCommand(Intent intent, int flags, int startId) {
    		// TODO Auto-generated method stub
    		Log.e(TAG, " onStartCommand ");
    		return super.onStartCommand(intent, flags, startId);
    	}
    
    }
    

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.servicedemoactivity"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="18" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            
            <activity android:name=".ServiceDemoActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>
            </activity>
            
            <service android:name="com.example.servicedemoactivity.ServiceDemo">
                <intent-filter >
                    <action android:name="com.example.servicedemoactivity.ServiceDemo"/>
                </intent-filter>
            </service>
            
        </application>
    
    </manifest>
    





    参考  http://aswang.iteye.com/blog/1424309










  • 相关阅读:
    readystatechange事件
    DOMContentLoaded事件
    beforeunload事件
    jieba
    模型评估
    机器学习术语
    决策树
    kafka
    即时通讯好文
    HTTP头的Expires与Cache-control
  • 原文地址:https://www.cnblogs.com/flyingsir/p/3983695.html
Copyright © 2011-2022 走看看