我的机器人应用程序在主页面上有一个按钮,为您提供一个对话弹出页面,并在对话框页面中询问您是否要拨打电话
我已经尝试了一遍又一遍,并且还没有弄明白如何让按钮拨打电话
我打开的java页面 打包your.package2.phonecalls;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ButtonphonecallsActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button TheCallButton = (Button) findViewById(R.id.phonecallbutton);
TheCallButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final Dialog dialog = new Dialog(ButtonphonecallsActivity.this);
dialog.setContentView(R.layout.alertphonecall);
dialog.setTitle("Zohan contact number");
Button Nosir = (Button) dialog.findViewById(R.id.nobutton);
Nosir.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.cancel();
}
});
Button Yessir = (Button) dialog.findViewById(R.id.yesbutton);
Yessir.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.cancel();
startActivity(new Intent(ButtonphonecallsActivity.this, makingaphonecall.class));
}
});
dialog.show();
}
});
}
}
电话活动
package your.package2.phonecalls;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
public class makingaphonecall extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
call();
}
private void call() {
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:7578263678"));
startActivity(callIntent);
} catch (ActivityNotFoundException activityException) {
Log.e("helloandroid dialing example", "Call failed");
}
}
}
android manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package2.phonecalls"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:debuggable="true" >
<activity
android:name=".ButtonphonecallsActivity"
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=".makeaphonecall"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
答案 0 :(得分:1)
为什么要进行单独的活动来拨打电话?只需将以下两行添加到您的onclick中。
Intent dialer = new Intent(Intent.ACTION_CALL,Uri.parse("tel:7578263678"));
startActivity(dialer);
答案 1 :(得分:0)
Button b = (Button) this.findViewById(R.id.callButton);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_CALL);
i.setData(Uri.parse("tel:123456"));
CallTestAndroidActivity.this.startActivity(i);
}
});
这应该可以正常工作。清单就像你拥有它一样好
希望这有帮助。