Android:无法使用Activity调用BroadcastReceiver类

时间:2013-01-03 10:50:06

标签: android broadcastreceiver

这是我的代码。我无法从主活动类调用我的接收器类。请帮忙。

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>

1 个答案:

答案 0 :(得分: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"));