0

私のアプリでは1つのことを追加しようとしています。私が何をしようとしているのは、ユーザーが電話をかけるとき、またはユーザーが電話をかけたとき、同時に録音を開始し、録音を切った後にSDカードに録音する必要があるときです。発信録音されていない発信通話

public class MainActivity extends AppCompatActivity { 

    private static final int REQUEST_CODE = 0; 
    private DevicePolicyManager mDPM; 
    private ComponentName mAdminName; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     PackageManager p = getPackageManager(); 
     p.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); 

     try { 
      // Initiate DevicePolicyManager. 
      mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); 
      mAdminName = new ComponentName(this, DeviceAdminDemo.class); 

      if (!mDPM.isAdminActive(mAdminName)) { 
       Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); 
       intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName); 
       intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Click on Activate button to secure your application."); 
       startActivityForResult(intent, REQUEST_CODE); 
      } else { 
       // mDPM.lockNow(); 
       // Intent intent = new Intent(MainActivity.this, 
       // TrackDeviceService.class); 
       // startService(intent); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     if (REQUEST_CODE == requestCode) { 
      Intent intent = new Intent(MainActivity.this, TService.class); 
      startService(intent); 
     } 
    } 

} 

TService

public class TService extends Service { 
    MediaRecorder recorder; 
    File audiofile; 
    String name, phonenumber; 
    String audio_format; 
    public String Audio_Type; 
    int audioSource; 
    Context context; 

    Timer timer; 
    Boolean offHook = false, ringing = false; 
    Toast toast; 
    Boolean isOffHook = false; 
    private boolean recordstarted = false; 

    private static final String ACTION_IN = "android.intent.action.PHONE_STATE"; 
    private static final String ACTION_OUT = "android.intent.action.NEW_OUTGOING_CALL"; 
    private CallBr br_call; 




    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void onDestroy() { 
     Log.d("service", "destroy"); 

     super.onDestroy(); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     // final String terminate =(String) 
     // intent.getExtras().get("terminate");// 
     // intent.getStringExtra("terminate"); 
     // Log.d("TAG", "service started"); 
     // 
     // TelephonyManager telephony = (TelephonyManager) 
     // getSystemService(Context.TELEPHONY_SERVICE); // TelephonyManager 
     // // object 
     // CustomPhoneStateListener customPhoneListener = new 
     // CustomPhoneStateListener(); 
     // telephony.listen(customPhoneListener, 
     // PhoneStateListener.LISTEN_CALL_STATE); 
     // context = getApplicationContext(); 

     final IntentFilter filter = new IntentFilter(); 
     filter.addAction(ACTION_OUT); 
     filter.addAction(ACTION_IN); 
     this.br_call = new CallBr(); 
     this.registerReceiver(this.br_call, filter); 

     // if(terminate != null) { 
     // stopSelf(); 
     // } 
     return START_NOT_STICKY; 
    } 

    public class CallBr extends BroadcastReceiver { 
     Bundle bundle; 
     String state; 
     String inCall, outCall; 
     public boolean wasRinging = false; 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      if (intent.getAction().equals(ACTION_IN)) { 
       if ((bundle = intent.getExtras()) != null) { 
        state = bundle.getString(TelephonyManager.EXTRA_STATE); 
        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 
         inCall = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER); 
         wasRinging = true; 
         Toast.makeText(context, "IN : " + inCall, Toast.LENGTH_LONG).show(); 
        } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) { 
         if (wasRinging == true) { 

          Toast.makeText(context, "ANSWERED", Toast.LENGTH_LONG).show(); 

          String out = new SimpleDateFormat("dd-MM-yyyy hh-mm-ss").format(new Date()); 
          File sampleDir = new File(Environment.getExternalStorageDirectory(), "/TestRecordingDasa1"); 
          if (!sampleDir.exists()) { 
           sampleDir.mkdirs(); 
          } 
          String file_name = "Record"; 
          try { 
           audiofile = File.createTempFile(file_name, ".amr", sampleDir); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
          String path = Environment.getExternalStorageDirectory().getAbsolutePath(); 
          System.out.println("PATH"+path); 

          recorder = new MediaRecorder(); 
//       recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL); 

          recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION); 
          recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB); 
          recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
          recorder.setOutputFile(audiofile.getAbsolutePath()); 
          try { 
           recorder.prepare(); 
          } catch (IllegalStateException e) { 
           e.printStackTrace(); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
          recorder.start(); 
          recordstarted = true; 
         } 
        } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) { 
         wasRinging = false; 
         Toast.makeText(context, "REJECT || DISCO", Toast.LENGTH_LONG).show(); 
         if (recordstarted) { 
          recorder.stop(); 
          recordstarted = false; 
         } 
        } 
       } 
      } else if (intent.getAction().equals(ACTION_OUT)) { 
       if ((bundle = intent.getExtras()) != null) { 
        outCall = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 
        Toast.makeText(context, "OUT : " + outCall, Toast.LENGTH_LONG).show(); 
       } 
      } 
     } 
    } 

} 

