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










  • 相关阅读:
    Huffman树与编码
    Python引用复制,参数传递,弱引用与垃圾回收
    Git使用说明
    numpy使用指南
    Python Socket
    温故知新之 数据库的事务、隔离级别、锁
    Oracle数据库的语句级读一致性
    VirtualBox NAT方式与主机互相通信
    Linux的定时任务
    Redis学习
  • 原文地址:https://www.cnblogs.com/flyingsir/p/3983695.html
Copyright © 2011-2022 走看看