How do I request permission to make a call in Android fragment?

Asked 1 years ago, Updated 1 years ago, 88 views

Startactivityf is created in the fragment while the manifest file is requested for permission I tried running it, but the app stopped when I called. Why is that? Below is the code for manifest and fragment.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lg.termpj3" >
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
public class KeypadFragment extends Fragment {
    String input_num="";
    Button btn_shop,btn_star;
    ImageButton btnCall,btn0,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btnAdd,btn1,btn2,btnErase;
    Button[] numbuttons =new Button[12];
    Integer[] numBtnId={R.id.button_0,R.id.button_1,R.id.button_2,R.id.button_3,R.id.button_4,R.id.button_5,R.id.button_6,R.id.button_7,
            R.id.button_8,R.id.button_9,R.id.button_star,R.id.button_shop};
    TextView number;
    int i;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle saveInstanceState){
        View view=inflater.inflate(R.layout.fragment_keypad,container,false);
        btnCall = (ImageButton)view.findViewById(R.id.button_call);
        btnAdd = (ImageButton) view.findViewById(R.id.button_add);
        btnErase = (ImageButton) view.findViewById(R.id.button_erase);
        number = (TextView) view.findViewById(R.id.textView_input_number);
        btn0=(ImageButton)view.findViewById(R.id.button_0);
        btn1=(ImageButton)view.findViewById(R.id.button_1);
        btn2=(ImageButton)view.findViewById(R.id.button_2);
        btn3=(ImageButton)view.findViewById(R.id.button_3);
        btn4=(ImageButton)view.findViewById(R.id.button_4);
        btn5=(ImageButton)view.findViewById(R.id.button_5);
        btn6=(ImageButton)view.findViewById(R.id.button_6);
        btn7=(ImageButton)view.findViewById(R.id.button_7);
        btn8=(ImageButton)view.findViewById(R.id.button_8);
        btn9=(ImageButton)view.findViewById(R.id.button_9);
        btn_star=(Button)view.findViewById(R.id.button_star);
        btn_shop=(Button)view.findViewById(R.id.button_shop);



    //Optimized
        btnCall.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                if (input_num.length()!=0){
                    String tel="tel:"+input_num;
                    startActivity(new Intent("android.intent.action.CALL", Uri.parse(tel)));
                }
            }
        });


    //Optimized  

        return view;
    }

}

android intent

2022-09-22 20:12

1 Answers

Who is the marshmallow version? Then, the code should allow the user to grant permission.


if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE, Manifest.permission.SEND_SMS, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
        }

Like this!


2022-09-22 20:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.