不绑定活动的service

来源:互联网 发布:网络销售特点 编辑:程序博客网 时间:2024/06/05 00:41

通过该方法启动的service,访问者与service之间没有联系,即使访问者退出了,service也会运行。一般使用在需要在后台长期进行某项活动,应为跟访问者之间不联系,所以在消息通知等功能中使用。

该方法启动服务的方式很简单,直接startService()就可以了。


活动:

public class TestService extends Activity implements OnClickListener {private Button start;private Button stop;private Intent intent;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.acy_testservice);initView();}public void initView() {start = (Button) findViewById(R.id.btn_start);stop = (Button) findViewById(R.id.btn_stop);start.setOnClickListener(this);stop.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubintent=new Intent();intent.setAction("service.testmyservice");if (v == start) {startService(intent);} else if (v == stop) {stopService(intent);}}}
布局:

<?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/btn_start"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="启动service" />    <Button        android:id="@+id/btn_stop"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="停止service" /></LinearLayout>


自定义服务:

public class MyService extends Service {private Thread t;private boolean isRun = false;// 必须实现的方法,返回一个IBinder对象,应用程序通过该对象与service组件通信。@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}// 第一次被创建的时候,回调该方法@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();}// startService()启动的时候,回调该方法@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// TODO Auto-generated method stubmyThread thread = new myThread();t = new Thread(thread);t.start();return super.onStartCommand(intent, flags, startId);}// 当绑定的所有客户端都断开连接的时候,回调该方法@Overridepublic boolean onUnbind(Intent intent) {// TODO Auto-generated method stubreturn super.onUnbind(intent);}// 关闭之前,回调该方法@Overridepublic void onDestroy() {// TODO Auto-generated method stubt.interrupted();Log.i("onDestroy", "******");isRun = true;super.onDestroy();}// 新建线程类,在里面进行耗时操作class myThread implements Runnable {@Overridepublic void run() {// TODO Auto-generated method stubwhile (!isRun) {Log.i("(^~^)", "show run!");try {Thread.sleep(2000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}

配置清单:

     <service            android:name="com.example.mytestandroid.MyService"            android:label="@string/app_name" >            <intent-filter>                <action android:name="service.testmyservice" />            </intent-filter>        </service>

这样的话,大概就差不多了!

注意:

        1.停止服务的话。服务虽然停止了,但循环的线程是没有停止的,所以要停止线程。这里是使用的共享变量的方法,通过改变共享变量的方法,停止循环的线程。





0 0
原创粉丝点击