如何使用 Android 中的 intent 拨打电话?
此示例阐述如何通过编程锁定 Android 设备。
步骤 1 − 在 Android Studio 中创建新项目,转到文件 ⇒ 新项目,填写所有必需详细信息创建新项目。
步骤 2 − 将以下代码添加到 res/layout/activity_main.xml
<? xml version= "1.0" encoding= "utf-8" ?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android :layout_width= "match_parent" android :layout_height= "match_parent" android :layout_margin= "16dp" tools :context= ".MainActivity" > <Button android :onClick= "call" android :layout_width= "match_parent" android :layout_height= "wrap_content" android :text= "Call" /> </RelativeLayout>
步骤 3 − 将以下代码添加到 src/MainActivity
package app.tutorialspoint.com.sample ;
import android.content.Intent ;
import android.net.Uri ;
import android.os.Bundle ;
import android.support.v7.app.AppCompatActivity ;
import android.view.View ;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate (Bundle savedInstanceState) {
super .onCreate(savedInstanceState) ;
setContentView(R.layout. activity_main ) ;
}
public void call (View view) {
Intent dialIntent = new Intent(Intent. ACTION_DIAL ) ;
dialIntent.setData(Uri. parse ( "tel:" + "8072223081" )) ;
startActivity(dialIntent) ;
}
}步骤 4 − 将以下代码添加到 androidManifest.xml
<? xml version= "1.0" encoding= "utf-8" ?> <manifest xmlns: android = "http://schemas.android.com/apk/res/android" package= "app.tutorialspoint.com.sample" > <uses-permission android :name= "android.permission.CALL_PHONE" /> <application android :allowBackup= "true" android :icon= "@mipmap/ic_launcher" android :label= "@string/app_name" android :roundIcon= "@mipmap/ic_launcher_round" android :supportsRtl= "true" android :theme= "@style/AppTheme" > <activity android :name= ".MainActivity" > <intent-filter> <action android :name= "android.intent.action.MAIN" /> <category android :name= "android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
让我们试着运行你的应用程序。我假定你已将你实际的 Android 移动设备连接到你的计算机。从 android studio 运行应用程序,打开一个项目活动文件,然后单击工具栏中的运行
图标。选择你的移动设备作为选项,然后检查你的移动设备,它将显示你的默认屏幕 –

广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP