AI智能
改变未来

android stdio Service服务使用方法 LocalBroadcastManager使用方法 广播使用方法 Intent 意图使用方法

UartService    服务文件package com.example.led20;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.util.Log;import androidx.annotation.Nullable;import androidx.localbroadcastmanager.content.LocalBroadcastManager;public class UartService extends Service {private static final String TAG =\"后台服务提示=\" ;@Nullable  //指示参数、字段或方法返回值可以为null。//必须设置公共方法   MainActivity才可调用public void led (int ff) {showMessage(\"第六步--收到=\" + ff);ff++;showMessage(\"第七步--执行++=\" + ff);Intent intent = new Intent(String.valueOf(ff));showMessage(\"第八步--创建意图成功\");//LocalBroadcastManager=帮助程序注册意图广播并将其发送到本地对象         getInstance=获取实例       sendBroadcast==向所有感兴趣的广播接收器广播给定的意图。LocalBroadcastManager.getInstance(this).sendBroadcast(intent);//showMessage(\"第九步--意图广播发送成功\");}public boolean intmain(){showMessage(\"第五步=intmain()已启动\" );return  true;}private void showMessage(String msg) {Log.e(TAG, msg);}//提示显示@Override //指示方法声明旨在重写public IBinder onBind(Intent intent) {showMessage(\"第三步=执行return mBinder函数\" );// return null;return mBinder;}private final IBinder mBinder = new UartService.LocalBinder();public class LocalBinder extends Binder {public LocalBinder() {showMessage(\"第一步=执行函数= public LocalBinder()\" );}UartService getService() {showMessage(\"第四步=执行函数= return UartService.this;\" );return UartService.this;}}//------------------------------------------------------public UartService() {showMessage(\"第二步=UartService()已启动\" );}//------------------------------------------------------}MainActivity 文件函数package com.example.led20;import android.content.BroadcastReceiver;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.util.Log;import android.view.View;import android.widget.Button;import androidx.annotation.Nullable;import androidx.appcompat.app.AppCompatActivity;import androidx.localbroadcastmanager.content.LocalBroadcastManager;public class MainActivity extends AppCompatActivity {private static final String TAG =\"主函数提示=\" ;private UartService uartService = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//打开窗口service_init();//启动后台服务Button button1=findViewById(R.id.button1);//获取按钮button1.setOnClickListener(new View.OnClickListener() {//按钮点击执行@Overridepublic void onClick(View v) {//按钮点击执行showMessage(\"1---按键1-按下\");try {Thread.sleep(200);//主线程休眠-防止连击} catch (InterruptedException e) {e.printStackTrace();}showMessage(\"2--完成线程休眠\");int ff=1;uartService.led(ff);showMessage(\"调用服务器端led()函数-完成\");}});}private void service_init() {showMessage(\"初始化服务\");//创建意图=AAA  实例化      UartService.classIntent AAA = new Intent(this, UartService.class);showMessage(\"创建意图完成\");//绑定服务  参数1=UartService.class  参数2=用于监视应用程序服务状态的接口     参数3=自动创建服务bindService(AAA, BBB, Context.BIND_AUTO_CREATE);showMessage(\"绑定服务完成\");//帮助程序注册意图广播并将其发送到本地对象+   获取实例+     注册与给定IntentFilter匹配的任何本地广播的接收。LocalBroadcastManager.getInstance(this).registerReceiver(DDD,EEE());//调用下面的函数showMessage(\"注册意图广播完成\");}//ServiceConnection=用于监视应用程序服务状态的接口private ServiceConnection BBB = new ServiceConnection() {public void onServiceConnected(ComponentName className, IBinder rawBinder) {//IBinder=远程对象的基本接口,是轻量级远程过程调用机制的核心部分,专为执行进程内和跨进程调用时的高性能而设计。此接口描述与远程对象交互的抽象协议。不要直接实现这个接口,而是从{binder}扩展。uartService = ((UartService.LocalBinder)rawBinder).getService();//-----------------------------------------------------------获取服务对象showMessage(\"服务已经启动\"+uartService);if(uartService.intmain()){showMessage(\"服务已经连接\"+uartService);}else {showMessage(\"服务已断开\"+uartService);}}public void onServiceDisconnected(ComponentName classname) {uartService = null;showMessage(\"服务为空\");}};private final BroadcastReceiver DDD = new BroadcastReceiver() {public void onReceive(Context context, Intent intent) {String action = intent.getAction();showMessage(\"接收广播context=\"+action);//2showMessage(\"接收广播intent=\"+intent);  //Intent { act=2 }}};//静态方法private static IntentFilter EEE() {//意图过滤器IntentFilter intentFilter = new IntentFilter();//必须匹配到以下字符intentFilter.addAction(\"2\");intentFilter.addAction(\"3\");intentFilter.addAction(\"4\");intentFilter.addAction(\"5\");intentFilter.addAction(\"6\");intentFilter.addAction(\"7\");return intentFilter;}private void showMessage(String msg) {Log.e(TAG, msg);//调试使用-error}//提示显示}添加服务<service android:enabled=\"true\" android:name=\"UartService\"/>
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » android stdio Service服务使用方法 LocalBroadcastManager使用方法 广播使用方法 Intent 意图使用方法