android 单击按钮 拨打电话 发短信

时间:2022-11-09 23:16:07
1.private Button btn2;
2.btn2=(Button)findViewById(R.id.btn2);//在xml中把button2改成btn2,为该按钮的id
3.btn2.setOnClickListener(new OnClickListener(){});
4.在OnClick()
    Intent in2 = new Intent();
    in2.setAction(Intent.ACTION_CALL);//指定意图动作
   in2.setData(Uri.parse("tel:1836380000"));//指定电话号码
   startActivity(in2);
5.在android系统中,所有系统请求,必须要在androidmainfest.xml中注册
  在<application上面
<uses-permission android:name="android.permission.CALL_PHONE"/>
MainActivity.java:
package com.example.qq;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

	private Button btn;
	private Button btn2;
    private EditText et;
    private EditText et2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
            

        et = (EditText)findViewById(R.id.editText1);
      

        et2 = (EditText)findViewById(R.id.editText2);
        
        btn2 = (Button)findViewById(R.id.btn2);
        btn = (Button)findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			    final String str = et.getText().toString().trim();
			    final String str2 = et2.getText().toString().trim();
				if (str.equals(str2))
				{
					Intent in = new Intent(MainActivity.this,SecActivity.class);
					startActivity(in);
				}
				else
				{
					System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!");
				}
			}});
        btn2.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent in2 = new Intent();
				in2.setAction(Intent.ACTION_CALL);
				in2.setData(Uri.parse("tel:18363853385"));
				startActivity(in2);
			}});
    }

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}


androidmainfest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.qq"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.qq.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>
        <activity
            android:name="com.example.qq.SecActivity"
            android:label="@string/title_activity_sec" >
        </activity>
    </application>

</manifest>


 同类:

package com.example.qq;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

	private Button btn;
	private Button btn2;
	private Button btn4;
    private EditText et;
    private EditText et2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
            

        et = (EditText)findViewById(R.id.editText1);
      

        et2 = (EditText)findViewById(R.id.editText2);
        
        btn4 = (Button)findViewById(R.id.btn4);
        btn2 = (Button)findViewById(R.id.btn2);
        btn = (Button)findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			    final String str = et.getText().toString().trim();
			    final String str2 = et2.getText().toString().trim();
				if (str.equals(str2))
				{
					Intent in = new Intent(MainActivity.this,SecActivity.class);
					startActivity(in);
				}
				else
				{
					System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!");
				}
			}});
        btn2.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent in2 = new Intent();
				in2.setAction(Intent.ACTION_CALL);
				in2.setData(Uri.parse("tel:18363853385"));
				startActivity(in2);
			}});
        
        btn4.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent in4 = new Intent();
				in4.setAction(Intent.ACTION_SENDTO);
				in4.setData(Uri.parse("smsto:18363850000"));
				in4.putExtra("sms_body", "Hello");
				startActivity(in4);
			}});    }

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}


 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.qq"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.qq.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>
        <activity
            android:name="com.example.qq.SecActivity"
            android:label="@string/title_activity_sec" >
        </activity>
    </application>

</manifest>