从非 ui 线程设置剪贴板
Setting Clipboard from non-ui thread
问题:无法从非ui后台线程
设置剪贴板
当我的应用程序在后台运行时,我该如何设置剪贴板?
public class Messages {
public void SetMessage(String text) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText(text);
} else {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("NewClip", text);
clipboard.setPrimaryClip(clip);
}
}
}
错误:无法解析 getSystemService(java.lang.String)
我尝试了多种方法来解决这个问题,其中之一是:
配置Class
/* I made a Config class file like so: */
public final class Config {
public static com.dysanix.official.MainActivity MainContext = null;
}
/* And put this in the onCreate of the MainActivity: */
Config.MainContext = this;
/*
* And then using Config.MainContext.getSystemService() in the other
* class works, as long as the UI is visible on the screen.. but as soon as
* I tab out, the code doesn't work anymore.
*/
我还尝试在 MainActivity 中制作一个 "Runnable" 并从另一个 class 调用它,但同样的问题:它有效,直到我退出应用程序。我正在从一个循环的 AsyncTask 调用该方法,我知道它是有效的,因为控制台在循环结束时不断打印一条日志消息。
如有任何帮助,我们将不胜感激!
Error: Cannot resolve getSystemService(java.lang.String)
getSystemService()
是 Context
上的一个方法。将 Context
传递到您的 SetMessage()
方法中,或将该方法移至某个 Activity
、Service
或其他 Context
实现。然后,在 Context
.
上调用 getSystemService()
I tried multiple things to solve this issue, one of them is:
不要在静态字段中放置 Activity
。这不仅代表内存泄漏,而且 Activity
一旦被销毁就没用了。
Issue: Can't set clipboard from non-ui background thread
此处与线程无关。
问题:无法从非ui后台线程
设置剪贴板当我的应用程序在后台运行时,我该如何设置剪贴板?
public class Messages {
public void SetMessage(String text) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText(text);
} else {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("NewClip", text);
clipboard.setPrimaryClip(clip);
}
}
}
错误:无法解析 getSystemService(java.lang.String)
我尝试了多种方法来解决这个问题,其中之一是:
配置Class
/* I made a Config class file like so: */
public final class Config {
public static com.dysanix.official.MainActivity MainContext = null;
}
/* And put this in the onCreate of the MainActivity: */
Config.MainContext = this;
/*
* And then using Config.MainContext.getSystemService() in the other
* class works, as long as the UI is visible on the screen.. but as soon as
* I tab out, the code doesn't work anymore.
*/
我还尝试在 MainActivity 中制作一个 "Runnable" 并从另一个 class 调用它,但同样的问题:它有效,直到我退出应用程序。我正在从一个循环的 AsyncTask 调用该方法,我知道它是有效的,因为控制台在循环结束时不断打印一条日志消息。
如有任何帮助,我们将不胜感激!
Error: Cannot resolve getSystemService(java.lang.String)
getSystemService()
是 Context
上的一个方法。将 Context
传递到您的 SetMessage()
方法中,或将该方法移至某个 Activity
、Service
或其他 Context
实现。然后,在 Context
.
getSystemService()
I tried multiple things to solve this issue, one of them is:
不要在静态字段中放置 Activity
。这不仅代表内存泄漏,而且 Activity
一旦被销毁就没用了。
Issue: Can't set clipboard from non-ui background thread
此处与线程无关。