近期学习到了Activity与Service进行交互的过程,在此记录。本次使用的Activity和Service交互的方式是通过广播。
下面是完成后的效果:
完成效果图
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
tools:context="linchange.com.activityservice.MainActivity">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="下载"/>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:gravity="center"
android:textSize="18sp"
android:text="0%" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="40dp"
android:max="100"/>
</LinearLayout>
MainActivity.java
package linchange.com.activityservice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button button; //按钮
private TextView textView; //进度提示文字
private ProgressBar progressBar; //进度条
private int count = 0; //计数器
private Intent serviceIntent; //服务意图
private CounterReceiver counterReceiver; //广播接收者
private IntentFilter intentFilter; //意图过滤器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button); //绑定按钮
textView = (TextView) findViewById(R.id.textView); //进度提示文字
progressBar = (ProgressBar) findViewById(R.id.progressBar); //绑定进度条
//初始化服务意图
serviceIntent = new Intent(MainActivity.this, CounterService.class);
counterReceiver = new CounterReceiver(); //初始化次数接收者
intentFilter = new IntentFilter(); //初始化意图过滤器
intentFilter.addAction(CounterService.ACTION_NAME); //添加动作
registerReceiver(counterReceiver, intentFilter); //注册广播
button.setOnClickListener(new View.OnClickListener() { //设置点击事件
@Override
public void onClick(View v) {
serviceIntent.putExtra(CounterService.COUNTER, count); //存放数据到intent
startService(serviceIntent); //启动服务
}
});
}
//次数接收者
class CounterReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, final Intent intent) {
runOnUiThread(new Runnable() { //运行在主线程
@Override
public void run() {
//从intent中获取计数次数
count = intent.getIntExtra(CounterService.COUNTER, 2000);
textView.setText(String.valueOf(count + "%"));
//设置进度条的进度
progressBar.setProgress(count);
}
});
}
}
@Override
protected void onDestroy() {
stopService(serviceIntent); //停止服务
unregisterReceiver(counterReceiver); //解除注册次数接收者
super.onDestroy();
}
}
CounterService.java
package linchange.com.activityservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
/**
* Created by lkmc2 on 2018/1/11.
* 计数服务
*/
public class CounterService extends Service {
//计数器名常量
public static final String COUNTER = "counter";
//动作名
public static final String ACTION_NAME = "linchange.com.activityservice.COUNTER_ACTION";
private static final int MAX_VALUE = 100; //最大值
public int count; //计数变量
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//从intent中获取点击次数
count = intent.getIntExtra(COUNTER, 1000);
final Intent countIntent = new Intent(); //计数意图
countIntent.setAction(ACTION_NAME); //设置意图的动作
new Thread() {
@Override
public void run() {
try {
while (count < MAX_VALUE) { //当前次数小于最大次数
count++; //次数加一
countIntent.putExtra(COUNTER, count); //设置数据
sendBroadcast(countIntent); //发送广播
Thread.sleep(200);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
return START_STICKY; //服务被关闭后会再启动,若是没有新的intent,则参数为null
}
}
最后记得在AndroidManifest.xml对Service进行注册:
<application>
<service android:name=".CounterService" />
</application>
网友评论