2016-11-12 18 views
-1
public class CallReceiver extends BroadcastReceiver { 

    MediaRecorder recorder; 
    TelephonyManager telManager; 
    boolean recordStarted; 
    private Context ctx; 
    static boolean status = false; 
    String phoneNumber; 

    public static SharedPreferences preferences; 
    boolean enabled; 
    private static Date date; 
    public static Boolean isIncoming; 
    private static String dateStr; 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     ctx = context; 
     String action = intent.getAction(); 

     preferences = context.getSharedPreferences("Numbers", Context.MODE_PRIVATE); 
     enabled = preferences.getBoolean("enabled", false); 

     date = new Date(); 

     dateStr = date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds(); 

     if (enabled) { 
      if (status == false) { 
       try { 
        recorder = new MediaRecorder(); 
       } catch (Exception ex) { 
        ex.printStackTrace(); 
       } 

       Bundle extras = intent.getExtras(); 
       if (extras != null) { 
        String state = extras.getString(TelephonyManager.EXTRA_STATE); 
        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 

         phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER); 
         incomingcallrecord(action, context); 

        } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) { 

         phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 
         incomingcallrecord(action, context); 
        } 
       } 
      } else { 
       status = false; 
      } 
     } 
    } 

    private void incomingcallrecord(String action, Context context) { 
     // TODO Auto-generated method stub 
     if (action.equals("android.intent.action.PHONE_STATE")) { 
      telManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
      telManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE); 
     } 
    } 

    private final PhoneStateListener phoneListener = new PhoneStateListener() { 
     @Override 
     public void onCallStateChanged(int state, String incomingNumber) { 

      Log.d("calling number", "calling number" + incomingNumber); 
      try { 
       switch (state) { 
        case TelephonyManager.CALL_STATE_RINGING: { 
         Log.e("CALL_STATE_RINGING", "CALL_STATE_RINGING"); 

         isIncoming = true; 

         recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL); 
         recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 
         recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); 

         Log.e("incoming", "incoming call" + incomingNumber); 

         File file = createDirIfNotExists(); 
         recorder.setOutputFile(file.getAbsolutePath()); 

         recorder.prepare(); 
         Thread.sleep(1000); 
         recorder.start(); 
         recordStarted = true; 
         status = true; 
         Log.e("Record start", " Start"); 

         String insertStr = "IN_" + dateStr; 

         ContentValues values = new ContentValues(); 
         values.put(CallRecorderSQLite.FeedEntry.COLUMN_2_NUMBER, insertStr); 
         Activity_Landing.dbWritable.insert(CallRecorderSQLite.FeedEntry.TABLE_NAME, null, values); 

         Log.d("calling number ringing", "" + incomingNumber); 

         break; 
        } 

        case TelephonyManager.CALL_STATE_OFFHOOK: { 
         Log.e("CALL_STATE_OFFHOOK", "CALL_STATE_OFFHOOK"); 

         isIncoming = false; 

         recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL); 
         recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 
         recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); 

         Log.e("outgoing", "outgoing call" + incomingNumber); 

         File file = createDirIfNotExists(); 
         recorder.setOutputFile(file.getAbsolutePath()); 

         recorder.prepare(); 
         Thread.sleep(1000); 
         recorder.start(); 
         recordStarted = true; 
         status = true; 
         Log.e("Record start", " Start"); 

         String insertStr = "OUT_" + dateStr; 

         ContentValues values = new ContentValues(); 
         values.put(CallRecorderSQLite.FeedEntry.COLUMN_2_NUMBER, insertStr); 
         Activity_Landing.dbWritable.insert(CallRecorderSQLite.FeedEntry.TABLE_NAME, null, values); 

         Log.d("calling number offhook", "" + incomingNumber); 

         break; 
        } 

        case TelephonyManager.CALL_STATE_IDLE: { 

         Log.e("CALL_STATE_IDLE", "CALL_STATE_IDLE"); 
         if (recordStarted) { 
          recorder.stop(); 
          recorder.reset(); 
          recorder.release(); 
          Log.e("Record stop", " stop"); 
          recorder = null; 
          recordStarted = false; 
         } 

         Log.d("calling number idle", "" + incomingNumber); 
         break; 
        } 
        default: { 
        } 
       } 
      } catch (Exception ex) { 

       Log.e("Exception ------", "" + ex.toString()); 
      } 
     } 
    }; 

    public File createDirIfNotExists() { 

     File folder = new File(Environment.getExternalStorageDirectory() + "/PhoneCallRecording"); 
     if (!folder.exists()) { 
      if (!folder.mkdirs()) { 
       Log.e("TravellerLog :: ", "folder is created"); 
      } 
     } 
     File file; 
     if (isIncoming) { 
      file = new File(folder, "IN_" + dateStr + ".amr"); 
     } else { 
      file = new File(folder, "OUT_" + dateStr + ".amr"); 
     } 
     try { 
      if (!file.exists()) { 
       if (file.createNewFile()) { 
        Log.e("TravellerLog :: ", "file is created"); 
       } 
      } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return file; 
    } 
} 

