SmsManger.getDefault();无法解决错误

时间:2016-05-20 12:35:41

标签: android

我的代码中存在错误,例如SmsManger.getDefault(); cannot be resolved错误

  Button b2 = (Button)findViewById(R.id.btn1); 
            b2.setOnClickListener(new View.OnClickListener() {          
                @Override
                public void onClick(View v) {
                    Log.i("Send Sms", null);
                    String phone="0899786592";
                    String message="Rescue Me: /n I am at Lattitude- "+lat+" ,Long- "+lon;
                    SmsManager smsManager =new SmsManager.getDefault()
//getting error in the above line i also tried without type casting.. works but the smsManager is getting an error
                    try{
                        smsManager.sendTextMessage(phone,null,message, null, null);
                        Toast.makeText(getApplicationContext(),"Sms Sent", Toast.LENGTH_SHORT).show();
                    }catch(Exception e){
                        Toast.makeText(getApplicationContext(), "Sms not sent!", Toast.LENGTH_SHORT).show();
                        e.printStackTrace();
                    }
                }
            });
        }

1 个答案:

答案 0 :(得分:0)

您无法实例化SmsManager,它是一个Singleton并且具有私有构造函数。这就是您使用new SmsManager.getDefault()收到错误的原因。

要获取实例,您只需要调用静态方法:getDefault

SmsManager smsManager = SmsManager.getDefault();