Dialing a phone call on click of textview in android

后端 未结 6 843
醉话见心
醉话见心 2020-12-03 01:05

I have a question that I want to dial a phone call when I click on a text view which also contain a phone number. But when I click on a Text view it returns an error as:

相关标签:
6条回答
  • 2020-12-03 01:43
     import android.app.Activity;
        import android.content.Intent;
        import android.net.Uri;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.Button;
    
         public class CustomCellActivity extends Activity {
              
    
         /** Called when the activity is first created. */
                         
    
          @Override
              
    
         public void onCreate(Bundle savedInstanceState) {
                                
    
         super.onCreate(savedInstanceState);
            
    
         setContentView(R.layout.main);
    
    
               Button Phone = (Button) findViewById(R.id.button1);
               
    
         Phone.setOnClickListener(new View.OnClickListener() {
           @Override
          
    
         public void onClick(View v) {
       
    
         Intent sIntent = new Intent(Intent.ACTION_CALL, Uri
         
    
         .parse("tel:12345"));
       
    
         sIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       
        
    
         startActivity(sIntent);
      
    
        }
      
    
        });
    

    This code works for me. try it urself.

    0 讨论(0)
  • 2020-12-03 01:45
          TextVIew dialers=(TextView)this.findViewById(R.id.dialer);
          dialers.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view) 
            {
                            launchDialer(dialers.getText().toString());
                        }
    
           });
    
    0 讨论(0)
  • 2020-12-03 01:49

    Change it to :callIntent.setData(Uri.parse("tel:"+phone_no));

    and i didnt add this: callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    but its working for me

    0 讨论(0)
  • 2020-12-03 01:51

    Add this attribute to your textview component within the XML layout file like this:

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="phone"
        android:text="123 123 1234" />
    
    0 讨论(0)
  • 2020-12-03 01:51

    this work for me :)

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:autoLink="phone" android:background="#ADD8E6" android:text="555-468-0602" />

    0 讨论(0)
  • 2020-12-03 02:05

    Add permission to call in AndroidManifest.xml file.

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

    In your textview of your layout.xml file

    <TextView
            android:clickable="true"
            android:autoLink="phone"
            ...
            ...
            .../>
    

    In your activity class. Run-time permission check added.

    if (Build.VERSION.SDK_INT > 22) {
    
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
    
                    ActivityCompat.requestPermissions(MoreProgramDetailActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 101);
    
                    return;
                }
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:+" + phoneTV.getText().toString().trim()));
                startActivity(callIntent);
     } else {
    
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:+" + phoneTV.getText().toString().trim()));
                startActivity(callIntent);
     }
    
    0 讨论(0)
提交回复
热议问题