Android之Service使用解析
来源:互联网 发布:万网域名cname解析 编辑:程序博客网 时间:2024/06/06 03:17
转载请注明出处:http://blog.csdn.net/professionit/article/details/71600788
Service是安卓四大组件之一,凡是接触安卓的人员应该都知道Service,但是具体Service怎么使用,大家可能还糊里糊涂,下面就来简单介绍下Service的使用吧
1、首先创建一个类继承自service
package com.daokou.servicetest;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.util.Log;/** * Created by mapeng on 2017/5/10. * EMail:704934878@qq.com */public class MyService extends Service { public static final String TAG = "MyService"; private MyBinder mBinder = new MyBinder(); @Override public void onCreate() { super.onCreate(); Log.d(TAG, "onCreate()"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "onStartCommand()"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { Log.d(TAG, "onDestroy()"); super.onDestroy(); } @Override public IBinder onBind(Intent intent) { return mBinder; } class MyBinder extends Binder { public void startDownload() { Log.d("TAG", "startDownload()"); // 具体做后台处理的事情,例如后台下载啊 } }}
2、再manifest中配置此service类
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.daokou.servicetest"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" 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=".MyService" ></service> </application></manifest>
3、mainactivity中绑定并启用service
package com.daokou.servicetest;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button startService; private Button stopService; private Button bindService; private Button unbindService; private MyService.MyBinder myBinder; private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { } @Override public void onServiceConnected(ComponentName name, IBinder service) { myBinder = (MyService.MyBinder) service; myBinder.startDownload(); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startService = (Button) findViewById(R.id.start_service); stopService = (Button) findViewById(R.id.stop_service); bindService = (Button) findViewById(R.id.bind_service); unbindService = (Button) findViewById(R.id.unbind_service); startService.setOnClickListener(this); stopService.setOnClickListener(this); bindService.setOnClickListener(this); unbindService.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.start_service: Intent startIntent = new Intent(this, MyService.class); startService(startIntent); break; case R.id.stop_service: Intent stopIntent = new Intent(this, MyService.class); stopService(stopIntent); break; case R.id.bind_service: Intent bindIntent = new Intent(this, MyService.class); bindService(bindIntent, connection, BIND_AUTO_CREATE); break; case R.id.unbind_service: unbindService(connection); break; default: break; } }}
4、xml文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/start_service" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Start Service" /> <Button android:id="@+id/stop_service" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Stop Service" /> <Button android:id="@+id/bind_service" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Bind Service" /> <Button android:id="@+id/unbind_service" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Unbind Service" /></LinearLayout>
好了,以上的准备工作都做好了,下面运行起来,就可以使用了
注意:以绑定的方式启动时,当activity销毁时,service也会随之销毁,只要不销毁,service就会继续后台运行。
1 0
- Android之Service使用解析
- Android Service学习之 Service完全解析
- Android 组件之Service解析
- Android Service的使用解析
- android之service使用
- Android Service 之(Bind Service,使用 Messenger)
- Android 回顾Service之Service基础使用
- Android四大应用构件之Service解析
- Android 四大组件之Service解析
- Android四大组件之Service解析
- Android提高之Service用法实例解析
- Android 中 Service 全面解析与使用
- Android中的Service使用全解析
- Android service解析与使用实例
- Android Service解析(二):远程Service的使用
- Android Service 之三(Bind Service,使用 AIDL)
- Android Service 之三(Bind Service,使用 Messenger)
- Android Service 之三(Bind Service,使用 Messenger)
- 80端口被占用,apache启动
- Threading
- <纯干货-2>GAN学习视频-part4
- stm32 unknown target connected 和 ST-LINK USB communication error错误
- 链表学习(1)
- Android之Service使用解析
- 连接池的作用
- java多线程之线程通信
- Android兼容性测试的一些坑
- Lenet、Alexnet 、VGG、 GoogleNet、ResNet模型
- test
- cas协议时序图
- Linux 的centOS下开启.htaccess的支持(去掉php后缀访问)
- 脚踝核磁共振