使用Fragment从我的应用程序发送消息

时间:2019-07-03 13:51:57

标签: android

我正在从我的应用程序中以片段形式发送短信,我有代码可以在Activity中正常工作,但不能以片段形式工作。

    //---when the SMS has been sent---

    `getActivity().registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getContext(), "SMS sent",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getContext(), "Generic failure",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getContext(), "No service",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getContext(), "Null PDU",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getContext(), "Radio off",
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    },new IntentFilter(SENT));`

    //---when the SMS has been delivered---

    getActivity().registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getContext(), "SMS delivered",
                            Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getContext(), "SMS not delivered",
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }, new IntentFilter(DELIVERED));

    SmsManager sms = SmsManager.getDefault();

    ///sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
    SmsManager smsMgr = SmsManager.getDefault();
    SmsManager sm = SmsManager.getDefault();
    ArrayList<String> parts =sm.divideMessage("You are assigning  to new booth with booth Number"+boothNumber.getText().toString()+"having username"+contactNumber.getText().toString()+",password"+contactNumber.getText().toString()+"@123");
    int numParts = parts.size();

    ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
    ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();

    for (int i = 0; i < numParts; i++) {

        sentIntents.add(PendingIntent.getBroadcast(getContext(), 0,
                new Intent(SENT), 0));

        deliveryIntents.add(PendingIntent.getBroadcast(getContext(), 0,
                new Intent(DELIVERED), 0));
    }

    if (ContextCompat.checkSelfPermission(getContext(),
            Manifest.permission.SEND_SMS)
            != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
                Manifest.permission.SEND_SMS)) {
        } else {
            ActivityCompat.requestPermissions(getActivity(),
                    new String[]{Manifest.permission.SEND_SMS},
                    1);
        }
    }else{         //already has permission granted
        //SmsManager smsManager = SmsManager.getDefault();
        if(contactNumber.getText().toString().length()!=0) {
            //CustomerList customerList=allCustomerList.get(i);
            //Log.e("cs",customerList.getMobileNumber().toString());
            sms.sendMultipartTextMessage(contactNumber.getText().toString(), null, parts, sentIntents, deliveryIntents);

        }
        // smsManager.sendTextMessage(phonenumber,null,smsbody, null, null);
        Toast.makeText(getApplicationContext(), "SMS sent.",
                Toast.LENGTH_LONG).show();

    }
  

java.lang.SecurityException:用户10248和当前进程都没有android.permission.READ_PHONE_STATE。   在android.telephony.SmsManager.sendMultipartTextMessage(SmsManager.java:623)

1 个答案:

答案 0 :(得分:0)

根据the docs,仅使用您需要的SEND_SMS权限。但是,也许是您处理权限的原因。

您的请求正在使用您的活动发送。您需要从Fragment发送这些。为此,请删除ActivityCompat并替换为FragmentCompat(您还需要换出getActivity())。

完成此操作后,还请确保在您的父母Activity中正在呼叫super.onActivityResultsuper.onRequestPermissionResult,否则您的Fragment将不会得到他们。

现在,您无需在Activity中处理权限请求,而需要在Fragment中处理它们