答えて

2

MainActivity.javaを働いていない

問題時々
1)着信履歴の作業

2)TServiceからこのif (wasRinging == true)を削除します。このwasRiningブール値は、電話機が以前に呼び出し音を鳴らしていたかどうか、つまり電話が着信したかどうかをチェックします。それを変えればうまくいくはずです。

ただ、これであなたのTServiceのコードを置き換える..

public class TService extends Service { 
    MediaRecorder recorder; 
    File audiofile; 
    String name, phonenumber; 
    String audio_format; 
    public String Audio_Type; 
    int audioSource; 
    Context context; 

    Timer timer; 
    Boolean offHook = false, ringing = false; 
    Toast toast; 
    Boolean isOffHook = false; 
    private boolean recordstarted = false; 

    private static final String ACTION_IN = "android.intent.action.PHONE_STATE"; 
    private static final String ACTION_OUT = "android.intent.action.NEW_OUTGOING_CALL"; 
    private CallBr br_call; 




    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void onDestroy() { 
     Log.d("service", "destroy"); 

     super.onDestroy(); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     // final String terminate =(String) 
     // intent.getExtras().get("terminate");// 
     // intent.getStringExtra("terminate"); 
     // Log.d("TAG", "service started"); 
     // 
     // TelephonyManager telephony = (TelephonyManager) 
     // getSystemService(Context.TELEPHONY_SERVICE); // TelephonyManager 
     // // object 
     // CustomPhoneStateListener customPhoneListener = new 
     // CustomPhoneStateListener(); 
     // telephony.listen(customPhoneListener, 
     // PhoneStateListener.LISTEN_CALL_STATE); 
     // context = getApplicationContext(); 

     final IntentFilter filter = new IntentFilter(); 
     filter.addAction(ACTION_OUT); 
     filter.addAction(ACTION_IN); 
     this.br_call = new CallBr(); 
     this.registerReceiver(this.br_call, filter); 

     // if(terminate != null) { 
     // stopSelf(); 
     // } 
     return START_NOT_STICKY; 
    } 

    public class CallBr extends BroadcastReceiver { 
     Bundle bundle; 
     String state; 
     String inCall, outCall; 
     public boolean wasRinging = false; 
     public boolean didMakeACall = false; 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      if (intent.getAction().equals(ACTION_IN)) { 
       if ((bundle = intent.getExtras()) != null) { 
        state = bundle.getString(TelephonyManager.EXTRA_STATE); 
        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 
         inCall = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER); 
         wasRinging = true; 
         Toast.makeText(context, "IN : " + inCall, Toast.LENGTH_LONG).show(); 
        } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) { 
         if (wasRinging || didMakeACall) { 

          Toast.makeText(context, "ANSWERED", Toast.LENGTH_LONG).show(); 

          String out = new SimpleDateFormat("dd-MM-yyyy hh-mm-ss").format(new Date()); 
          File sampleDir = new File(Environment.getExternalStorageDirectory(), "/TestRecordingDasa1"); 
          if (!sampleDir.exists()) { 
           sampleDir.mkdirs(); 
          } 
          String file_name = "Record"; 
          try { 
           audiofile = File.createTempFile(file_name, ".amr", sampleDir); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
          String path = Environment.getExternalStorageDirectory().getAbsolutePath(); 
          System.out.println("PATH"+path); 

          recorder = new MediaRecorder(); 


          recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION); 
          recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB); 
          recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
          recorder.setOutputFile(audiofile.getAbsolutePath()); 
          try { 
           recorder.prepare(); 
          } catch (IllegalStateException e) { 
           e.printStackTrace(); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
          recorder.start(); 
          recordstarted = true; 
         } 
        } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) { 
         wasRinging = false; 
         Toast.makeText(context, "REJECT || DISCO", Toast.LENGTH_LONG).show(); 
         if (recordstarted) { 
          recorder.stop(); 
          recordstarted = false; 
         } 
        } 
       } 
      } else if (intent.getAction().equals(ACTION_OUT)) { 
       if ((bundle = intent.getExtras()) != null) { 
        didMakeACall = true; 
        outCall = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 
        Toast.makeText(context, "OUT : " + outCall, Toast.LENGTH_LONG).show(); 
       } 
      } 
     } 
    } 

} 
+0

私は – chris

+0

英語say..sorryものを手に入れるくぼみ私はちょうど私のしてあなたのTServiceコードを置き換える、私の答えをedittedました。 –

+0

...あなたは私があなたのTServiceコードを修正してみましょう、あなたはそれを得るでしょう –

0

私はここに私の迅速なデモプロジェクトを掲載していますので、コミュニティがここに外部リンクを好きではないだろう怖い...

AndroidManifest.xmlを

