我想从不同的类更新我的UI。我熟悉runOnUiThread()
方法,但不知道如何在这种情况下实现它?
public class UploadAct extends MainActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
}
//my code and functions would go here
}
然后,我的UploadData类
public class UploadData extends UploadAct {
public void doSomethig(){
printThis("I want to print this message to the UI");
}
public void printThis(String messsage) {
final String mess = message;
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),mess,Toast.LENGTH_LONG).show();
// I want this to display on the main thread
txt_upload.setText(mess);// and this also
}
});
}
}
答案 0 :(得分:2)
使用BroadcastReceiver
// define a Broadcast Intent Action in String resources
<string name="broadcast_id">MY_BROADCAST_ID</string>
// register receiver in constructor/onCreate()
MyBroadcastReceiver myBroadcastReceiver = new MyBroadcastReceiver();
IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction(context.getString(R.string.broadcast_id));
context.registerReceiver(myBroadcastReceiver, myIntentFilter);
// place your BroadcastReceiver in MainActivity, your UploadData class
public class MyBroadcastReceiver extends BroadcastReceiver {
public MyBroadcastReceiver(){
super();
}
@Override public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Broadcast received");
if(intent.getAction() != null && intent.getAction().equals(context.getString(R.string.broadcast_id)) ){
// do something
}
}
}
// send Broadcasts from where you want to act, your UploadAct class.
Intent intent = new Intent();
intent.setAction(context.getString(R.string.broadcast_id));
context.sendBroadcast(intent);
Log.d(TAG, "Broadcast sent.");
// you can unregister this receiver in onDestroy() method
context.unregisterReceiver(myBroadcastReceiver);
答案 1 :(得分:1)
您还可以使用界面将UI更新为监听器。
首先,创建一个界面
public interface UpdateTextListener {
void updateText(String data);
}
然后,在UploadData类中调用其方法
public class UploadData extends UploadAct {
UpdateTextListener listener;
public void doSomethig(){
listener.updateText("data to be loaded");
}
}
然后,通过聆听此方法更新您的UploadAct
public class UploadAct extends MainActivity implements UpdateTextListener {
@Override
public void updateText(String data) {
textview.setText(data);
}
}
答案 2 :(得分:0)
首先 - 没有像某类课程那样的东西。有些活动可以处理UI小部件(ex TextView)。如果要从UploadData类对UI进行一些更改,则必须以某种方式传递给此类。可能是构造函数:
public class UploadData extends UploadAct{
private TextView txt_upload;
public UploadData(TextView tv)
{
txt_upload = tv;
}
public void doSomethig(){
printThis("I want to print this message to the UI")
}
public void printThis(String messsage) {
final String mess = message;
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),mess,Toast.LENGTH_LONG).show();// I want this to display on the main thread
txt_upload.setText(mess);// and this also
}
});
}
}
我假设您在MainActivity中创建了DataUpload。
答案 3 :(得分:0)
每个人都使用这么多的图书馆,因为他们在Android中忘记了内置函数:)
确保使用AsyncTask
并不困难,除此之外它还提供doInBackground
功能,它具有https://developer.android.com/reference/android/os/AsyncTask.html#publishProgress(Progress...)功能,您所要求的功能。
只需创建一个扩展AsyncTask并覆盖1-2函数的类(UploadTask)。