Android broadcast receiver for PHONE_STATE is not

2019-09-16 02:52发布

hello friends i am working on android broadcast receiver but it is not working. My code for broadcast receiver is given below.

    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();  
   }

and the manifiest file is also given

    <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>

please solve the problem.

1条回答
看我几分像从前
2楼-- · 2019-09-16 03:54

Try this code. Sure it will help for you. Its working fine for me.

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;
                    } 
            }
    }
}
查看更多
登录 后发表回答