My code発信番号

を得ていない私は、コールレコーダーからAndroidアプリを開発しています。マニフェストファイルでは、私は電話の状態のアクセス許可を使用していますが、発信呼び出しにアクセスできない、私はnullポインタの例外を取得しています。

マニフェスト、これにマニフェスト

<receiver android:name=".Receiver.CallReceiver"> 
    <intent-filter> 
     <action android:name="android.intent.action.PHONE_STATE"></action> 
    </intent-filter> 
</receiver> 
+0

あなたのコードを投稿してください。 –

+0

これは私のマニフェストファイルです。 –

+0

<受信アンドロイド:名= "Receiver.CallReceiver "> <インテントフィルタ> <アクションアンドロイド:名=" android.intent.action.PHONE_STATE">

答えて

0

変更:

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" /> 
<receiver android:name=".MyPhoneReceiver" > 
<intent-filter> 
<action android:name="android.intent.action.NEW_OUTGOING_CALL" /> 
</intent-filter> 
</receiver> 

とコード:

... 
@Override public void onReceive(final Context context, final Intent intent) {  

if (Intent.ACTION_NEW_OUTGOING_CALL.equals(intent.getAction())) { 
    final String originalNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 

} 
... 

このanswer

+0

返信ありがとうございます。解決してください。 –

0
public class CallReceiver extends BroadcastReceiver { 

MediaRecorder recorder; 
TelephonyManager telManager; 
boolean recordStarted; 
private Context ctx; 
static boolean status = false; 
String phoneNumber; 

public static SharedPreferences preferences; 
boolean enabled; 
private static Date date; 
Boolean isIncoming; 
String isInOrOut; 
private static String dateStr; 
static long start_time, end_time; 

String lastCallnumber; 

@Override 
public void onReceive(Context context, Intent intent) { 

    ctx = context; 
    String action = intent.getAction(); 

    preferences = context.getSharedPreferences("Numbers", Context.MODE_PRIVATE); 
    enabled = preferences.getBoolean("enabled", false); 

    date = new Date(); 

    dateStr = date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds(); 

    if (enabled) { 
     if (status == false) { 
      try { 
       recorder = new MediaRecorder(); 
      } catch (Exception ex) { 
       ex.printStackTrace(); 
      } 

      Bundle extras = intent.getExtras(); 
      if (extras != null) { 
       String state = extras.getString(TelephonyManager.EXTRA_STATE); 
       if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 
        incomingcallrecord(action, context); 
       } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) { 
        incomingcallrecord(action, context); 
       } 
      } 
     } else { 
      status = false; 
     } 
    } 
} 

private void incomingcallrecord(String action, Context context) { 
    // TODO Auto-generated method stub 
    if (action.equals("android.intent.action.PHONE_STATE")) { 
     telManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
     telManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE); 
    } 
} 

