从非活动对象访问 UI-Thread,例如 iOS 中的 ([self PerformSelectorOnMainThread])

发布于 2024-12-17 08:21:41 字数 298 浏览 0 评论 0原文

我已经从 iOS 应用程序开发转向 Android,所以在寻找我以前使用过的一些功能时遇到了一些问题。

android中是否有像[self PerformSelectorOnMainThread:@selector() withObject: waitUntilDone:];这样的函数可以从任何类的实例访问,知道这个类不是继承自Activity类。

我发现有一个函数 runOnUiThread 可以执行此操作,但我无法使用它,因为我需要从非活动对象调用它,那么有没有办法访问当前的可见的活动或者我应该使用其他解决方案。

I've moved from iOS applications development to android so I'm facing some problems finding some functions I used to use before.

Is there a function like [self performSelectorOnMainThread:@selector() withObject: waitUntilDone:]; in android that can be accessed from an instance of any class, knowing that this class is not inheriting from the Activity class.

I've found that there is a function runOnUiThread that does this, but I couldn't use it since I need to call it from a non-activity object, so is there a way to access the current visible activity or should I use another solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风流物 2024-12-24 08:21:41

如果您没有活动对象,则可以使用处理程序。 “Handler”类可以更新UI。句柄提供了接收消息和可运行对象的方法。要使用处理程序,您必须对其进行子类化并重写handleMessage() 来处理消息。要处理可运行对象,您可以使用方法 post();您的活动中只需要一个处理程序实例。

您的线程可以通过方法 sendMessage(Message msg) 或 sendEmptyMessage 发布消息。

示例示例

  1. Handler 示例

在此示例中,我们使用“Handler”类来更新后台线程中的 ProgressBar。

使用活动“ProgressTestActivity”创建一个新的 Android 项目“de.vogella.android.handler”。创建以下布局“main.xml”。此布局包含 ProgressBar 并通过样式设置其外观。

将您的活动更改为以下内容:

package de.vogella.android.handler;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ProgressBar;

public class ProgressTestActivity extends Activity {
private Handler handler;
private ProgressBar progress;


/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    progress = (ProgressBar) findViewById(R.id.progressBar1);
    handler = new Handler();
}

public void startProgress(View view) {
    // Do something long
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i <= 10; i++) {
                final int value = i;
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        progress.setProgress(value);
                    }
                });
            }
        }
    };
    new Thread(runnable).start();
}
}

运行您的应用程序。一旦按下按钮,进度条就会从后台线程更新。

该示例来自 http://www.vogella.de/articles/AndroidPerformance/ Article.html#concurrency_handler

If you don't have an activity object, you can use handlers. The class "Handler" can update the UI. A handle provides methods for receiving messages and for runnables. To use a handler you have to subclass it and override handleMessage() to process messages. To process runnables, you can use the method post(); You only need one instance of a handler in your activity.

You thread can post messages via the method sendMessage(Message msg) or sendEmptyMessage.

Sample example

  1. Handler Example

In this example we use the class "Handler" to update a ProgressBar in a background thread.

Create a new Android project "de.vogella.android.handler" with the activity "ProgressTestActivity". Create the following layout "main.xml". This layout contains the ProgressBar and sets its appearance via a style.

Change your activity to the following:

package de.vogella.android.handler;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ProgressBar;

public class ProgressTestActivity extends Activity {
private Handler handler;
private ProgressBar progress;


/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    progress = (ProgressBar) findViewById(R.id.progressBar1);
    handler = new Handler();
}

public void startProgress(View view) {
    // Do something long
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i <= 10; i++) {
                final int value = i;
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        progress.setProgress(value);
                    }
                });
            }
        }
    };
    new Thread(runnable).start();
}
}

Run your application. Once you press your button the ProgressBar will get updated from the background thread.

The sample is from http://www.vogella.de/articles/AndroidPerformance/article.html#concurrency_handler

雨轻弹 2024-12-24 08:21:41

任何类都可以访问 Activity,只要您在该类中公开一些可以获取当前 ActivityContext 的公共方法。例如:

    public class SomeClass {

    public void launchActivity(Context context) {
       context.startActivity(new Intent(context, SomeOtherActivity.class));
    }

    public void showToast(Context context) {
       Toast.makeText(context, "check me out!", Toast.LENGHT_LONG).show();
    }

 }

这没有什么魔力。 Java 与 Objective-C 一样,是一种面向对象的语言。问题是,为了“赋予”普通的旧 java 对象当前的 Activity,您必须从该 Activity 中将其传递给它。

Any class can access the Activity so long as you expose some public method in that class that can grab hold of the current Activity or Context. For example:

    public class SomeClass {

    public void launchActivity(Context context) {
       context.startActivity(new Intent(context, SomeOtherActivity.class));
    }

    public void showToast(Context context) {
       Toast.makeText(context, "check me out!", Toast.LENGHT_LONG).show();
    }

 }

There's no magic to it. Java, like Objective-C is an object-oriented language. The thing is that in order to "give" your plain old java object the current Activity, you'll have to pass it to it from within that Activity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文