<?xml version="1.0" encoding="utf-8"?> 

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/> 
<uses-permission android:name="android.permission.RECORD_AUDIO"/> 


<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <service android:name=".TService"/> 
</application> 

MainActivity.java

私はそののonCreateメソッドでTServiceを始めている、これは、プロジェクト内のすべての活動、またはあなたがからサービスを開始することができます任意のコンテキスト可能性がありますのでご注意下さい。

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     startService(new Intent(this, TService.class)); 
    } 
} 

、今ようやく、 TService.java 私はキットカットとロリポップでこれをテストしてみたし、それが働いている、新しいバージョンを明示的に実行時にマニフェストの許可を求める必要があり、それだけで動作するはずです罰金として

public class TService extends Service { 
    MediaRecorder recorder; 
    File audiofile; 
    String name, phonenumber; 
    String audio_format; 
    public String Audio_Type; 
    int audioSource; 
    Context context; 

    Timer timer; 
    Boolean offHook = false, ringing = false; 
    Toast toast; 
    Boolean isOffHook = false; 
    private boolean recordstarted = false; 

    private static final String ACTION_IN = "android.intent.action.PHONE_STATE"; 
    private static final String ACTION_OUT = "android.intent.action.NEW_OUTGOING_CALL"; 
    private CallBr br_call; 




    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void onDestroy() { 
     Log.d("service", "destroy"); 

     super.onDestroy(); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     // final String terminate =(String) 
     // intent.getExtras().get("terminate");// 
     // intent.getStringExtra("terminate"); 
     // Log.d("TAG", "service started"); 
     // 
     // TelephonyManager telephony = (TelephonyManager) 
     // getSystemService(Context.TELEPHONY_SERVICE); // TelephonyManager 
     // // object 
     // CustomPhoneStateListener customPhoneListener = new 
     // CustomPhoneStateListener(); 
     // telephony.listen(customPhoneListener, 
     // PhoneStateListener.LISTEN_CALL_STATE); 
     // context = getApplicationContext(); 

     final IntentFilter filter = new IntentFilter(); 
     filter.addAction(ACTION_OUT); 
     filter.addAction(ACTION_IN); 
     this.br_call = new CallBr(); 
     this.registerReceiver(this.br_call, filter); 

     // if(terminate != null) { 
     // stopSelf(); 
     // } 
     return START_NOT_STICKY; 
    } 

    public class CallBr extends BroadcastReceiver { 
     Bundle bundle; 
     String state; 
     String inCall, outCall; 
     public boolean wasRinging = false; 
     boolean didMakeACall = false; 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      if (intent.getAction().equals(ACTION_IN)) { 
       if ((bundle = intent.getExtras()) != null) { 
        state = bundle.getString(TelephonyManager.EXTRA_STATE); 
        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 
         inCall = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER); 
         wasRinging = true; 
         Toast.makeText(context, "IN : " + inCall, Toast.LENGTH_LONG).show(); 
        } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) { 
         if (wasRinging || didMakeACall) { 

          Toast.makeText(context, "ANSWERED", Toast.LENGTH_LONG).show(); 

          String out = new SimpleDateFormat("dd-MM-yyyy hh-mm-ss").format(new Date()); 
          File sampleDir = new File(Environment.getExternalStorageDirectory(), "/MyRecorder"); 
          if (!sampleDir.exists()) { 
           sampleDir.mkdirs(); 
          } 
          String file_name = "Record"; 
          try { 
           audiofile = File.createTempFile(file_name, ".amr", sampleDir); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
          String path = Environment.getExternalStorageDirectory().getAbsolutePath(); 
          System.out.println("PATH"+path); 

          recorder = new MediaRecorder(); 
//       recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL); 

          recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION); 
          recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB); 
          recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
          recorder.setOutputFile(audiofile.getAbsolutePath()); 
          try { 
           recorder.prepare(); 
          } catch (IllegalStateException e) { 
           e.printStackTrace(); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
          recorder.start(); 
          recordstarted = true; 
         } 
        } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) { 
         wasRinging = false; 
         Toast.makeText(context, "REJECT || DISCO", Toast.LENGTH_LONG).show(); 
         if (recordstarted) { 
          recorder.stop(); 
          recordstarted = false; 
         } 
        } 
       } 
      } else if (intent.getAction().equals(ACTION_OUT)) { 
       if ((bundle = intent.getExtras()) != null) { 
        didMakeACall = true; 
        outCall = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 
        Toast.makeText(context, "OUT : " + outCall, Toast.LENGTH_LONG).show(); 
       } 
      } 
     } 
    } 

} 
+0

? – chris

+0

htc欲望820gランニングキットキットとエミュレータランチポップ、問題がある場合はgithubプロジェクトを作成して共有できます –

+0

私はエミュレータkitkat..google nexus 7 api level 4.4.4を使用しています。 – chris

関連する問題