Search code examples
androidandroid-studiosmsmanager

SMS Manager crash app


i make app using SMSmanager but it's crash and this is my code

  Button button1;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
            button1=(Button)findViewById(R.id.b1);
            button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String SENT = "SMS_SENT";
                String phoneNumber = "0123456789";
                String message = "Hello World!";
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendMultipartTextMessage(phoneNumber, null,message, null, null);
                Toast.makeText(getApplicationContext(),"wow",Toast.LENGTH_LONG).show();
            }
        });
}

I already add the permission to the manifest file

and this is the logcat

01-02 08:05:52.126 4215-4215/? I/art: Not late-enabling -Xcheck:jni (already on)
01-02 08:05:52.248 4215-4215/com.example.humam.blacne W/System: ClassLoader referenced unknown path: /data/app/com.example.humam.blacne-2/lib/x86
01-02 08:05:52.578 4215-4242/com.example.humam.blacne D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
01-02 08:05:52.582 4215-4215/com.example.humam.blacne D/: HostConnection::get() New Host Connection established 0xaa24db20, tid 4215
01-02 08:05:52.638 4215-4242/com.example.humam.blacne D/: HostConnection::get() New Host Connection established 0xaa24dd40, tid 4242
01-02 08:05:52.655 4215-4242/com.example.humam.blacne I/OpenGLRenderer: Initialized EGL, version 1.4
01-02 08:05:52.727 4215-4242/com.example.humam.blacne W/EGL_emulation: eglSurfaceAttrib not implemented
01-02 08:05:52.727 4215-x 4242/com.example.humam.blacne W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabaf9300, error=EGL_SUCCESS

thankfor helping


Solution

  • hi fist of all are you using the simulated second put the send sms in a try catch

          try
                {
                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage(PhoneNumber, null, SmsContent, null, null);
      Toast.makeText(getApplicationContext(),"wow",Toast.LENGTH_LONG).show();
    
                }
                catch(Exception e)
                {
                     Toast.makeText(getApplicationContext(), e.printStackTrace,Toast.LENGTH_LONG).show();
                }
    

    if it is a try catch the app wont crash