文章的目的为了记录使用java 进行android app 开发学习的经历。本职为嵌入式软件开发,公司安排开发app,临时学习,完成app的开发。开发流程和要点有些记忆模糊,赶紧记录,防止忘记。
相关链接:
开源 java android app 开发(一)开发环境的搭建-CSDN博客
开源 java android app 开发(二)工程文件结构-CSDN博客
开源 java android app 开发(三)GUI界面布局和常用组件-CSDN博客
开源 java android app 开发(四)GUI界面重要组件-CSDN博客
开源 java android app 开发(五)文件和数据库存储-CSDN博客
开源 java android app 开发(六)多媒体使用-CSDN博客
开源 java android app 开发(七)通讯之Tcp和Http-CSDN博客
开源 java android app 开发(八)通讯之Mqtt和Ble-CSDN博客
开源 java android app 开发(九)后台之线程和服务-CSDN博客
推荐链接:
开源C# .net mvc 开发(一)WEB搭建_c#部署web程序-CSDN博客
开源 C# .net mvc 开发(二)网站快速搭建_c#网站开发-CSDN博客
开源 C# .net mvc 开发(三)WEB内外网访问(VS发布、IIS配置网站、花生壳外网穿刺访问)_c# mvc 域名下不可訪問內網,內網下可以訪問域名-CSDN博客
开源 C# .net mvc 开发(四)工程结构、页面提交以及显示_c#工程结构-CSDN博客
开源 C# .net mvc 开发(五)常用代码快速开发_c# mvc开发-CSDN博客
本章节主要内容是广播机制,广播(Broadcast)是Android四大组件之一,用于实现应用程序内部或应用程序之间的消息传递机制。它基于观察者模式,采用发布/订阅模型,能够实现组件间的解耦通信。
广播分为两个主要部分:广播发送者和广播接收者(BroadcastReceiver)。发送者通过Intent发送广播,接收者通过注册监听特定类型的广播来接收消息。
本章内容如下:
1 2个APP之间进行广播通讯。
2 基于服务和广播通讯的网络文件下载。
一、APP之间的广播通讯(APP1和APP2)
1.1 APP1的界面Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btnSendBroadcast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Broadcast"
android:layout_centerInParent="true"/>
</RelativeLayout>
1.2 APP1的主程序,MainActivity.java
package com.example.Mqtt;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnSendBroadcast = findViewById(R.id.btnSendBroadcast);
btnSendBroadcast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 创建Intent并设置Action
Intent intent = new Intent("com.example.app1.CUSTOM_BROADCAST");
// 添加数据
intent.putExtra("message", "Hello from App1!");
// 发送广播
sendBroadcast(intent);
}
});
}
}
1.3 APP2的界面activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvBroadcastMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Waiting for broadcast..."
android:layout_centerInParent="true"/>
</RelativeLayout>
1.4 APP2的广播接收类CustomBroadcastReceiver
package com.example.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;
public class CustomBroadcastReceiver extends BroadcastReceiver {
private Handler handler;
public CustomBroadcastReceiver(Handler handler) {
this.handler = handler;
}
@Override
public void onReceive(Context context, Intent intent) {
// 获取广播中的消息
String message = intent.getStringExtra("message");
// 显示Toast消息
//Toast.makeText(context, "Received: " + message, Toast.LENGTH_LONG).show();
new Thread(new Runnable() {
@Override
public void run() {
// 子线程操作
final String result = message;
// 通过 Handler 发送消息到主线程
Message msg = handler.obtainMessage();
msg.obj = result;
handler.sendMessage(msg);
}
}).start();
}
}
1.5 APP2的主程序,MainActivity.java
package com.example.myapplication;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private CustomBroadcastReceiver receiver;
TextView textView;
private Handler handler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
String data = (String) msg.obj;
updateUI(data);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.tvBroadcastMessage);
// 初始化BroadcastReceiver
receiver = new CustomBroadcastReceiver(handler);
// 创建IntentFilter并设置Action
IntentFilter filter = new IntentFilter("com.example.app1.CUSTOM_BROADCAST");
// 注册BroadcastReceiver
registerReceiver(receiver, filter);
}
public void updateUI(String data) {
textView.setText(data);
}
@Override
protected void onDestroy() {
super.onDestroy();
// 注销BroadcastReceiver
unregisterReceiver(receiver);
}
}
1.6 2个APP运行的效果,点击APP1的按钮,APP2收到消息。
二、基于服务和广播通讯的网络文件下载。该程序稍微复杂,通过服务实现了用户名和密码登录指定的网站登录,成功后跳转到相应页面下载apk程序。在下载过程中计算下载的文件大小,通过广播,更新主界面的文件下载进度条。下载后可以在文件管理中设置安装。在android studio的 device file Explorer中可以看到下载的文件。(注意下载后需要将文件删除,才能再次下载)
2.1 添加库,build.gradle
implementation 'com.squareup.okhttp3:okhttp:4.9.3'
implementation 'com.google.code.gson:gson:2.8.8'
2.2 配置文件中添加权限和服务,
<?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" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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.3 界面文件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" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
2.4 服务MyService
package com.example.myapplication;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MyService extends Service {
private static final String TAG = "MyService";
private final IBinder binder = new LocalBinder(); // Binder 用于通信
private static final String LOGIN_URL = "https://www.xxx.com/";
private static final String exe_URL = "https://xxx.apk";
private OkHttpClient client;
long fileSize;
// 自定义 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");
this.client = new OkHttpClient.Builder()
.cookieJar(new CookieJar() {
private final HashMap<String, List<Cookie>> cookieStore = new HashMap<>();
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
cookieStore.put(url.host(), cookies);
}
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
List<Cookie> cookies = cookieStore.get(url.host());
return cookies != null ? cookies : new ArrayList<>();
}
})
.build();
}
public void login() {
FormBody formBody = new FormBody.Builder()
.add("inputEmail3", "")
.add("inputPassword3","")
.build();
Request loginRequest = new Request.Builder()
.url(LOGIN_URL)
.post(formBody)
.build();
client.newCall(loginRequest).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e(TAG, "登录请求失败: " + e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
Log.d(TAG, "登录成功");
String responseData = response.body().string();
Log.d(TAG, "登录成功,响应数据: " + responseData);
// 登录成功后,跳转到目标网址
//redirectToTarget();Login
downLoad();
/*
if (mainActivity != null) {
mainActivity.enableDownloadButton();
mainActivity.showLoginComplete();
}
*/
} else {
Log.e(TAG, "登录失败,状态码: " + response.code());
}
}
});
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Service started");
login();
return START_NOT_STICKY;
}
public void downLoad() {
Request targetRequest = new Request.Builder()
//.url(IMG_URL)
//.url(IMG_URL)
.url(exe_URL)
.build();
client.newCall(targetRequest).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e(TAG, "下载请求失败: " + e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
fileSize = response.body().contentLength();
InputStream inputStream = response.body().byteStream();
saveFile(inputStream, "new.apk"); // 保存文件
Log.d(TAG, "文件下载成功");
//readAndPrintBinaryFile("downloaded_file.zip");
} else {
Log.e(TAG, "下载失败,状态码: " + response.code());
}
}
});
}
private void saveFile(InputStream inputStream, String fileName) throws IOException {
//FileOutputStream outputStream = new FileOutputStream(new File(context.getFilesDir(), fileName));
// 获取 Downloads 目录
File downloadsDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
// 创建目标文件
File outputFile = new File(downloadsDir, fileName);
// 确保目录存在
if (!downloadsDir.exists()) {
downloadsDir.mkdirs();
}
// 如果文件已存在,先删除
if (outputFile.exists()) {
boolean deleted = outputFile.delete();
if (!deleted) {
throw new IOException("无法删除已存在的文件: " + outputFile.getAbsolutePath());
}
}
// 写入文件
FileOutputStream outputStream = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int bytesRead;
long totalBytesRead = 0;
int lastProgress = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
//Log.d(TAG, "写入数据");
outputStream.write(buffer, 0, bytesRead); // 将文件写入本地
// 计算并更新进度
totalBytesRead += bytesRead;
int progress = (int) ((totalBytesRead * 100) / fileSize);
if (progress > lastProgress) {
Log.d(TAG, "进度条:"+ progress);
lastProgress = progress;
Intent progressIntent = new Intent(MainActivity.DOWNLOAD_PROGRESS_ACTION);
progressIntent.putExtra(MainActivity.PROGRESS_KEY, progress);
sendBroadcast(progressIntent);
}
}
outputStream.close();
inputStream.close();
Log.d(TAG, "文件保存成功: " + fileName);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "Service destroyed");
}
// 服务提供的公共方法
public String getMessage() {
return "Hello from Service!";
}
}
2.5 主程序 MainActivity.java
package com.example.myapplication;
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 android.widget.ProgressBar;
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;
private ProgressBar progressBar;
// 定义广播Action
public static final String DOWNLOAD_PROGRESS_ACTION = "com.example.myapplication.DOWNLOAD_PROGRESS";
public static final String PROGRESS_KEY = "progress";
// 广播接收器
private BroadcastReceiver progressReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null && intent.getAction().equals(DOWNLOAD_PROGRESS_ACTION)) {
int progress = intent.getIntExtra(PROGRESS_KEY, 0);
progressBar.setProgress(progress);
messageText.setText("下载进度: " + progress + "%");
}
}
};
// 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);
progressBar = findViewById(R.id.progressBar);
// 注册广播接收器
IntentFilter filter = new IntentFilter(DOWNLOAD_PROGRESS_ACTION);
registerReceiver(progressReceiver, filter);
// 启动服务
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.6 APP效果图和文件下载位置
文件下载位置