簡體   English   中英

在Android中使用線程發送消息

[英]Using Thread in Android for sending message

我有以下代碼:

 if (value) {
            thread = new Thread() {
                @Override
                public void run() {
                    try {
                        while (!isConnected()) {
                            synchronized (this) {
                                wait(3000);
                            }
                        }
                    } catch (InterruptedException ex) {
                    }

                    if(wifiManager.isWifiEnabled()){
                        sendMessageWidget();
                    } else {
                        showWifiSettingsAlert();
                    }
                }
            };

            thread.start();
        }

我希望我的應用等待Google api客戶端連接,然后再發送消息。

isConnected方法的代碼為:

public boolean isConnected() {
        mGoogleApiClient.connect();
        if (mGoogleApiClient.isConnected()) {
            return true;
        }
        return false;
    }

但是我收到此錯誤消息:NullPointerException:無法在未調用Looper.prepare()的線程內創建處理程序,它說該錯誤在id showWifiSettingsAlert()某處。

這是代碼:

public void showWifiSettingsAlert() {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());

        // Setting Dialog Title
        alertDialog.setTitle("Location accuracy tips");

        // Setting Dialog Message
        alertDialog
                .setMessage("You can improve the accuracy of your location by turning on\n- Wi-Fi");

        // On pressing Settings button
        alertDialog.setPositiveButton("Turn on",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        wifiManager.setWifiEnabled(true);
//                       Posalji poruke al pre toga jos jednom azuriraj
//                       lokaciju al ako je pozvana aplikacija iz widgeta
                        if (value) {
                            sendMessageWidget();
                        }
                    }
                });

        // on pressing cancel button
        alertDialog.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        wifiManager.setWifiEnabled(false);

                        // Posalji poruke al pre toga jos jednom azuriraj
                        // lokaciju al ako je pozvana aplikacija iz widgeta
                        if (value) {
                            sendMessageWidget();
                        }
                    }
                });

        // Showing Alert Message
        alertDialog.show();
    }

我想要,如果未啟用wifi,則用戶可以選擇啟用或不啟用wifi,但是無論哪種方式都應該發送消息...您能幫忙嗎?

您正在使用mGoogleApiClient.connect(); ,它是線程中的異步方法,因此不允許這樣做。

您可以嘗試使用runOnUiThread代替:

runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    //do your stuff here
                }
            });

由於您無法通過除主線程之外的其他線程來觸摸UI,因此必須將這些更改發布回UI線程及其循環程序和關聯的處理程序。 您可以通過創建與UI線程關聯的處理程序(可以在任何地方使用,因為Looper.getMainLooper()是靜態調用)來顯式地執行此操作,例如:

if (value) {
            Handler uiCallback = new Handler(Looper.getMainLooper());
            thread = new Thread() {
                @Override
                public void run() {
                    try {
                        while (!isConnected()) {
                            synchronized (this) {
                                wait(3000);
                            }
                        }
                    } catch (InterruptedException ex) {
                    }

                    uiCallback.post(new Runnable() {
                        @Override public void run() {
                            if(wifiManager.isWifiEnabled()){
                                sendMessageWidget();
                            } else {
                                showWifiSettingsAlert();
                            }
                        }
                    });

                }
            };

            thread.start();
        }

或者,如果您處於活動中並且執行相同的操作,則可以根本不使用處理程序,而可以將零件包裝在runOnUiThread()run()方法中。

您應該注意,實際上,您實際上不需要在這里使用任何線程。 如果您通過以下示例進行操作: https : //developer.android.com/google/auth/api-client.html,您會發現通過實現ConnectionCallbacks, OnConnectionFailedListener您可以從活動的onStart()調用mGoogleApis.connect() onStart()當連接失敗時,相應的回調將在調用線程上執行。 例如,

@Override
public void onConnected(Bundle connectionHint) {
     if(wifiManager.isWifiEnabled()){
         sendMessageWidget();
     } else {
         showWifiSettingsAlert();
     }
}

實現相同的事情...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM