功能描述:
点击'启动service’按钮后启动后台线程,点击“停止service”按钮后关闭线程。后台线程每一秒产生一个随机数,并通过Handler将产生的随机数显示在用户界面上。
activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="vertical"> <TextView android:id="@+id/tv01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" " android:textSize="30sp"/> <Button android:id="@+id/btnstart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动service"/> <Button android:id="@+id/btnstop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止service"/> </LinearLayout>
MainActivity.java文件
package com.example.pprp.myapplication2_lh; import android.app.Activity; import android.app.Service; import android.content.Intent; import android.os.Handler; import android.os.IBinder; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private static TextView tv01=null; Button btnstart; Button btnstop; private static Handler handler=new Handler(); private static double randomnum; public static void UpdateGUI(double refreshDouble){ randomnum=refreshDouble; handler.post(RefreshLable); } private static Runnable RefreshLable=new Runnable() { @Override public void run() { tv01.setText(String.valueOf(randomnum)); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv01=(TextView)findViewById(R.id.tv01); btnstart=(Button)findViewById(R.id.btnstart); btnstop=(Button)findViewById(R.id.btnstop); final Intent serviceintent=new Intent(this,ThreadActivity.class); //连接两个java文件的媒介 btnstart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startService(serviceintent); //开始线程 } }); btnstop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { stopService(serviceintent); //停止线程 } }); } }
ThreadActivity .java文件
package com.example.pprp.myapplication2_lh; import android.app.Activity; import android.app.Service; import android.content.Intent; import android.os.Handler; import android.os.IBinder; import android.support.annotation.Nullable; import android.widget.TextView; /** * Created by lh on 2018/11/21. */ public class ThreadActivity extends Service{ private Thread thread; @Override public void onCreate() { super.onCreate(); thread=new Thread(null,backgroundwork,"thread"); //创建线程,将Runnable对象作为参数传递给thread,第三个参数是线程的名称
} @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); if(!thread.isAlive()){ thread.start(); } } @Override public void onDestroy() { super.onDestroy(); thread.interrupt(); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } private Runnable backgroundwork=new Runnable() { //在run()中放置代码的主体部分 @Override public void run() { try { while(!thread.interrupted()){ double randomnum=Math.random(); MainActivity.UpdateGUI(randomnum); Thread.sleep(1000); } }catch (InterruptedException e){ e.printStackTrace(); } } }; }
AndroidManifest.xml文件:新建立的threadactivity.java文件是service类型,所以在AndroidManifest.xml中注册时需注意写成service.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.pprp.myapplication2_lh"> <application android: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"> <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=".ThreadActivity"> </service> </application> </manifest>