本来打算周更的。。过年那段时间尽情的放纵去了。
我作为一个菜鸟,我来介绍一点关于Service的用法。
Service 是 Android 四大基本组件之一,是无界面的应用程序,可以长期在后台运行,在实际工作中非常重要。比如我们在听歌的时候突然来一条信息,那么当我点开信息的时候,我的音乐也要运行,这个时候就要服务了。
首先我介绍如何启动服务。
先创建一个Activity.我这里就写MainActivyt。创建完成之后在创建一个服务,
创建完成之后,我们先将主布局改为LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.liqipeter.startservice.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvout"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/etData"
/>
<Button
android:text="启动服务"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnstartservice" />
<Button
android:text="停止服务"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnstopservice" />
<Button
android:text="绑定服务"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnbingdservice" />
<Button
android:text="解除绑定"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnendservice" />
<Button
android:text="同步数据"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/updata" />
</LinearLayout>然后在Activity中初始化控件,并且添加点击事件
findViewById(R.id.btnstartservice).setOnClickListener(this);
findViewById(R.id.btnstopservice).setOnClickListener(this);
findViewById(R.id.btnbingdservice).setOnClickListener(this);
findViewById(R.id.btnendservice).setOnClickListener(this);
findViewById(R.id.updata).setOnClickListener(this);然后在点击事件中开启服务
switch (view.getId()){
case R.id.btnstartservice:
Intent i = new Intent(MainActivity.this,MyService.class);
i.putExtra("data",etData.getText().toString());
startService(i);
break;
case R.id.btnstopservice:
stopService(new Intent(MainActivity.this,MyService.class));
break;
case R.id.btnbingdservice:
bindService(new Intent(this,MyService.class),this, Context.BIND_AUTO_CREATE);
break;
case R.id.btnendservice:
unbindService(this);
break;
case R.id.updata:
if (binder!=null){
binder.setData(etData.getText().toString());
}
break;
}然后在Service中写一个方法 running = true;
new Thread(){
@Override
public void run() {
super.run();
int i = 0;
while (running){
i++;
String str =i+":"+data;
System.out.println(str);
if (callback!=null){
callback.onDataChange(str);
}
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
当我们点击绑定服务的时候,我们会将data值传到服务中,服务中接收到之后,把他显示在TextView上。但是不能直接显示,因为不能在子线程更新UI,所以我们要在写一个消息机制。
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
tvout.setText(msg.getData().getString("data"));
}
};
这是效果图:
下面我附上我的GitHub地址源代码在这里:https://github.com/FoxconnPeter/StartService
(ps:本人现在想找份工作,在深圳。至于薪资我目前在深圳工厂做Android,薪资5000左右。我想学更多东西和提升自己的价值 邮箱:597058061@qq.com)
8083

被折叠的 条评论
为什么被折叠?



