认识 Android(安卓)Service
16lz
2021-01-26
Service 是 Android 四大基本组件之一,是无界面的应用程序,可以长期在后台运行,在实际工作中非常重要,比如接收推送消息、在锁屏状态下侦听传感器信息
第一课时:
使用 Service本课讲解如何使用 startService 启动服务和如何使用 stopService 停止服务,以及在过程中需要注意的问题。
MyService extends Service:
package com.example.learnservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread(){
@Override
public void run() {
super.run();
while(true){
System.out.println("服务正在进行...");
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
return super.onStartCommand(intent, flags, startId);
}
}
MainActivity extends Activity :
package com.example.learnservice;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//这个两个Intent操作都是的一个service
findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startService(new Intent(MainActivity.this,MyService.class));
}
});
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopService(new Intent(MainActivity.this,MyService.class));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
第二课时: 绑定 Service
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent=new Intent(MainActivity.this,MyService.class);
//这个两个Intent操作都是的一个service
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this) ;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
startService(intent);
break;
case R.id.button2:
stopService(intent);
break;
case R.id.button3:
//this:代表服务的连接,监听服务的
bindService(intent, this, Context.BIND_AUTO_CREATE);
break;
case R.id.button4:
unbindService(this);
break;
default:
break;
}
}
public void onSericeConnected(ComponentName name,IBinder service){
}
private void bindService(Intent intent2, MainActivity mainActivity,
int bindAutoCreate) {
// TODO Auto-generated method stub
}
private void unbindService(MainActivity mainActivity) {
// TODO Auto-generated method stub
}
private void bindService(Intent intent2) {
// TODO Auto-generated method stub
}
private void stopActivity(Intent intent2) {
// TODO Auto-generated method stub
}
第三课时:
更多相关文章
- ArcGIS for Android示例解析之GP服务调用-----ViewShed
- Android(安卓)day_11-2 (服务)
- Android(安卓)service跨进程调用和启动检查
- context、getApplicationContext()
- Android(安卓)binder机制(native服务篇)
- (20120731)android面试总结(002)
- Android(安卓)服务入门(电话监听)
- Android(安卓)服务端将位置信息发送给客户端的实现
- Android(安卓)webview加载本地html实现跨域访问