1

これが私のコードです。メインアクティビティクラスからレシーバークラスを呼び出すことができません。助けてください。

public class RecorderActivity extends Activity {           

@Override       
public void onCreate(Bundle savedInstanceState) {           
    super.onCreate(savedInstanceState);           
    setContentView(R.layout.main);                      

    Intent i = new Intent(this, CallRecorder.class);
    startActivity(i);
    } 
}

   /////////////////////////////////////

 class CallRecorder extends BroadcastReceiver {

private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";       
private MediaRecorder recorder = null;
boolean recordstarted = false;
String phonenbr = null;
TelephonyManager telManager;

@Override
public void onReceive(Context context, Intent intent) {

    // TODO Auto-generated method stub
    if (intent.getAction().equalsIgnoreCase(Intent.ACTION_ANSWER)) 
    {
        try
        {
            phonenbr = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            Log.v("GDHGDHGHDGDHGDHGHDGHDGH", phonenbr);
            recorder = new MediaRecorder();
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);                   
            recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);                   
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);                   
            recorder.setOutputFile(getFilename());
            recorder.prepare();
            recorder.start();
            recordstarted = true;
            telManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        final PhoneStateListener phoneListener = new PhoneStateListener()
        {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                try {
                    switch (state) {
                        case TelephonyManager.CALL_STATE_RINGING: {
                            // 
                            break;
                        }
                        case TelephonyManager.CALL_STATE_OFFHOOK: {
                            // 
                            break;
                        }
                        case TelephonyManager.CALL_STATE_IDLE: {
                            if (recordstarted) {
                                recorder.stop();
                                recordstarted = false;
                            }
                            break;
                        }
                        default: { }
                    }
                } catch (Exception ex) {
                }
            } 
        };

        telManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
    }
}
private String getFilename(){                  
    String filepath = Environment.getExternalStorageDirectory().getPath();  
    File file = new File(filepath,AUDIO_RECORDER_FOLDER);                    
    if(!file.exists()){                          
        file.mkdirs();                  
    }

    return (file.getAbsolutePath() + "/" + phonenbr + System.currentTimeMillis() + ".mp4");   
}
}

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

    <receiver android:name=".CallRecorder" android:enabled="true">
<intent-filter android:priority="1000">
    <action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>

    <activity
        android:name=".RecorderActivity"
        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>
4

1 に答える 1

3
BroadcastReceiver receiver = new CallRecorder();

Youractivity.onStart() で:

IntentFilter intentFilter = new ItentFilter("some_string-to_call_receiver");
registerReceiver(receiver, intentFilter);

そして Youractivity.onStop() で:

uregisterReceiver(receiver);

どこでも受信機を呼び出します。

sendBroadcast(new Intent("some_string-to_call_receiver"));
于 2013-01-03T11:01:40.187 に答える