private final PhoneStateListener phoneListener = new PhoneStateListener() { 
    @Override 
    public void onCallStateChanged(int state, String incomingNumber) { 

     Log.d("calling number", "calling number" + incomingNumber); 
     try { 
      switch (state) { 
       case TelephonyManager.CALL_STATE_RINGING: { 
        Log.e("CALL_STATE_RINGING", "CALL_STATE_RINGING"); 

        isIncoming = true; 

        recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL); 
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); 

        Log.e("incoming", "incoming call" + incomingNumber); 

        phoneNumber = incomingNumber; 

        File file = createDirIfNotExists(); 
        recorder.setOutputFile(file.getAbsolutePath()); 

        recorder.prepare(); 
        Thread.sleep(1000); 
        recorder.start(); 
        recordStarted = true; 
        status = true; 
        Log.e("Record start", " Start"); 

        Log.d("calling number ringing", "" + incomingNumber); 

        break; 
       } 

       case TelephonyManager.CALL_STATE_OFFHOOK: { 
        Log.e("CALL_STATE_OFFHOOK", "CALL_STATE_OFFHOOK"); 

        isIncoming = false; 

        recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL); 
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); 

        Log.e("outgoing", "outgoing call" + lastCallnumber); 

        File file = createDirIfNotExists(); 
        recorder.setOutputFile(file.getAbsolutePath()); 

        recorder.prepare(); 
        Thread.sleep(1000); 
        recorder.start(); 
        recordStarted = true; 
        status = true; 
        Log.e("Record start", " Start"); 

        Log.d("calling number offhook", "" + lastCallnumber); 

        break; 
       } 

       case TelephonyManager.CALL_STATE_IDLE: { 

        Log.e("CALL_STATE_IDLE", "CALL_STATE_IDLE"); 
        if (recordStarted) { 
         recorder.stop(); 
         recorder.reset(); 
         recorder.release(); 
         Log.e("Record stop", " stop"); 
         recorder = null; 
         recordStarted = false; 
        } 

        if (isIncoming) { 

         isInOrOut = "in"; 

         ContentValues values = new ContentValues(); 
         values.put(CallRecorderSQLite.FeedEntry.COLUMN_1_TIME, dateStr); 
         values.put(CallRecorderSQLite.FeedEntry.COLUMN_2_NUMBER, phoneNumber); 
         values.put(CallRecorderSQLite.FeedEntry.COLUMN_3_INOUT, isInOrOut); 
         Activity_Landing.dbWritable.insert(CallRecorderSQLite.FeedEntry.TABLE_NAME, null, values); 

        } else { 

         isInOrOut = "out"; 
         Handler handler = new Handler(); 
         handler.postDelayed(new Runnable() { 

          @Override 
          public void run() { 
           Log.i("CallLogDetailsActivity", "Getting Log activity..."); 
           String[] projection = new String[]{CallLog.Calls.NUMBER}; 
           if (ActivityCompat.checkSelfPermission(ctx, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) { 
            // TODO: Consider calling 
            return; 
           } 
           Cursor cur = ctx.getContentResolver().query(CallLog.Calls.CONTENT_URI, projection, null, null, CallLog.Calls.DATE + " desc"); 
           cur.moveToFirst(); 
           lastCallnumber = cur.getString(0); 

           ContentValues values = new ContentValues(); 
           values.put(CallRecorderSQLite.FeedEntry.COLUMN_1_TIME, dateStr); 
           values.put(CallRecorderSQLite.FeedEntry.COLUMN_2_NUMBER, lastCallnumber); 
           values.put(CallRecorderSQLite.FeedEntry.COLUMN_3_INOUT, isInOrOut); 
           Activity_Landing.dbWritable.insert(CallRecorderSQLite.FeedEntry.TABLE_NAME, null, values); 
          } 
         }, 500); 
        } 

        Log.d("calling number idle", "" + incomingNumber); 
        break; 
       } 
       default: { 
       } 
      } 
     } catch (Exception ex) { 

      Log.e("Exception ------", "" + ex.toString()); 
     } 
    } 
}; 

public File createDirIfNotExists() { 

    File folder = new File(Environment.getExternalStorageDirectory() + "/PhoneCallRecording"); 
    if (!folder.exists()) { 
     if (!folder.mkdirs()) { 
      Log.e("TravellerLog :: ", "folder is created"); 
     } 
    } 
    File file = new File(folder, dateStr + ".amr"); 
    try { 
     if (!file.exists()) { 
      if (file.createNewFile()) { 
       Log.e("TravellerLog :: ", "file is created"); 
      } 
     } 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return file; 
} 
によります

関連する問題