public class MainActivity extends AppCompatActivity {
public final static String UPPER_NUM="upper_num";
private EditText editText;
public jisuanThread jisuan;
public Handler mainhandler;
private TextView textView;
class jisuanThread extends Thread{
public Handler mhandler;
@Override
public void run() {
Looper.prepare();
final ArrayList<Integer> al=new ArrayList<>();
mhandler=new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what==0x123){
Bundle bundle=msg.getData();
int up=bundle.getInt(UPPER_NUM);
outer:
for(int i=3;i<=up;i++){
for(int j=2;j<=Math.sqrt(i);j++){
if(i%j==0){
continue outer;
}
}
al.add(i);
}
Message message=new Message();
message.what=0x124;
Bundle bundle1=new Bundle();
bundle1.putIntegerArrayList("Result",al);
message.setData(bundle1);
mainhandler.sendMessage(message);
}
}
};
Looper.loop();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText= (EditText) findViewById(R.id.et_num);
textView= (TextView) findViewById(R.id.tv_show);
jisuan=new jisuanThread();
jisuan.start();
mainhandler=new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what==0x124){
Bundle bundle=new Bundle();
bundle=msg.getData();
ArrayList<Integer> al=bundle.getIntegerArrayList("Result");
textView.setText(al.toString());
}
}
};
findViewById(R.id.bt_jisuan).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Message message=new Message();
message.what=0x123;
Bundle bundle=new Bundle();
bundle.putInt(UPPER_NUM, Integer.parseInt(editText.getText().toString()));
message.setData(bundle);
jisuan.mhandler.sendMessage(message);
}
});
}
}
public static void loop() {
final Looper me = myLooper();
if (me == null) {
throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
}
final MessageQueue queue = me.mQueue;
// Make sure the identity of this thread is that of the local process,
// and keep track of what that identity token actually is.
Binder.clearCallingIdentity();
final long ident = Binder.clearCallingIdentity();
for (;;) {
Message msg = queue.next(); // might block
if (msg == null) {
// No message indicates that the message queue is quitting.
return;
}
// This must be in a local variable, in case a UI event sets the logger
Printer logging = me.mLogging;
if (logging != null) {
logging.println(">>>>> Dispatching to " + msg.target + " " +
msg.callback + ": " + msg.what);
}
msg.target.dispatchMessage(msg);
if (logging != null) {
logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
}
// Make sure that during the course of dispatching the
// identity of the thread wasn't corrupted.
final long newIdent = Binder.clearCallingIdentity();
if (ident != newIdent) {
Log.wtf(TAG, "Thread identity changed from 0x"
+ Long.toHexString(ident) + " to 0x"
+ Long.toHexString(newIdent) + " while dispatching to "
+ msg.target.getClass().getName() + " "
+ msg.callback + " what=" + msg.what);
}
msg.recycleUnchecked();
}
}
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
private static void prepare(boolean quitAllowed) {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
sThreadLocal.set(new Looper(quitAllowed));
}
public Handler(Callback callback, boolean async) {
if (FIND_POTENTIAL_LEAKS) {
final Class<? extends Handler> klass = getClass();
if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
(klass.getModifiers() & Modifier.STATIC) == 0) {
Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
klass.getCanonicalName());
}
}
mLooper = Looper.myLooper();
if (mLooper == null) {
throw new RuntimeException(
"Can't create handler inside thread that has not called Looper.prepare()");
}
mQueue = mLooper.mQueue;
mCallback = callback;
mAsynchronous = async;
}
public static Looper myLooper() {
return sThreadLocal.get();
}
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
msg.target = this;
if (mAsynchronous) {
msg.setAsynchronous(true);
}
return queue.enqueueMessage(msg, uptimeMillis);
}
public void dispatchMessage(Message msg) {
if (msg.callback != null) {
handleCallback(msg);
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有