Search code examples
androidandroid-serviceandroid-alertdialogandroid-dialogfragment

Starting service from AlertDialog in onClick. Sometime service is not starting


I have follow class for showing message from the social website:

public class MessageDialog extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    final LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.message_dialog, null);

    builder.setView(view)
            .setNeutralButton("Send", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(getActivity().getApplicationContext(), RequestService.class);
                    //intent.putExtra("data", some_extra_data);
                    getActivity().startService(intent);
                }
            })
            .setPositiveButton("GoTo", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //GoTo();
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                }
            });

    return builder.create();
}

@Override
public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);
    if(getActivity() != null)
    getActivity().finish();
}

}

Above class is simplified, of course. This message showing from FragmentActivity that starting from background service. I found that in some cases buttons of AlertDialog (message dialog) are not execute code inside onClick. For example, service RequestService.class is not starting only the next day and only for first time. After showing dialog again it is working fine.

FragmentActivity class where from AlertDialog is showing:

public class MyFragmentDialog extends FragmentActivity {

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

    FragmentManager fm = getSupportFragmentManager();

    Intent intent = getIntent();
    Bundle extras = intent.getExtras();

    switch (extras.getInt("action", 0))
    {
        case SHOW_MSG_DIALOG:
            // 1. Message case
            String json = extras.getString("data");

            if(json != null)
            {
                try {
                    JSONObject mail = new JSONObject(json);

                    MessageDialog msgDialog = new MessageDialog();
                    msgDialog.setData(mail);
                    msgDialog.show(fm, "msgDialog");

                } catch (JSONException e) {
                    e.printStackTrace();
                    finish();
                }
            }
            else
            finish();

        break;

        // 2. case
        // ...

        // 3. case
        // ...

        case 0:
            // exit default
        break;
    }

}
}

Have I missing something?

SOLVED: Bug was in a AppCompatEditText that broken my json string. Android... a peace of shirt)


Solution

  • OK, after many tries I'm found why my ServerRequestService doesn't want to work. Base64 encoded string was sent as part of URL as parameter. If I send string containing cyrillic symbols it's broken Base64 and JSON sended to the server now is not correct. Solution was use URLEncoder.encode(Base64.encodeToString(data, Base64.NO_WRAP),"UTF-8"); to encode URL string parameter to avoid corrupted data. Thanks to all!