我正在使用类中的调用函数并从另一个类中使用它

时间:2016-08-08 05:14:16

标签: java android

我想从我的应用程序拨打电话,但是在调用即时收到错误时,所以基本上我创建了一个类,我在其中编写一个函数用于调用,当我从另一个活动调用它时它没有连接,请帮助我在此解决这个问题

这是我的代码

ListWarrenty.java
public class ListWarrenty extends Fragment {
    private ListView lv1;
    private ArrayAdapter<String> adapter;

  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootview = inflater.inflate(R.layout.list_warrenty, container, false);

        ArrayList<String> list = new ArrayList<String>();

        list.add("Ace Brain Systems & Software Pvt Ltd - Acer Laptop Service Center in Pune\n" +
                "1559, SADASHIV PETH,\n" +
                "Phone: 20 - 4335871 /72, 4329730 /31 /32\n" +
                "yogesh@acebrain.com, service@acebrain.com");
        list.add( "Cms Computers Ltd - Acer Laptop Service Center in Pune\n" +
                "Bhosari Complex,Unit No. 24,\n" +
                "stores.pune@cms.com");
        list.add("Computer Home - Acer Laptop Service Center in Pune\n" +
                "Plot No-562,,Africana, Shyamsundar Society Lane,\n" +
                "Phone:  20 - 24327327, 32544448\n" +
                "jsushil@computer-home.com");
        list.add("Glodyne Technoserve Ltd - Acer Laptop Service Center in Pune\n" +
                "Office No.103 &104, Monoplex Plaza,\n" +
                "Mobile:  9520202519");


        list.add("Network Integrators ( India ) Pvt Ltd - Acer Laptop Service Center in Pune\n" +
                "A-15, Ground Floor,Pavan Industrial Estate,\n" +
                "info@niplindia.com");

        list.add("Real Time Services - Acer Laptop Service Center in Pune\n" +
                "Plot No-562,,Africana,\n" +
                "Phone:  20 - 24327327, 32544448\n" +
                "jsushil@computer-home.com");

        list.add("Spicenet India Ltd - Acer Laptop Service Center in Pune\n" +
                "Flat no.5, Bldg no.1, Plot no.285,,New Lakshminarayan\n" +
                "kishore.b@spicelimited.com");

        list.add("Support Technologies - Acer Laptop Service Center in Pune\n" +
                "Purva Plaza, Flat# 3,515,\n" +
                "Phone:  20 - 24431608, 56012893\n" +
                "supporttech@iqara.net");


        MyCustomAdapter adapter = new MyCustomAdapter(list, getActivity());

        //handle listview and assign adapter

        lv1= (ListView)rootview.findViewById(R.id.listView);

       // ListView lView = (ListView)findViewById(R.id.my_listview);
        lv1.setAdapter(adapter);
  return rootview;
    }

}

这是我的适配器类

MyCustomAdapter.java

public class MyCustomAdapter extends BaseAdapter implements ListAdapter {
    private ArrayList<String> list = new ArrayList<String>();
    private Context context;
    TextView listItemText1;
    TextView listItemText;
    Button emailBtn,callButton;

    public MyCustomAdapter(ArrayList<String> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int pos) {
        return list.get(pos);
    }

    @Override
    public long getItemId(int pos) {
     //   return list.get(pos).getId();
        return 0;
        //just return 0 if your list items do not have an Id variable.
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.custom, null);
        }
        //Handle TextView and display string from your list

        listItemText = (TextView)view.findViewById(R.id.list_item_string);
        listItemText.setText(list.get(position));

//         listItemText1 = (TextView)view.findViewById(R.id.list_item);
//        listItemText1.setText(list.get(position));

        //Handle buttons and add onClickListeners
        emailBtn = (Button)view.findViewById(R.id.delete_btn);

        callButton = (Button)view.findViewById(R.id.add_btn);

        emailBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {

                Email();
//                //do something
//                list.remove(position); //or some other task
//                notifyDataSetChanged();
            }
        });
        callButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {

                call();
            }
        });
        return view;
    }

    private void call() {

        try{
            String url2 = "0000000000";
            Intent in=new Intent(Intent.ACTION_CALL,Uri.parse(url2));
            context.startActivity(in);

        }
        catch (Exception ex){
            Toast.makeText(context,"Your call is not get connected",Toast.LENGTH_SHORT).show();
        }
    }
    private void Email() {
        Toast toast = Toast.makeText(context, "Your Mail has been sent successfully", Toast.LENGTH_LONG);
        toast.show();
        //Toast.makeText("Your Activity is not founded", Toast.LENGTH_SHORT).show();
    }
}

2 个答案:

答案 0 :(得分:0)

你传递的URI是错误的,这就是为什么它不能发出任何电话。做这样的事情:

 String uri = "tel:00000000" ;
 Intent intent = new Intent(Intent.ACTION_DIAL);
 intent.setData(Uri.parse(uri));
 startActivity(intent);

这将为您打开拨号器,您可以按通话......这不需要任何权限....

如果你想直接尝试呼叫,你需要在清单中使用CALL权限,如果设备高于6.0 android,你也可能需要在运行时请求权限 - &gt; android.permission.CALL_PHONE

获得权限后,请执行以下操作:

 String uri = "tel:00000000" ;
 Intent intent = new Intent(Intent.ACTION_CALL);
 intent.setData(Uri.parse(uri));
 startActivity(intent);

确保在try块中调用它,因为任何人都可能拒绝该权限。

答案 1 :(得分:0)

在清单中添加权限

<uses-permission android:name="android.permission.CALL_PHONE" />

并在您的活动中添加此代码

String uri = "tel:"+number      //in your case number = "0000000000";
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
startActivity(callIntent);

或者您可以像这样使用。呼叫号码的方法都是正确的。

Intent callIntent = new Intent(Intent.ACTION_CALL);  
callIntent.setData(Uri.parse("tel:"+8802177690));   //change the number  
startActivity(callIntent);