从onClick中的AlertDialog启动服务。有时服务没有启动

时间:2016-05-30 10:03:00

标签: android android-service android-alertdialog android-dialogfragment

我已按照课程显示来自社交网站的消息:

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();
}

}

当然,上面的课程是简化的。此消息显示从FragmentActivity开始,从后台服务开始。 我发现在某些情况下AlertDialog(消息对话框)的按钮不是onClick内的执行代码。例如,服务RequestService.class不是仅在第二天启动,而是仅在第一次启动。再次显示对话框后,它正常工作。

FragmentActivity类,其中显示AlertDialog:

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;
    }

}
}

我遗失了什么吗?

已解决:错误是在AppCompatEditText中破坏了我的json字符串。 Android ......和平的衬衫)

2 个答案:

答案 0 :(得分:0)

试试这个,

在Dialog Fragment中创建一个构造函数。

public class MessageDialog extends DialogFragment {

  private Activity mActivity;
     public MessageDialog(Activity mActivity) {
            this.mActivity = mActivity;

        }

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
    final LayoutInflater inflater = mActivity.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(mActivity, RequestService.class);
                    //intent.putExtra("data", some_extra_data);
                    mActivity.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(mActivity != null)
    mActivity.finish();
}

打电话给碎片。

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

答案 1 :(得分:0)

好的,经过多次尝试,我发现为什么我的ServerRequestService不想工作。 Base64编码的字符串作为参数的URL的一部分发送。如果我发送包含西里尔符号的字符串,那么它已经破坏了Base64和JSON发送到服务器现在是不正确的。解决方法是使用URLEncoder.encode(Base64.encodeToString(data, Base64.NO_WRAP),"UTF-8");对URL字符串参数进行编码以避免数据损坏。谢谢大家!