700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Android复习10【Service与Thread的区别 Service的生命周期 Service生命周期解析(相

Android复习10【Service与Thread的区别 Service的生命周期 Service生命周期解析(相

时间:2019-04-22 02:04:32

相关推荐

Android复习10【Service与Thread的区别 Service的生命周期 Service生命周期解析(相

音乐播放器Android代码下载:/ifqzihaxvij

目 录

Service与Thread的区别

Service的生命周期

Service生命周期解析(相关方法详解、启动方式的不同、绑定)

course1001

运行截图

AndroidManifest.xml

MainActivity.java

音乐播放器(course0902-wzg)

-04-21-星期二【第10周】

菜鸟教程---4.2.1 Service初涉【/w3cnote/android-tutorial-intro.html】

老师讲了很多这篇文章。

Service与Thread的区别

Service的生命周期

Service生命周期解析(相关方法详解、启动方式的不同、绑定)

course1001

运行截图

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="/apk/res/android"package="cn.wangzg.course1001"><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><serviceandroid:name=".MyService"android:enabled="true"android:exported="true"></service><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

MainActivity.java

package cn.wangzg.course1001;import androidx.appcompat.app.AppCompatActivity;import ponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.util.Log;import android.view.View;public class MainActivity extends AppCompatActivity {private static final String TAG = "MainActivity";private Intent intent;private MyService.MyBinder binder;//匿名内部类private ServiceConnection conn = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {Log.d(TAG, "onServiceConnected: ");binder = (MyService.MyBinder) service;}@Overridepublic void onServiceDisconnected(ComponentName name) {Log.d(TAG, "onServiceDisconnected: ");binder = null;}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);intent = new Intent(this, MyService.class);}//Ctrl+alt+fpublic void btnStart(View view) {startService(intent);}public void btnStop(View view) {//Intent intent=new Intent(this,MyService.class);stopService(intent);}public void btnBind(View view) {bindService(intent, conn, BIND_AUTO_CREATE);binder.getAaa();}public void btnUnBind(View view) {unbindService(conn);}}

Service.java

package cn.wangzg.course1001;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.util.Log;public class MyService extends Service {private static final String TAG = "MyService";private String aaa;public MyService() {aaa = "hello Service!";}public class MyBinder extends Binder {public String getAaa() {return aaa;}}//返回的是一个Binder对象,作用:用来操作Service@Overridepublic IBinder onBind(Intent intent) {Log.d(TAG, "onBind: ");return new MyBinder();}@Overridepublic boolean onUnbind(Intent intent) {Log.d(TAG, "onUnbind: ");return super.onUnbind(intent);}@Overridepublic void onCreate() {super.onCreate();Log.d(TAG, "onCreate: ");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.d(TAG, "onStartCommand: ");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {Log.d(TAG, "onDestroy: ");super.onDestroy();}}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="16dp"android:layout_marginTop="16dp"android:onClick="btnStart"android:text="startService"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="16dp"android:onClick="btnStop"android:text="StopService"app:layout_constraintBaseline_toBaselineOf="@+id/button"app:layout_constraintStart_toEndOf="@+id/button" /><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="16dp"android:layout_marginTop="16dp"android:onClick="btnBind"android:text="bind"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/button" /><Buttonandroid:id="@+id/button4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="16dp"android:onClick="btnUnBind"android:text="unbind"app:layout_constraintBaseline_toBaselineOf="@+id/button3"app:layout_constraintStart_toEndOf="@+id/button3" /></androidx.constraintlayout.widget.ConstraintLayout>

音乐播放器(course0902-wzg)

Android复习10【Service与Thread的区别 Service的生命周期 Service生命周期解析(相关方法详解 启动方式的不同 绑定) 音乐播放器+服务】

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。