반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- react
- 개발
- MachineLearning
- localserver
- centos
- xcode
- TensorFlow
- MAC
- vsCode
- jest
- 오블완
- IOS
- 맥
- VirtualBox
- androidstudio
- 센토스
- build
- Chrome
- qunit
- 리눅스
- 네트워크
- ReactNative
- unittest
- webpack
- 티스토리챌린지
- node
- Android
- PYTHON
- linux
Archives
- Today
- Total
로메오의 블로그
[Android] CallLog로 전화통화내역 가져오기 본문
반응형
Android 프로젝트 생성
Empty Activity를 선택하고 Next를 누릅니다.
Name, Save location, Language를 설정하고 Finish를 누릅니다.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gaeyou.calllog">
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
</application>
</manifest>
READ_CALL_LOG 권한을 추가합니다.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/get_calllog"
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
android:text="Call Log 가져오기"/>
</RelativeLayout>
버튼을 하나 만듭니다.
MainActivity.java
package com.gaeyou.calllog;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.provider.CallLog;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.util.Date;
import static android.Manifest.permission.READ_CALL_LOG;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final int PERMISSION_REQUEST_CODE = 200;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button get_calllog = (Button) findViewById(R.id.get_calllog);
get_calllog.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int id = view.getId();
switch (id) {
case R.id.get_calllog:
// permission 요청하기
if (!checkPermission()) {
requestPermission();
} else {
// 권한이 있으면 call log를 가지고 옵니다.
String callLog = getCallLog();
Log.d("romeoh", callLog);
}
break;
}
}
// permission 확인
private boolean checkPermission() {
int result = ContextCompat.checkSelfPermission(getApplicationContext(), READ_CALL_LOG);
return result == PackageManager.PERMISSION_GRANTED;
}
// permission 요청
private void requestPermission() {
ActivityCompat.requestPermissions(this,
new String[]{READ_CALL_LOG},
PERMISSION_REQUEST_CODE);
}
// permission 요청 결과
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0) {
boolean callLogAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
if (callLogAccepted) {
// 권한이 있으면 callLog를 가지고 옵니다.
String callLog = getCallLog();
Log.d("romeoh", callLog);
}
}
break;
}
}
// CallLog를 반환합니다.
private String getCallLog() {
StringBuffer sb = new StringBuffer();
Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null,
null, null, null);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
sb.append("Call Details :");
while (managedCursor.moveToNext()) {
String phNumber = managedCursor.getString(number);
String callType = managedCursor.getString(type);
String callDate = managedCursor.getString(date);
Date callDayTime = new Date(Long.valueOf(callDate));
String callDuration = managedCursor.getString(duration);
String dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- "
+ dir + " \nCall Date:--- " + callDayTime
+ " \nCall duration in sec :--- " + callDuration);
sb.append("\n----------------------------------");
}
managedCursor.close();
return sb.toString();
}
}
App build
Android Emulator에서 설정 버튼을 누릅니다.
Phone 메뉴에서 CALL DEVICE 버튼을 몇 번 눌러줍니다.
Call Log 가져오기 버튼을 누르고 permission을 허용합니다.
Android Studio Logcat에서 CallLog를 가져온것을 확인 할 수 있습니다.
반응형
'App & OS > Android' 카테고리의 다른 글
[Android Studio] 안드로이드 앱 배포 (1) | 2020.02.05 |
---|---|
[Android] 오류 Manifest merger failed with multiple errors (0) | 2019.07.10 |
[Android] Runtime permission 권한 인증 받기 (0) | 2019.06.25 |
[Android] terminal에서 Android Emulator 실행하기 (1) | 2019.06.17 |
Comments