android应用开发详解(十七)---------------service初级

1、工程目录


2、MainActivity.java

package com.example.test_service;

import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;

public class MainActivity extends Activity {
	private Button startBtn, stopBtn, bindBtn, unbindBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		startBtn = (Button) findViewById(R.id.button01);
		stopBtn = (Button) findViewById(R.id.button02);
		bindBtn = (Button) findViewById(R.id.button03);
		unbindBtn = (Button) findViewById(R.id.button04);
		startBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setAction("com.example.test_service.action.MY_SERVICE");
				startService(intent);
			}
		});
		stopBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setAction("com.example.test_service.action.MY_SERVICE");
				stopService(intent);
			}
		});
		final ServiceConnection conn = new ServiceConnection() {

			@Override
			public void onServiceDisconnected(ComponentName arg0) {
				// TODO Auto-generated method stub
				Log.i("SERVICE", "断开连接");
				Toast.makeText(MainActivity.this, "断开连接!", Toast.LENGTH_LONG)
						.show();
			}

			@Override
			public void onServiceConnected(ComponentName arg0, IBinder arg1) {
				// TODO Auto-generated method stub
				Log.i("SERVICE", "连接成功");
				Toast.makeText(MainActivity.this, "连接成功!", Toast.LENGTH_LONG)
						.show();
			}

		};
		bindBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setAction("com.example.test_service.action.MY_SERVICE");
				bindService(intent, conn, Service.BIND_AUTO_CREATE);
			}
		});
		unbindBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setAction("com.example.test_service.action.MY_SERVICE");
				unbindService(conn);
			}
		});

	}

}

3、MyService.java

package com.example.test_service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service{

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		Log.i("SERVICE", "on create.................");
		Toast.makeText(MyService.this,"on create .........." , Toast.LENGTH_LONG).show();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.i("SERVICE", "on destroy.................");
		Toast.makeText(MyService.this,"on destroy .........." , Toast.LENGTH_LONG).show();
	}


	@Override
	@Deprecated
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);
		Log.i("SERVICE", "on start.................");
		Toast.makeText(MyService.this,"on start .........." , Toast.LENGTH_LONG).show();
	}

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		Log.i("SERVICE", "on bind.................");
		Toast.makeText(MyService.this,"on bind .........." , Toast.LENGTH_LONG).show();
		return null;
	}

}


4、main.xml

<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/button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="启动Service" />

    <Button
        android:id="@+id/button02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="停止Service" />

    <Button
        android:id="@+id/button03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="绑定Service" />

    <Button
        android:id="@+id/button04"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="解绑Service" />

</LinearLayout>

5、Android Menifest.xml中添加Service的注册

 <service android:name="MyService" >
            <intent-filter>
                <action android:name="com.example.test_service.action.MY_SERVICE" />
            </intent-filter>
        </service>

6、打印输出


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值