在android中进行传出呼叫操作时调用活动时出错

时间:2014-07-09 17:57:04

标签: android

我正在创建一个应用程序,用户将在其中调用输入的号码,当该号码接听电话时,它会自动显示一个屏幕,其中显示了四个导航键。

基本上我试图通过android使用DTMF来运行微控制器。 我不太了解android。请帮我解决这个错误。

这些是我为此应用程序定义的类。

MainActivity.java

import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

final Context context = this;
private Button button;
private EditText edit;
String number;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.call);

    button = (Button) findViewById(R.id.button1);
    edit = (EditText)findViewById(R.id.editText1);
    number = "tel:" + edit.getText().toString().trim();
    // add PhoneStateListener
    PhoneCallListener phoneListener = new PhoneCallListener();
    TelephonyManager telephonyManager = (TelephonyManager) this
        .getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse(number));
            startActivity(callIntent);

        }

    });

}

//monitor phone call activities
private class PhoneCallListener extends PhoneStateListener {

    private boolean isPhoneCalling = false;

    String LOG_TAG = "LOGGING 123";

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {

        if (TelephonyManager.CALL_STATE_RINGING == state) {
            // phone ringing
            Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
        }

        if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
            // active
            Log.i(LOG_TAG, "OFFHOOK");

            isPhoneCalling = true;

            Intent intent = new Intent(context, DtmfActivity.class);
            startActivity(intent);
        }

        if (TelephonyManager.CALL_STATE_IDLE == state) {
            // run when class initial and phone call ended, 
            // need detect flag from CALL_STATE_OFFHOOK
            Log.i(LOG_TAG, "IDLE");

            if (isPhoneCalling) {

                Log.i(LOG_TAG, "restart app");

                // restart app
                Intent i = getBaseContext().getPackageManager()
                    .getLaunchIntentForPackage(
                        getBaseContext().getPackageName());
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);

                isPhoneCalling = false;
            }

        }
    }
  }

}

DtmfActivity.java

import android.app.Activity;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import static android.media.ToneGenerator.TONE_DTMF_2;
import static android.media.ToneGenerator.TONE_DTMF_4;
import static android.media.ToneGenerator.TONE_DTMF_6;
import static android.media.ToneGenerator.TONE_DTMF_8;

public class DtmfActivity extends Activity{

public Button up, dwn, left, right;
static final ToneGenerator _toneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    up = (Button) findViewById(R.id.button1);
    left = (Button) findViewById(R.id.button2);
    right = (Button) findViewById(R.id.button3);
    dwn = (Button) findViewById(R.id.button4);

    up.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            _toneGenerator.startTone(TONE_DTMF_2);
            _toneGenerator.stopTone();
        }
    });

    left.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            _toneGenerator.startTone(TONE_DTMF_4);
            _toneGenerator.stopTone();
        }
    });

    right.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            _toneGenerator.startTone(TONE_DTMF_6);
            _toneGenerator.stopTone();
        }
    });

    dwn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            _toneGenerator.startTone(TONE_DTMF_8);
            _toneGenerator.stopTone();
        }
    });
  }
}

清单

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

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<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" >

    <receiver android:name=".PhoneStateReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE"/>
        </intent-filter>
        </receiver>

    <activity
        android:name="com.ziindia.dtmf.DtmfActivity"
        android:label="@string/app_name" />

    <activity
        android:name="com.project.dtmf.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>
</application>

和logcat详细信息

07-09 13:33:53.990: I/LOGGING 123(1153): OFFHOOK
07-09 13:33:54.200: D/AndroidRuntime(1153): Shutting down VM
07-09 13:33:54.200: W/dalvikvm(1153): threadid=1: thread exiting with uncaught exception (group=0xb3a5dba8)
07-09 13:33:54.270: E/AndroidRuntime(1153): FATAL EXCEPTION: main
07-09 13:33:54.270: E/AndroidRuntime(1153): Process: com.project.dtmf, PID: 1153
07-09 13:33:54.270: E/AndroidRuntime(1153): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.project.dtmf/com.project.dtmf.DtmfActivity}; have you declared this activity in your AndroidManifest.xml?
07-09 13:33:54.270: E/AndroidRuntime(1153):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
07-09 13:33:54.270: E/AndroidRuntime(1153):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
07-09 13:33:54.270: E/AndroidRuntime(1153):     at android.app.Activity.startActivityForResult(Activity.java:3424)
07-09 13:33:54.270: E/AndroidRuntime(1153):     at android.app.Activity.startActivityForResult(Activity.java:3385)
07-09 13:33:54.270: E/AndroidRuntime(1153):     at android.app.Activity.startActivity(Activity.java:3627)
07-09 13:33:54.270: E/AndroidRuntime(1153):     at android.app.Activity.startActivity(Activity.java:3595)
07-09 13:33:54.270: E/AndroidRuntime(1153):     at com.project.dtmf.MainActivity$PhoneCallListener.onCallStateChanged(MainActivity.java:75)
07-09 13:33:54.270: E/AndroidRuntime(1153):     at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:369)
07-09 13:33:54.270: E/AndroidRuntime(1153):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-09 13:33:54.270: E/AndroidRuntime(1153):     at android.os.Looper.loop(Looper.java:136)
07-09 13:33:54.270: E/AndroidRuntime(1153):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-09 13:33:54.270: E/AndroidRuntime(1153):     at java.lang.reflect.Method.invokeNative(Native Method)
07-09 13:33:54.270: E/AndroidRuntime(1153):     at java.lang.reflect.Method.invoke(Method.java:515)
07-09 13:33:54.270: E/AndroidRuntime(1153):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-09 13:33:54.270: E/AndroidRuntime(1153):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-09 13:33:54.270: E/AndroidRuntime(1153):     at dalvik.system.NativeStart.main(Native Method)
07-09 13:33:58.290: I/Process(1153): Sending signal. PID: 1153 SIG: 9

我不明白为什么我会收到此错误 当我用我最好的知识正确地定义了每一件事 有人帮助我 提前谢谢。

2 个答案:

答案 0 :(得分:0)

错误告诉您到底出了什么问题:

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.project.dtmf/com.project.dtmf.DtmfActivity}; have you declared this activity in your AndroidManifest.xml?

您混淆了包名称。看看你的清单:

<activity
    android:name="com.ziindia.dtmf.DtmfActivity"
        android:label="@string/app_name" />
你看到了吗? com.ziindia.dtmf.DtmfActivity应为com.project.dtmf.DtmfActivity

答案 1 :(得分:0)

我不确定你的项目的dierctory结构。从日志中第一眼看来你的调用似乎是无效的意图。 Soit可能是因为在您的清单文件中,两个活动使用不同的 目录结构。确保使用正确的限定名称。

<activity
            android:name="com.**ziindia**.dtmf.DtmfActivity"
            android:label="@string/app_name" />

        <activity
            android:name="com.**project**.dtmf.MainActivity"
            android:label="@string/app_name" >