PHONE_STATE的Android广播接收器无法正常工作

时间:2013-12-18 09:45:56

标签: android broadcastreceiver

你好朋友我正在研究Android广播接收器,但它无法正常工作。我的广播接收器代码如下所示。

    public class MainActivity extends BroadcastReceiver {

@Override
  public void onReceive(Context arg0, Intent arg1){
     Toast.makeText(arg0, "Service Explicitely", Toast.LENGTH_SHORT).show();  
    arg0.stopService(new Intent(arg0,CallRecordingService.class));
    Intent intent=new Intent(arg0, CallRecordingService.class);
    arg0.startService(intent);
    Toast.makeText(arg0, "Service Explicitely", Toast.LENGTH_SHORT).show();  
   }

并且最明显的文件也是

    <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver android:name="MainActivity">

        <intent-filter>

            <action android:name="android.intent.action.PHONE_STATE" ></action>


        </intent-filter>
  </receiver>
     <service android:name=".CallRecordingService" />
</application>

请解决问题。

1 个答案:

答案 0 :(得分:0)

试试这段代码。当然它会对你有所帮助。它对我来说很好。

public class PhoneStatReceiver extends BroadcastReceiver{        

    private static final String TAG = "PhoneStatReceiver";
    private static boolean incomingFlag = false;
    private static String incoming_number = null;

    @Override
    public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
                    incomingFlag = false;
                    String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 
                    Log.i(TAG, "call OUT:"+phoneNumber);
            }else{
                       TelephonyManager tm = 
                        (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
                    switch (tm.getCallState()) {
                    case TelephonyManager.CALL_STATE_RINGING:
                            incomingFlag = true;
                            incoming_number = intent.getStringExtra("incoming_number");
                            Log.i(TAG, "RINGING :"+ incoming_number);
                            break;
                    case TelephonyManager.CALL_STATE_OFFHOOK:
                            if(incomingFlag){
                Intent callIntent = new Intent(context, RecordService.class);
                context.startService(callIntent);
                                    Log.i(TAG, "incoming ACCEPT :"+ incoming_number);
                            }
                            break;
                    case TelephonyManager.CALL_STATE_IDLE:
                            if(incomingFlag){
                context.stopService(new Intent(context, RecordService.class));
                                    Log.i(TAG, "incoming IDLE");
                            }
                            break;
                    } 
            }
    }
}