【发布时间】:2014-03-25 11:28:22
【问题描述】:
我有这个非常基本的代码,应该可以用作呼叫应用程序。 一切都是硬编码的。
根据我需要/想要创建的这个应用程序,我没有找到有用的教程。
我的问题很大!谁能帮我创建一个 CallApplication
要求我觉得很简单
需要能够拨打号码
这是我目前拥有的代码,据说它非常基本,但我被卡住了^^
非常感谢任何帮助!
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
final Context context = this;
private Button btn;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
PhoneCallListener phoneCallListener = new PhoneCallListener();
TelephonyManager telManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
telManager.listen(phoneCallListener, PhoneStateListener.LISTEN_CALL_STATE);
// add button listener
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse("tel:123456"));
startActivity(phoneCallIntent);
}
});
}
// monitor phone call states
private class PhoneCallListener extends PhoneStateListener {
String TAG = "LOGGING PHONE CALL";
private boolean phoneCalling = false;
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (TelephonyManager.CALL_STATE_RINGING == state) {
// phone ringing
Log.i(TAG, "RINGING, number: " + incomingNumber);
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
// active
Log.i(TAG, "OFFHOOK");
phoneCalling = true;
}
// When the call ends launch the main activity again
if (TelephonyManager.CALL_STATE_IDLE == state) {
Log.i(TAG, "IDLE");
if (phoneCalling) {
Log.i(TAG, "restart app");
// restart app
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(
getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
phoneCalling = false;
}
}
}
}
}
【问题讨论】:
-
就像你在代码中的某个点...我得到了一个硬编码的联系人。 ---phoneCallIntent.setData(Uri.parse("tel:123456"));--- 这必须是动态的......我的意思是......而不是硬编码......我需要能够拨号一个号码,然后拨打我已拨打的号码……你看到了吗? xD ...