I
I
ivan8m82016-09-27 11:52:27
Java
ivan8m8, 2016-09-27 11:52:27

How to call Android not from an Activity?

In the RecyclerAdapter class, I handle the button click:

final Button buttonCall = (Button)dialog.findViewById(R.id.buttonCall);
                buttonCall.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            Intent callIntent = new Intent(context, MainActivity.class);
                            callIntent.setAction(Intent.ACTION_CALL);
                            callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            callIntent.setData(Uri.parse("tel:"+911));
                            context.startActivity(callIntent);
                        } catch (ActivityNotFoundException e) {
                            Toast.makeText(context, "Error" + e.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    }
                });

So I try to make a call, but nothing happens.
I believe the problem is with Context
In the same class at the very top:
private final Context context;

    public RecyclerAdapter(List<Item> items, Activity activity, Context context) {
        this.items = items;
        this.activity = activity;
        this.context = context;
    }

In MainActivity:
recyclerAdapter = new RecyclerAdapter(itemArrayList, this, getApplicationContext());

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
davidnum95, 2016-09-27
@ivan8m8

For calls, the application context is not suitable, you need to use the activity context.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question