文章的目的为了记录使用java 进行android app 开发学习的经历。本职为嵌入式软件开发,公司安排开发app,临时学习,完成app的开发。开发流程和要点有些记忆模糊,赶紧记录,防止忘记。
相关链接:
开源 java android app 开发(一)开发环境的搭建-优快云博客
开源 java android app 开发(二)工程文件结构-优快云博客
开源 java android app 开发(三)GUI界面布局和常用组件-优快云博客
开源 java android app 开发(四)GUI界面重要组件-优快云博客
开源 java android app 开发(五)文件和数据库存储-优快云博客
开源 java android app 开发(六)多媒体使用-优快云博客
开源 java android app 开发(七)通讯之Tcp和Http-优快云博客
开源 java android app 开发(八)通讯之Mqtt和Ble-优快云博客
开源 java android app 开发(九)后台之线程和服务-优快云博客
开源 java android app 开发(十)广播机制-优快云博客
开源 java android app 开发(十一)调试、发布-优快云博客
开源 java android app 开发(十二)封库.aar-优快云博客
推荐链接:
开源C# .net mvc 开发(一)WEB搭建_c#部署web程序-优快云博客
开源 C# .net mvc 开发(二)网站快速搭建_c#网站开发-优快云博客
开源 C# .net mvc 开发(三)WEB内外网访问(VS发布、IIS配置网站、花生壳外网穿刺访问)_c# mvc 域名下不可訪問內網,內網下可以訪問域名-优快云博客
开源 C# .net mvc 开发(四)工程结构、页面提交以及显示_c#工程结构-优快云博客
开源 C# .net mvc 开发(五)常用代码快速开发_c# mvc开发-优快云博客
前面讲的主要是安卓的前台控件,以及通讯等,这章主要是讲安卓的后台程序。顾名思义就是和前台无关,后台运行。
本章内容如下:
1.线程和通讯
2.服务
一、Thread线程,界面为主线程,通常把复杂耗时的任务放在线程中执行,主线程卡顿。
new Thread(() -> {
// 耗时操作(如网络请求)
runOnUiThread(() -> {
// 返回主线程更新 UI
});
}).start();
Handler 和 Looper,线程间通信。后台线程通过这种方式,与主线程界面进行通讯
Handler handler = new Handler(Looper.getMainLooper());
new Thread(() -> {
// 耗时操作
handler.post(() -> {
// 在主线程执行
});
}).start();
实例,实现button按下后,创建线程,定时发送数据给前台,更新进度条。
1.1.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="等待子线程发送消息..."
android:textSize="18sp" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送消息" />
</LinearLayout>
1.2.MainActivity.java
package com.example.myapplication;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private Button button;
private Handler handler;
private ProgressBar progressbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化 UI 组件
textView = findViewById(R.id.textView);
button = findViewById(R.id.button);
progressbar = findViewById(R.id.progressBar);
// 初始化 Handler,绑定到主线程的 Looper
handler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
// 在主线程中处理消息
if (msg.what == 1) { // 检查消息类型
String data = (String) msg.obj; // 获取消息内容
textView.setText("收到消息: " + data);
}
else if(msg.what == 2) {
int i = (int)msg.obj;
progressbar.setProgress(i);
}
}
};
// 设置按钮的点击事件
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 按钮点击后,启动一个子线程发送消息
new Thread(new Runnable() {
@Override
public void run() {
/*
// 模拟子线程中的操作
String data = "Hello from Thread!";
Message msg = handler.obtainMessage(1, data); // 创建消息
handler.sendMessage(msg); // 发送消息到主线程
*/
Message msg = handler.obtainMessage(1, "收到消息"); // 创建消息
handler.sendMessage(msg); // 发送消息到主线程
int i;
for(i=0;i<100;i++)
{
msg = handler.obtainMessage(2, i); // 创建消息
handler.sendMessage(msg); // 发送消息到主线程
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt(); // 重新标记中断状态
break; // 退出循环
}
}
}
}).start();
}
});
}
}
1.3.效果
二、服务Service,Service是一种可以在后台执行长时间运行操作的组件,它不提供用户界面,但可以用于处理网络请求、播放音乐、文件下载等任务,即使应用退出(未被销毁)仍可继续运行。Service 默认运行在主线程,因此耗时操作仍需结合线程或协程处理。
-
无界面:在后台运行,不与用户直接交互。
-
生命周期:由系统管理,可通过
startService()
或bindService()
启动。 -
主线程运行:默认在主线程执行代码,耗时操作需另开线程。
2.1AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity"
android:exported="true"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService" />
</application>
</manifest>
2.2j修改界面Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/messageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press buttons to interact with the service"
android:textSize="18sp"
android:layout_marginBottom="16dp" />
<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Service"
android:layout_marginBottom="16dp" />
<Button
android:id="@+id/stopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop Service"
android:layout_marginBottom="16dp" />
<Button
android:id="@+id/getMessageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Message from Service" />
</LinearLayout>
2.3添加服务MyService,这里注意是添加服务,不是添加类
package com.example.myapplication;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private static final String TAG = "MyService";
private final IBinder binder = new LocalBinder(); // Binder 用于通信
// 自定义 Binder 类
public class LocalBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "Service bound");
return binder;
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "Service created");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Service started");
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "Service destroyed");
}
// 服务提供的公共方法
public String getMessage() {
return "Hello from Service!";
}
}
2.4主程序MainActivity.java
package com.example.myapplication;
import android.content.ComponentName;
import android.content.Context;
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.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private MyService myService;
private boolean isBound = false;
private TextView messageText;
private Button startButton, stopButton, getMessageButton;
// ServiceConnection 用于绑定服务
private final ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "Service connected");
MyService.LocalBinder binder = (MyService.LocalBinder) service;
myService = binder.getService();
isBound = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "Service disconnected");
isBound = false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
messageText = findViewById(R.id.messageText);
startButton = findViewById(R.id.startButton);
stopButton = findViewById(R.id.stopButton);
getMessageButton = findViewById(R.id.getMessageButton);
// 启动服务
startButton.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent); // 启动服务
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE); // 绑定服务
});
// 停止服务
stopButton.setOnClickListener(v -> {
if (isBound) {
unbindService(serviceConnection); // 解绑服务
isBound = false;
}
Intent intent = new Intent(MainActivity.this, MyService.class);
stopService(intent); // 停止服务
});
// 与服务通信
getMessageButton.setOnClickListener(v -> {
if (isBound) {
String message = myService.getMessage();
messageText.setText(message);
} else {
messageText.setText("Service not bound");
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (isBound) {
unbindService(serviceConnection); // 解绑服务
isBound = false;
}
}
}
2.5最终效果,按钮创建,传递消息和消灭。