2013-01-03 80 views
1

這是我的代碼。我無法從主要活動類中調用我的接收者類。請幫忙。Android:無法使用Activity調用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> 
+0

無需調用'BroadcastReceiver'因爲這時候電話狀態改變,你只需要在正確的方式進行註冊火災自動。 'startActivity'用於表示Activity而不是'BroadcastReceiver' –

回答

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")); 
相關問題