Android实例三:学习Service
16lz
2021-01-26
第一步.MainActivity.java
package com.chaowen;import android.app.Activity;import android.app.Service;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity { private Button startButton,stopButton,bindButton,unbindButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startButton = (Button)findViewById(R.id.startButton); stopButton = (Button)findViewById(R.id.stopButton); bindButton = (Button)findViewById(R.id.bindButton); unbindButton = (Button)findViewById(R.id.unbindButton); //添加监听器 startButton.setOnClickListener(startListener); stopButton.setOnClickListener(stopListener); bindButton.setOnClickListener(bindListener); unbindButton.setOnClickListener(unbindListener); } //启动Service监听器 private OnClickListener startListener = new OnClickListener() {@Overridepublic void onClick(View v) {//创建IntentIntent intent = new Intent();//设置Action属性intent.setAction("com.chaowen.action.MY_SERVICE");//启动该ServicestartService(intent);}};//停止Service监听器private OnClickListener stopListener = new OnClickListener() {@Overridepublic void onClick(View v) { Intent intent = new Intent(); intent.setAction("com.chaowen.action.MY_SERVICE"); stopService(intent);}};//连接对象private ServiceConnection connection =new ServiceConnection() {//断开时调用@Overridepublic void onServiceDisconnected(ComponentName name) {//输出日志Log.i("Service", "断开连接");//通过Toast显示信息Toast.makeText(MainActivity.this, "断开连接", Toast.LENGTH_LONG).show();}//连接时调用@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {//输出日志Log.i("Service", "连接成功");//通过Toast显示信息Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_LONG).show();}};//绑定Service监听器private OnClickListener bindListener = new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent();intent.setAction("com.chaowen.action.MY_SERVICE");bindService(intent, connection, Service.BIND_AUTO_CREATE);}};//解除绑定Service监听器private OnClickListener unbindListener = new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent();intent.setAction("com.chaowen.action.MY_SERVICE");unbindService(connection);}};}
第二步.MyService.java
package com.chaowen;
import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;import android.widget.Toast;public class MyService extends Service {@Overridepublic IBinder onBind(Intent intent) { Log.i("SERVICE", "onBind"); Toast.makeText(MyService.this, "onBind...", Toast.LENGTH_LONG).show();return null;} @Overridepublic void onCreate() { Log.i("SERVICE", "onCreate"); Toast.makeText(MyService.this, "onCreate...", Toast.LENGTH_LONG).show();}@Overridepublic void onStart(Intent intent, int startId) { Log.i("SERVICE", "onStart"); Toast.makeText(MyService.this, "onStart...", Toast.LENGTH_LONG).show();}@Overridepublic void onDestroy() { Log.i("SERVICE", "onDestroy"); Toast.makeText(MyService.this, "onDestroy...", Toast.LENGTH_LONG).show();}}
第三步.main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><Button android:text="启动Service" android:id="@+id/startButton" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:text="停止Service" android:id="@+id/stopButton" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:text="绑定Service" android:id="@+id/bindButton" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:text="解除绑定" android:id="@+id/unbindButton" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
第四步.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.chaowen" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="4" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="MyService"> <intent-filter> <action android:name="com.chaowen.action.MY_SERVICE" /> </intent-filter> </service> </application></manifest>
更多相关文章
- SpringBoot 2.0 中 HikariCP 数据库连接池原理解析
- Android(安卓)ListView实现方法三(自定义适配器)
- Android(安卓)蓝牙客户端实现
- Adroid Studio基于TCP协议的通信
- Android(安卓)应用的耗电量优化
- android网络与通信(三种网络接口简述 )
- 二、获取wifi列表并连接wifi
- 一个不错的loading效果
- Android(安卓)基于NetworkCallback的网络状态监听框架