android Service类简介

    Service类与 Activity类的原理类似,只是Service类并不像Activity一样提供与用户交互的表示层。Service是一种后台运行的Android组件,位于android.app包下。

    当应用程序不需要在前台显示某些界面时,可以使用Service来完成,比如后台的数据计算,后台音乐播放等。

    这个小例子是同时介绍Service组件与BroadcastReceiver组件联合使用的方法,在例子中使用java代码动态注册 BroadcastReceiver 并且通过前台的用户界面控制后台服务的进行。

  运行如下

android Service类简介_第1张图片

android Service类简介_第2张图片

android Service类简介_第3张图片

    strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">请启动 Service</string>
    <string name="app_name">xh_test_03</string>
    <string name="myButton1">启动Service</string>
    <string name="myButton2">停止Service</string>
</resources>


  布局文件main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    >
<TextView  
	android:id="@+id/myTextView"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="20px"
    android:gravity="center_horizontal"
    android:text="@string/hello"
    />
<Button
	android:id="@+id/myButton1"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="@string/myButton1"/>
<Button
	android:id="@+id/myButton2"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="@string/myButton2"/>
</LinearLayout>

   Service类 MyService
package xiaohang.zhimeng;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class MyService extends Service {
	MyThread myThread;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onDestroy() {
		myThread.flag = false;
		super.onDestroy();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		myThread = new MyThread();
		myThread.start();
		super.onStart(intent, startId);
	}

	class MyThread extends Thread {
		boolean flag = true;
		int c = 0;

		@Override
		public void run() {
			while (flag) {
				Intent i = new Intent("xiaohang.zhimeng.myThread");
				i.putExtra("myThread", c);
				sendBroadcast(i);
				c++;
				try {
					// 睡眠指定毫秒数
					Thread.sleep(1000);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	};
}



Activity类 Activity01
package xiaohang.zhimeng;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Activity01 extends Activity {
	Button button1;
	Button button2;
	TextView myTextView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		button1 = (Button) this.findViewById(R.id.myButton1);
		button2 = (Button) this.findViewById(R.id.myButton2);
		myTextView = (TextView) this.findViewById(R.id.myTextView);

		button1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(Activity01.this, MyService.class);
				startService(i);
				Toast.makeText(Activity01.this, "Service启动成功",
						Toast.LENGTH_SHORT).show();
			}
		});
		button2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// 创建Intent
				Intent i = new Intent(Activity01.this, MyService.class);
				stopService(i);
				Toast.makeText(Activity01.this, "Service停止成功",
						Toast.LENGTH_SHORT).show();
			}
		});
		// 创建IntentFilter过滤器
		IntentFilter intentFilter = new IntentFilter(
				"xiaohang.zhimeng.myThread");
		MyBroadcasReceiver myBroadcasReceiver = new MyBroadcasReceiver();
		registerReceiver(myBroadcasReceiver, intentFilter);
	}

	public class MyBroadcasReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {

			// 获得Intent中的数据
			Bundle myBundle = intent.getExtras();
			int myInt = myBundle.getInt("myThread");
			if (myInt < 10) {
				myTextView.setText("后台Service运行了" + myInt + "秒");
			} else {
				Intent i = new Intent(Activity01.this, MyService.class);
				// 停止服务
				stopService(i);
				myTextView.setText("后台Service在" + myInt + "秒后停止运行");
			}
		}
	}
}

 
  最后在若弱的 提醒大家一句 服务需要在 AndroidManifest.xml中声明一下

android Service类简介_第4张图片

源码见附件

你可能感兴趣的:(xml,android,OS,ITeye,音乐)