相关疑难解决方法(0)

从 Workmanager 更新 UI

如何从 Android WorkManager 更新 UI。

Android Workmanager可以接收Boolean、Integer、Long、Float、Double、String

文档说:

public Data.Builder putAll (Map<String, Object> values)
Run Code Online (Sandbox Code Playgroud)

“将所有输入键值对放入生成器中。有效类型包括:Boolean、Integer、Long、Float、Double、String 以及每种类型的数组版本。无效类型会引发 IllegalArgumentException。”

  1. 如何传递更新 UI 的回调。
  2. 如何在不调用 DB 的情况下传递 POJO。

Result.SUCCESS 或 Result.FAILURE 不是解决方案,因为只有在工作完成时才会返回。

android android-jetpack android-workmanager

8
推荐指数
1
解决办法
9548
查看次数

在服务中更新MusicPlayer SeekBar

我正在尝试更新音乐播放器SeekBar,但没有任何反应.服务在我的活动中启动正常并正确绑定(因为我可以使用我服务中的一些方法和音乐播放正常).这是我的服务代码:

public class MusicPlayerService extends Service {

MediaPlayer myMusicPlayer;
String path;
Intent getMusicId = new Intent();

// Binder given to clients
private final IBinder mBinder = new LocalBinder();

/**
 * Class used for the client Binder.  Because we know this service always
 * runs in the same process as its clients, we don't need to deal with IPC.
 */
public class LocalBinder extends Binder {
    MusicPlayerService getService() {
        // Return this instance of LocalService so clients can call public methods …
Run Code Online (Sandbox Code Playgroud)

service android seekbar audio-player android-music-player

6
推荐指数
1
解决办法
5848
查看次数

如何更改GlobalVariables时通知活动

我有一个通过USB线连接到计算机的Android应用程序.我使用TCPServer类发送消息和监听.例如:

当我发送消息时:request:x
我收到响应:响应:x:55

我需要根据我得到的反应对我的活动进行更改.目前,我通过将activity和activity类对象传递给TCPServer的构造函数来暂时解决问题

public TCPServer(int portNum, Activity activity, IntroActivity ia) {
    super();
    port = portNum;
    this.activity = activity;
    this.ia = ia;
}    
Run Code Online (Sandbox Code Playgroud)

然后我收到回复后:

void updateButton(final int color, final String txt) {
    activity.runOnUiThread(new Runnable() {
         public void run() {
             ia.getConnectionButton().setBackgroundColor(color);
             ia.getConnectionButton().setText(txt);
        }
    });
}   
Run Code Online (Sandbox Code Playgroud)

如你所见,这根本没有效果.每当收到相关变量时,我都需要以某种方式通知活动.我使用Class for GlobalVariables并在listen()之后更改那些静态变量,但是我在通知活动时遇到了麻烦.

android notify android-activity

1
推荐指数
1
解决办法
2996
查看次数

从服务更新/访问进度条、TextView

我有一个活动 A,它有一个进度条和一个文本视图。

如果用户单击正在启动的服务(ServiceB)按钮,我试图找到一种方法如何从 ServiceB 更新 Activity A 中的进度条,同时在 Activity A 的 Textview 中设置(进度)文本。

我在 Google 和 Stackoverflow 上四处查看,我想我找到了一种方法来做到这一点,如下所述

但我在实施这一点时遇到困难,非常感谢任何帮助。

PS:不要投反对票,我知道 UI 不应该直接从服务访问,所以我正在寻找一种正确的方法。

一些相关代码:

活动一:

@EActivity(R.layout.downloads_activity)
public class DownloadsActivity extends BaseActivity {

@ViewById(R.id.progress_text)
TextView progresstxt;

@ViewById(R.id.progressdownload)
ProgressBar downloadprogress;

// Update Progressbar and set Text sent from ServiceB
}
Run Code Online (Sandbox Code Playgroud)

服务B:

public class ServiceB extends IntentService {
...

@Override
    public void onProgress(DownloadRequest request, long totalBytes, long downloadedBytes, int progress) {
        int id = request.getDownloadId();

        if (!isActive) {
            downloadManager.cancel(downloadId1);
            deleteCancelledFile.deleteOnExit();
        } else if …
Run Code Online (Sandbox Code Playgroud)

service android handler

1
推荐指数
1
解决办法
925
查看次数