您的位置:首页 > 移动开发 > Android开发

Android Service 生命周期

2016-06-13 23:54 357 查看




















package com.example.metrox.l14;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection {

Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent(MainActivity.this,MyService.class);
//        findViewById(R.id.btnStartService).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                 startService(intent);
//            }
//        });

//        findViewById(R.id.btnStopService).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                 stopService(intent);
//                 System.out.println("服务已停止!!!");
//            }
//        });
findViewById(R.id.btnStartService).setOnClickListener(this);
findViewById(R.id.btnStopService).setOnClickListener(this);
findViewById(R.id.btnBindService).setOnClickListener(this);
findViewById(R.id.btnUnBindService).setOnClickListener(this);
}

@Override
public void onClick(View view) {
switch (view.getId()){
case  R.id.btnStartService:
startService(intent);
break;
case  R.id.btnStopService:
stopService(intent);
System.out.println("服务已停止!!!");
break;
case  R.id.btnBindService:
bindService(intent,this,BIND_AUTO_CREATE);
break;
case  R.id.btnUnBindService:
unbindService(this);
break;
}
}

@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
System.out.println("服务已连接...");
}

@Override
public void onServiceDisconnected(ComponentName componentName) {
System.out.println("服务已断开...");
}
}


package com.example.metrox.l14;

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

public class MyService extends Service {

private boolean isRuning = false;
public MyService() {
}

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("OnStartCommand");
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onCreate() {
super.onCreate();
System.out.println("Service OnCreate");
isRuning = true;
new Thread(){
@Override
public void run() {
super.run();
while (isRuning){
System.out.println("服务正在运行中...");
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}}
}
}.start();
}

@Override
public void onDestroy() {
super.onDestroy();
System.out.println("Service Destroy");
isRuning = false;
}
}


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.metrox.l14.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动服务"
android:id="@+id/btnStartService"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止服务"
android:id="@+id/btnStopService"
android:layout_below="@+id/btnStartService"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="54dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绑定服务"
android:id="@+id/btnBindService"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解绑服务"
android:id="@+id/btnUnBindService"
android:layout_below="@+id/btnBindService"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="70dp" />
</RelativeLayout>


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.metrox.l14">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service
android:name=".MyService"
android:enabled="true"
android:exported="true"></service>
</application>

</manifest>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: