zoukankan      html  css  js  c++  java
  • Android简单拨号

    package com.example.phonecall;
    
    import android.net.Uri;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            Button btn=(Button)findViewById(R.id.Call);
            btn.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    makeCall();
                }
    
                private void makeCall() {
                    // TODO Auto-generated method stub
                    Log.i("Make call", "");
    
                      Intent phoneIntent = new Intent(Intent.ACTION_CALL);
                      phoneIntent.setData(Uri.parse("tel:10086"));
                      
                      try {
                          startActivity(phoneIntent);
                          finish();
                          Log.i("Finished making a call...", "");
                       } catch (android.content.ActivityNotFoundException ex) {
                          Toast.makeText(MainActivity.this, 
                          "Call faild, please try again later.", Toast.LENGTH_SHORT).show();
                       }
                      
                }
            });
        }
    
        @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;
        }
    
    }
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:orientation="vertical" >
    
       <Button android:id="@+id/Call"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="@string/make_call"/>
        
    </LinearLayout>

    Strings.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <string name="app_name">Phonecall</string>
        <string name="action_settings">Settings</string>
        <string name="hello_world">Hello world!</string>
        <string name="make_call">Call 10086</string>
        
    </resources>

    AndroidManifest.xml中写入权限:

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

    run:

    不努力,还要青春干什么?
  • 相关阅读:
    文件上传Web小案例
    加密方法(MD5加密)
    解决中文乱码(不可能解决不了)
    jquery的一些常见使用方法
    Ajax的作用
    日期时间格式的转换
    前端点击复制内容
    uniapp 移动端防止点击事件穿透
    getCurrentPages 获取当前网页完整的URL
    关闭微信浏览器网页
  • 原文地址:https://www.cnblogs.com/caidupingblogs/p/5446939.html
Copyright © 2011-2022 走看看