没有回应理由执行服务

时间:2014-10-11 12:33:03

标签: java android

我正在尝试创建一个登录页面,并使用android的服务类在后台运行。但我看不到我在代码末尾创建的菜单。当我在模拟器中运行它时,应用程序完全正常。但是在调试模式下,它会出现 anr error ,这意味着应用程序没有响应。我认为我的应用程序崩溃是因为在完成oncreate方法之前服务响应时间非常长。我该如何解决这个问题?这是我的代码。

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {

            imService = ((IMService.IMBinder)service).getService();  

            if (imService.isUserAuthenticated() == true)
            {
                Intent i = new Intent(Login.this, FriendList.class);                                                                
                startActivity(i);
                Login.this.finish();
            }
        }

        public void onServiceDisconnected(ComponentName className) {

            imService = null;
            Toast.makeText(Login.this, R.string.local_service_stopped,
                    Toast.LENGTH_SHORT).show();
        }
    };




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    

        startService(new Intent(Login.this,  IMService.class));         


        setContentView(R.layout.login_screen);
        setTitle("Login");

        Button loginButton = (Button) findViewById(R.id.login);
        cancelButton = (Button) findViewById(R.id.cancel_login);
        usernameText = (EditText) findViewById(R.id.userName);
        passwordText = (EditText) findViewById(R.id.password);        

        loginButton.setOnClickListener(new OnClickListener(){
            public void onClick(View arg0) 
            {                   
                if (imService == null) {
                    Toast.makeText(getApplicationContext(),R.string.not_connected_to_service, Toast.LENGTH_LONG).show();
                    //showDialog(NOT_CONNECTED_TO_SERVICE);
                    return;
                }
                else if (imService.isNetworkConnected() == false)
                {
                    Toast.makeText(getApplicationContext(),R.string.not_connected_to_network, Toast.LENGTH_LONG).show();
                    //showDialog(NOT_CONNECTED_TO_NETWORK);

                }
                else if (usernameText.length() > 0 && 
                    passwordText.length() > 0)
                {

                    Thread loginThread = new Thread(){
                        private Handler handler = new Handler();
                        @Override
                        public void run() {
                            String result = null;
                            try {
                                result = imService.authenticateUser(usernameText.getText().toString(), passwordText.getText().toString());
                            } catch (UnsupportedEncodingException e) {

                                e.printStackTrace();
                            }
                            if (result == null || result.equals(AUTHENTICATION_FAILED)) 
                            {

                                handler.post(new Runnable(){
                                    public void run() { 
                                        Toast.makeText(getApplicationContext(),R.string.make_sure_username_and_password_correct, Toast.LENGTH_LONG).show();

                                    }                                   
                                });

                            }
                            else {


                                handler.post(new Runnable(){
                                    public void run() {                                     
                                        Intent i = new Intent(Login.this, FriendList.class);                                                
                                        //i.putExtra(FRIEND_LIST, result);                      
                                        startActivity(i);   
                                        Login.this.finish();
                                    }                                   
                                });

                            }

                        }
                    };
                    loginThread.start();

                }

            }           
        });

        cancelButton.setOnClickListener(new OnClickListener(){

            public void onClick(View arg0) 
            {                   
                imService.exit();
                finish();

            }

        });


    }

    @Override
    protected Dialog onCreateDialog(int id) 
    {       
        int message = -1;       
        switch (id) 
        {
            case NOT_CONNECTED_TO_SERVICE:
                message = R.string.not_connected_to_service;            
                break;
            case FILL_BOTH_USERNAME_AND_PASSWORD:
                message = R.string.fill_both_username_and_password;
                break;
            case MAKE_SURE_USERNAME_AND_PASSWORD_CORRECT:
                message = R.string.make_sure_username_and_password_correct;
                break;
            case NOT_CONNECTED_TO_NETWORK:
                message = R.string.not_connected_to_network;
                break;
            default:
                break;
        }

        if (message == -1) 
        {
            return null;
        }
        else 
        {
            return new AlertDialog.Builder(Login.this)       
            .setMessage(message)
            .setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            })        
            .create();
        }
    }

    @Override
    protected void onPause() 
    {
        unbindService(mConnection);
        super.onPause();
    }

    @Override
    protected void onResume() 
    {       
        bindService(new Intent(Login.this, IMService.class), mConnection , Context.BIND_AUTO_CREATE);

        super.onResume();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {     
        boolean result = super.onCreateOptionsMenu(menu);

         menu.add(0, SIGN_UP_ID, 0, R.string.sign_up);
         menu.add(0, EXIT_APP_ID, 0, R.string.exit_application);


        return result;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {

        switch(item.getItemId()) 
        {
            case SIGN_UP_ID:
                Intent i = new Intent(Login.this, SignUp.class);
                startActivity(i);
                return true;
            case EXIT_APP_ID:
                cancelButton.performClick();
                return true;
        }

        return super.onMenuItemSelected(featureId, item);
    }

}
我在调试时的logcat是这样的

10-11 16:06:39.196: E/ActivityManager(58): ANR in at.vcity.androidim
10-11 16:06:39.196: E/ActivityManager(58): Reason: Executing service at.vcity.androidim/.services.IMService
10-11 16:06:39.196: E/ActivityManager(58): Load: 0.27 / 0.16 / 0.11
10-11 16:06:39.196: E/ActivityManager(58): CPU usage from 39193ms to 16ms ago:
10-11 16:06:39.196: E/ActivityManager(58):   adbd: 2% = 0% user + 2% kernel
10-11 16:06:39.196: E/ActivityManager(58):   vcity.androidim: 1% = 1% user + 0% kernel / faults: 3237 minor 1 major
10-11 16:06:39.196: E/ActivityManager(58):   system_server: 0% = 0% user + 0% kernel / faults: 66 minor
10-11 16:06:39.196: E/ActivityManager(58):   logcat: 0% = 0% user + 0% kernel
10-11 16:06:39.196: E/ActivityManager(58):   m.android.phone: 0% = 0% user + 0% kernel / faults: 40 minor
10-11 16:06:39.196: E/ActivityManager(58):   m.android.email: 0% = 0% user + 0% kernel / faults: 33 minor
10-11 16:06:39.196: E/ActivityManager(58):   ndroid.settings: 0% = 0% user + 0% kernel / faults: 51 minor
10-11 16:06:39.196: E/ActivityManager(58):   zygote: 0% = 0% user + 0% kernel / faults: 24 minor
10-11 16:06:39.196: E/ActivityManager(58):   d.process.acore: 0% = 0% user + 0% kernel / faults: 31 minor
10-11 16:06:39.196: E/ActivityManager(58):   .quicksearchbox: 0% = 0% user + 0% kernel / faults: 29 minor
10-11 16:06:39.196: E/ActivityManager(58):   com.android.mms: 0% = 0% user + 0% kernel / faults: 26 minor
10-11 16:06:39.196: E/ActivityManager(58): TOTAL: 4% = 1% user + 2% kernel + 0% irq + 0% softirq

1 个答案:

答案 0 :(得分:0)

尝试:

@Override
public boolean onCreateOptionsMenu(Menu menu) {     
    getMenuInflater().inflate(R.menu.menu, menu);

    menu.add(0, SIGN_UP_ID, 0, R.string.sign_up);
    menu.add(0, EXIT_APP_ID, 0, R.string.exit_application);
    return true;
}