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










posted @ 2014-04-30 16:39  freedragon  阅读(276)  评论(0编辑  收藏  举报